kafka-ts 0.0.13 → 0.0.16

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.
@@ -122,6 +122,7 @@ class Connection {
122
122
  }
123
123
  catch (error) {
124
124
  if (error instanceof error_1.KafkaTSApiError) {
125
+ error.apiName = apiName;
125
126
  error.request = body;
126
127
  }
127
128
  throw error;
@@ -42,6 +42,7 @@ export declare class Consumer extends EventEmitter<{
42
42
  start(): Promise<void>;
43
43
  close(force?: boolean): Promise<void>;
44
44
  private startFetchManager;
45
+ private waitForReassignment;
45
46
  private process;
46
47
  private fetch;
47
48
  }
@@ -108,38 +108,36 @@ class Consumer extends events_1.default {
108
108
  async startFetchManager() {
109
109
  const { groupId, batchGranularity, concurrency } = this.options;
110
110
  while (!this.stopHook) {
111
- await this.consumerGroup?.join();
112
- // TODO: If leader is not available, find another read replica
113
- const nodeAssignments = Object.entries((0, messages_to_topic_partition_leaders_1.distributeMessagesToTopicPartitionLeaders)(Object.entries(this.metadata.getAssignment()).flatMap(([topic, partitions]) => partitions.map((partition) => ({ topic, partition }))), this.metadata.getTopicPartitionLeaderIds())).map(([nodeId, assignment]) => ({
114
- nodeId: parseInt(nodeId),
115
- assignment: Object.fromEntries(Object.entries(assignment).map(([topic, partitions]) => [
116
- topic,
117
- Object.keys(partitions).map(Number),
118
- ])),
119
- }));
120
- const numPartitions = Object.values(this.metadata.getAssignment()).flat().length;
121
- const numProcessors = Math.min(concurrency, numPartitions);
122
- this.fetchManager = new fetch_manager_1.FetchManager({
123
- fetch: this.fetch.bind(this),
124
- process: this.process.bind(this),
125
- metadata: this.metadata,
126
- consumerGroup: this.consumerGroup,
127
- nodeAssignments,
128
- batchGranularity,
129
- concurrency: numProcessors,
130
- });
131
111
  try {
112
+ await this.consumerGroup?.join();
113
+ // TODO: If leader is not available, find another read replica
114
+ const nodeAssignments = Object.entries((0, messages_to_topic_partition_leaders_1.distributeMessagesToTopicPartitionLeaders)(Object.entries(this.metadata.getAssignment()).flatMap(([topic, partitions]) => partitions.map((partition) => ({ topic, partition }))), this.metadata.getTopicPartitionLeaderIds())).map(([nodeId, assignment]) => ({
115
+ nodeId: parseInt(nodeId),
116
+ assignment: Object.fromEntries(Object.entries(assignment).map(([topic, partitions]) => [
117
+ topic,
118
+ Object.keys(partitions).map(Number),
119
+ ])),
120
+ }));
121
+ const numPartitions = Object.values(this.metadata.getAssignment()).flat().length;
122
+ const numProcessors = Math.min(concurrency, numPartitions);
123
+ this.fetchManager = new fetch_manager_1.FetchManager({
124
+ fetch: this.fetch.bind(this),
125
+ process: this.process.bind(this),
126
+ metadata: this.metadata,
127
+ consumerGroup: this.consumerGroup,
128
+ nodeAssignments,
129
+ batchGranularity,
130
+ concurrency: numProcessors,
131
+ });
132
132
  await this.fetchManager.start();
133
133
  if (!nodeAssignments.length) {
134
- logger_1.log.debug('No partitions assigned. Waiting for reassignment...', { groupId });
135
- await (0, delay_1.delay)(this.options.maxWaitMs);
136
- this.consumerGroup?.handleLastHeartbeat();
134
+ await this.waitForReassignment();
137
135
  }
138
136
  }
139
137
  catch (error) {
140
- await this.fetchManager.stop();
138
+ await this.fetchManager?.stop();
141
139
  if (error.errorCode === api_1.API_ERROR.REBALANCE_IN_PROGRESS) {
142
- logger_1.log.debug('Rebalance in progress...');
140
+ logger_1.log.debug('Rebalance in progress...', { apiName: error.apiName });
143
141
  continue;
144
142
  }
145
143
  if (error.errorCode === api_1.API_ERROR.FENCED_INSTANCE_ID) {
@@ -162,6 +160,14 @@ class Consumer extends events_1.default {
162
160
  }
163
161
  this.stopHook?.();
164
162
  }
163
+ async waitForReassignment() {
164
+ const { groupId } = this.options;
165
+ logger_1.log.debug('No partitions assigned. Waiting for reassignment...', { groupId });
166
+ while (!this.stopHook) {
167
+ await (0, delay_1.delay)(1000);
168
+ this.consumerGroup?.handleLastHeartbeat();
169
+ }
170
+ }
165
171
  async process(messages) {
166
172
  const { options } = this;
167
173
  const { retrier } = options;
@@ -5,6 +5,7 @@ export declare class KafkaTSApiError<T = any> extends KafkaTSError {
5
5
  errorCode: number;
6
6
  errorMessage: string | null;
7
7
  response: T;
8
+ apiName: string | undefined;
8
9
  request: unknown | undefined;
9
10
  constructor(errorCode: number, errorMessage: string | null, response: T);
10
11
  }
@@ -13,6 +13,7 @@ class KafkaTSApiError extends KafkaTSError {
13
13
  errorCode;
14
14
  errorMessage;
15
15
  response;
16
+ apiName;
16
17
  request;
17
18
  constructor(errorCode, errorMessage, response) {
18
19
  const [errorName] = Object.entries(api_1.API_ERROR).find(([, value]) => value === errorCode) ?? ['UNKNOWN'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kafka-ts",
3
- "version": "0.0.13",
3
+ "version": "0.0.16",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",