@platformatic/kafka 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -148,13 +148,26 @@ export class Base extends EventEmitter {
148
148
  }, callback);
149
149
  }
150
150
  [kMetadata](options, callback) {
151
- const metadataMaxAge = options.metadataMaxAge ?? this[kOptions].metadataMaxAge;
152
- const isStale = options.forceUpdate ||
153
- !this.#metadata ||
154
- Date.now() > this.#metadata.lastUpdate + metadataMaxAge ||
155
- options.topics.some(topic => !this.#metadata?.topics.has(topic));
156
- if (!isStale) {
157
- callback(null, this.#metadata);
151
+ const expiralDate = Date.now() - (options.metadataMaxAge ?? this[kOptions].metadataMaxAge);
152
+ let topicsToFetch = [];
153
+ // Determine which topics we need to fetch
154
+ if (!this.#metadata || options.forceUpdate) {
155
+ topicsToFetch = options.topics;
156
+ }
157
+ else {
158
+ for (const topic of options.topics) {
159
+ const existingTopic = this.#metadata.topics.get(topic);
160
+ if (!existingTopic || existingTopic.lastUpdate < expiralDate) {
161
+ topicsToFetch.push(topic);
162
+ }
163
+ }
164
+ }
165
+ // All topics are already up-to-date, simply return them
166
+ if (this.#metadata && !topicsToFetch.length) {
167
+ callback(null, {
168
+ ...this.#metadata,
169
+ topics: new Map(options.topics.map(topic => [topic, this.#metadata.topics.get(topic)]))
170
+ });
158
171
  return;
159
172
  }
160
173
  const autocreateTopics = options.autocreateTopics ?? this[kOptions].autocreateTopics;
@@ -178,12 +191,26 @@ export class Base extends EventEmitter {
178
191
  deduplicateCallback(error, undefined);
179
192
  return;
180
193
  }
194
+ const lastUpdate = Date.now();
195
+ if (!this.#metadata) {
196
+ this.#metadata = {
197
+ id: metadata.clusterId,
198
+ brokers: new Map(),
199
+ topics: new Map(),
200
+ lastUpdate
201
+ };
202
+ }
203
+ else {
204
+ this.#metadata.lastUpdate = lastUpdate;
205
+ }
181
206
  const brokers = new Map();
182
- const topics = new Map();
207
+ // This should never change, but we act defensively here
183
208
  for (const broker of metadata.brokers) {
184
209
  const { host, port } = broker;
185
210
  brokers.set(broker.nodeId, { host, port });
186
211
  }
212
+ this.#metadata.brokers = brokers;
213
+ // Update all the topics in the cache
187
214
  for (const { name, topicId: id, partitions: rawPartitions, isInternal } of metadata.topics) {
188
215
  /* c8 ignore next 3 - Sometimes internal topics might be returned by Kafka */
189
216
  if (isInternal) {
@@ -197,16 +224,15 @@ export class Base extends EventEmitter {
197
224
  replicas: rawPartition.replicaNodes
198
225
  };
199
226
  }
200
- topics.set(name, { id, partitions, partitionsCount: rawPartitions.length });
227
+ this.#metadata.topics.set(name, { id, partitions, partitionsCount: rawPartitions.length, lastUpdate });
201
228
  }
202
- this.#metadata = {
203
- id: metadata.clusterId,
204
- brokers,
205
- topics,
206
- lastUpdate: Date.now()
229
+ // Now build the object to return
230
+ const updatedMetadata = {
231
+ ...this.#metadata,
232
+ topics: new Map(options.topics.map(topic => [topic, this.#metadata.topics.get(topic)]))
207
233
  };
208
- this.emitWithDebug('client', 'metadata', this.#metadata);
209
- deduplicateCallback(null, this.#metadata);
234
+ this.emitWithDebug('client', 'metadata', updatedMetadata);
235
+ deduplicateCallback(null, updatedMetadata);
210
236
  }, 0);
211
237
  }, callback);
212
238
  }
@@ -14,6 +14,7 @@ export interface ClusterTopicMetadata {
14
14
  id: string;
15
15
  partitions: ClusterPartitionMetadata[];
16
16
  partitionsCount: number;
17
+ lastUpdate: number;
17
18
  }
18
19
  export interface ClusterMetadata {
19
20
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/kafka",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Modern and performant client for Apache Kafka",
5
5
  "homepage": "https://github.com/platformatic/kafka",
6
6
  "author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",