rocketmq-client-nodejs 1.0.5 → 1.0.7

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.
Files changed (33) hide show
  1. package/dist/consumer/ConsumeResult.d.ts +38 -3
  2. package/dist/consumer/ConsumeResult.js +58 -7
  3. package/dist/consumer/FifoConsumeService.d.ts +33 -1
  4. package/dist/consumer/FifoConsumeService.js +72 -87
  5. package/dist/consumer/LiteFifoConsumeService.d.ts +44 -0
  6. package/dist/consumer/LiteFifoConsumeService.js +94 -0
  7. package/dist/consumer/LitePushConsumer.d.ts +14 -0
  8. package/dist/consumer/LitePushConsumer.js +25 -4
  9. package/dist/consumer/LitePushConsumerImpl.d.ts +9 -0
  10. package/dist/consumer/LitePushConsumerImpl.js +30 -1
  11. package/dist/consumer/LiteStandardConsumeService.d.ts +32 -0
  12. package/dist/consumer/LiteStandardConsumeService.js +63 -0
  13. package/dist/consumer/LiteSubscriptionManager.js +3 -1
  14. package/dist/consumer/ProcessQueue.js +50 -6
  15. package/dist/consumer/PushConsumer.d.ts +1 -0
  16. package/dist/consumer/PushConsumer.js +5 -4
  17. package/dist/consumer/index.d.ts +2 -0
  18. package/dist/consumer/index.js +3 -1
  19. package/dist/message/MessageView.d.ts +8 -1
  20. package/dist/message/MessageView.js +11 -1
  21. package/package.json +1 -1
  22. package/proto/apache/rocketmq/v2/definition.proto +1 -1
  23. package/src/consumer/ConsumeResult.ts +62 -3
  24. package/src/consumer/FifoConsumeService.ts +78 -93
  25. package/src/consumer/LiteFifoConsumeService.ts +103 -0
  26. package/src/consumer/LitePushConsumer.ts +29 -3
  27. package/src/consumer/LitePushConsumerImpl.ts +36 -0
  28. package/src/consumer/LiteStandardConsumeService.ts +73 -0
  29. package/src/consumer/LiteSubscriptionManager.ts +2 -0
  30. package/src/consumer/ProcessQueue.ts +65 -6
  31. package/src/consumer/PushConsumer.ts +4 -3
  32. package/src/consumer/index.ts +2 -0
  33. package/src/message/MessageView.ts +12 -1
@@ -20,7 +20,7 @@ import { Code } from '../../proto/apache/rocketmq/v2/definition_pb';
20
20
  import { MessageView } from '../message';
21
21
  import { MessageQueue } from '../route';
22
22
  import { TooManyRequestsException } from '../exception';
23
- import { ConsumeResult } from './ConsumeResult';
23
+ import { ConsumeResult, ConsumeResultSuspend } from './ConsumeResult';
24
24
  import { FilterExpression } from './FilterExpression';
25
25
  import type { PushConsumer } from './PushConsumer';
26
26
 
@@ -73,6 +73,9 @@ export class ProcessQueue {
73
73
  }
74
74
 
75
75
  cacheMessages(messageList: MessageView[]): void {
76
+ if (!messageList || messageList.length === 0) {
77
+ return;
78
+ }
76
79
  for (const messageView of messageList) {
77
80
  this.#cachedMessages.push(messageView);
78
81
  this.#cachedMessagesBytes += messageView.body.length;
@@ -187,6 +190,11 @@ export class ProcessQueue {
187
190
  }
188
191
 
189
192
  eraseMessage(messageView: MessageView, consumeResult: ConsumeResult): void {
193
+ if (!messageView) {
194
+ return;
195
+ }
196
+ consumeResult = this.#convertSuspendResultIfNeeded(consumeResult);
197
+ this.#statsConsumptionResult(consumeResult);
190
198
  const task = consumeResult === ConsumeResult.SUCCESS
191
199
  ? this.#ackMessage(messageView)
192
200
  : this.#nackMessage(messageView);
@@ -256,22 +264,51 @@ export class ProcessQueue {
256
264
  }
257
265
 
258
266
  async eraseFifoMessage(messageView: MessageView, consumeResult: ConsumeResult): Promise<void> {
267
+ if (!messageView) {
268
+ return;
269
+ }
270
+ consumeResult = this.#convertSuspendResultIfNeeded(consumeResult);
271
+ this.#statsConsumptionResult(consumeResult);
259
272
  const retryPolicy = this.#consumer.getRetryPolicy();
260
273
  const maxAttempts = retryPolicy?.getMaxAttempts() ?? 1;
261
274
  const attempt = messageView.deliveryAttempt ?? 1;
275
+ const messageId = messageView.messageId;
276
+ const clientId = (this.#consumer as any).clientId;
262
277
 
263
278
  if (consumeResult === ConsumeResult.FAILURE && attempt < maxAttempts) {
264
279
  const nextAttemptDelay = retryPolicy?.getNextAttemptDelay(attempt) ?? 0;
280
+ // Increment delivery attempt before redelivering
281
+ messageView.incrementAndGetDeliveryAttempt();
282
+ (this.#consumer as any).logger?.debug(
283
+ 'Prepare to redeliver the fifo message because of the consumption failure, maxAttempts=%d, '
284
+ + 'attempt=%d, mq=%s, messageId=%s, nextAttemptDelay=%d, clientId=%s',
285
+ maxAttempts, messageView.deliveryAttempt, this.#mq, messageId, nextAttemptDelay, clientId,
286
+ );
265
287
  // Redeliver the fifo message
266
288
  const result = await this.#consumer.getConsumeService().consumeMessage(messageView, nextAttemptDelay);
267
289
  await this.eraseFifoMessage(messageView, result);
290
+ return;
291
+ }
292
+
293
+ if (consumeResult === ConsumeResult.SUCCESS) {
294
+ await this.#ackMessage(messageView);
295
+ } else if (consumeResult instanceof ConsumeResultSuspend) {
296
+ const suspendTimeMs = consumeResult.suspendTimeMs;
297
+ (this.#consumer as any).logger?.info(
298
+ 'Suspend consumption, consumerGroup=%s, topic=%s, liteTopic=%s, messageId=%s, result=%s',
299
+ this.#consumer.getConsumerGroup(), messageView.topic, messageView.liteTopic ?? '',
300
+ messageId, consumeResult,
301
+ );
302
+ await this.#changeInvisibleDuration(messageView, suspendTimeMs);
268
303
  } else {
269
- const task = consumeResult === ConsumeResult.SUCCESS
270
- ? this.#ackMessage(messageView)
271
- : this.#forwardToDeadLetterQueue(messageView);
272
- await task;
273
- this.#evictCache(messageView);
304
+ (this.#consumer as any).logger?.info(
305
+ 'Failed to consume fifo message finally, run out of attempt times, maxAttempts=%d, attempt=%d, '
306
+ + 'mq=%s, messageId=%s, clientId=%s',
307
+ maxAttempts, attempt, this.#mq, messageId, clientId,
308
+ );
309
+ await this.#forwardToDeadLetterQueue(messageView);
274
310
  }
311
+ this.#evictCache(messageView);
275
312
  }
276
313
 
277
314
  async #forwardToDeadLetterQueue(messageView: MessageView, attempt = 1): Promise<void> {
@@ -325,6 +362,28 @@ export class ProcessQueue {
325
362
  }
326
363
  }
327
364
 
365
+ #convertSuspendResultIfNeeded(consumeResult: ConsumeResult): ConsumeResult {
366
+ if (consumeResult instanceof ConsumeResultSuspend) {
367
+ if (!(this.#consumer as any).isLiteConsumer?.()) {
368
+ (this.#consumer as any).logger?.warn(
369
+ 'Only LitePushConsumer support ConsumeResultSuspend! Convert to ConsumeResult.FAILURE, '
370
+ + 'consumerGroup=%s, consumerType=%s',
371
+ this.#consumer.getConsumerGroup(), this.#consumer.constructor.name,
372
+ );
373
+ return ConsumeResult.FAILURE;
374
+ }
375
+ }
376
+ return consumeResult;
377
+ }
378
+
379
+ #statsConsumptionResult(consumeResult: ConsumeResult): void {
380
+ if (consumeResult === ConsumeResult.SUCCESS) {
381
+ this.#consumer.incrementConsumptionOkQuantity();
382
+ return;
383
+ }
384
+ this.#consumer.incrementConsumptionErrorQuantity();
385
+ }
386
+
328
387
  cachedMessagesCount(): number {
329
388
  return this.#cachedMessages.length;
330
389
  }
@@ -103,7 +103,7 @@ export class PushConsumer extends Consumer {
103
103
  await super.startup();
104
104
  this.logger.info('Super startup completed, clientId=%s', this.clientId);
105
105
  try {
106
- this.#consumeService = this.#createConsumeService();
106
+ this.#consumeService = this.createConsumeService();
107
107
  // Start scanning assignments periodically
108
108
  this.startAssignmentScanning();
109
109
  this.logger.info('Push consumer started successfully, clientId=%s', this.clientId);
@@ -181,7 +181,7 @@ export class PushConsumer extends Consumer {
181
181
  // No-op for push consumer; assignments are queried separately
182
182
  }
183
183
 
184
- #createConsumeService(): ConsumeService {
184
+ protected createConsumeService(): ConsumeService {
185
185
  if (this.#pushSubscriptionSettings.isFifo()) {
186
186
  return new FifoConsumeService(this.clientId, this.#messageListener, this.#enableFifoConsumeAccelerator);
187
187
  }
@@ -323,9 +323,10 @@ export class PushConsumer extends Consumer {
323
323
  .setInvisibleDuration(createDuration(invisibleDuration))
324
324
  .setMessageId(messageView.messageId);
325
325
 
326
- // For lite consumers, must set liteTopic in request
326
+ // For lite consumers, must set liteTopic and suspend in request
327
327
  if (this.isLiteConsumer() && messageView.liteTopic) {
328
328
  request.setLiteTopic(messageView.liteTopic);
329
+ request.setSuspend(true);
329
330
  }
330
331
 
331
332
  return request;
@@ -29,6 +29,8 @@ export * from './ConsumeTask';
29
29
  export * from './ConsumeService';
30
30
  export * from './StandardConsumeService';
31
31
  export * from './FifoConsumeService';
32
+ export * from './LiteFifoConsumeService';
33
+ export * from './LiteStandardConsumeService';
32
34
  export * from './ProcessQueue';
33
35
  export * from './PushConsumer';
34
36
  export * from './LitePushConsumer';
@@ -36,7 +36,7 @@ export class MessageView {
36
36
  readonly keys: string[];
37
37
  readonly bornHost: string;
38
38
  readonly bornTimestamp?: Date;
39
- readonly deliveryAttempt?: number;
39
+ deliveryAttempt?: number;
40
40
  readonly endpoints: Endpoints;
41
41
  readonly receiptHandle: string;
42
42
  readonly offset?: number;
@@ -218,4 +218,15 @@ export class MessageView {
218
218
  getDeliveryAttempt(): number | undefined {
219
219
  return this.deliveryAttempt;
220
220
  }
221
+
222
+ /**
223
+ * Increment the delivery attempt and return the new value.
224
+ * Used by FIFO retry logic to track redelivery attempts.
225
+ *
226
+ * @return the incremented delivery attempt
227
+ */
228
+ incrementAndGetDeliveryAttempt(): number {
229
+ this.deliveryAttempt = (this.deliveryAttempt ?? 0) + 1;
230
+ return this.deliveryAttempt;
231
+ }
221
232
  }