rocketmq-client-nodejs 1.0.8 → 1.1.1
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.
- package/README.md +61 -0
- package/dist/client/BaseClient.d.ts +44 -1
- package/dist/client/BaseClient.js +353 -85
- package/dist/client/Logger.js +9 -6
- package/dist/client/RpcClient.js +42 -134
- package/dist/client/RpcClientManager.d.ts +22 -17
- package/dist/client/RpcClientManager.js +61 -56
- package/dist/client/TelemetrySession.js +107 -17
- package/dist/consumer/ConsumeService.d.ts +2 -1
- package/dist/consumer/ConsumeService.js +12 -6
- package/dist/consumer/ConsumeTask.d.ts +6 -1
- package/dist/consumer/ConsumeTask.js +36 -5
- package/dist/consumer/Consumer.d.ts +13 -5
- package/dist/consumer/Consumer.js +94 -33
- package/dist/consumer/FifoConsumeService.d.ts +3 -1
- package/dist/consumer/FifoConsumeService.js +4 -4
- package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
- package/dist/consumer/LiteConsumerSupport.js +47 -0
- package/dist/consumer/LiteFifoConsumeService.d.ts +3 -1
- package/dist/consumer/LiteFifoConsumeService.js +4 -4
- package/dist/consumer/LitePushConsumerImpl.d.ts +15 -4
- package/dist/consumer/LitePushConsumerImpl.js +26 -30
- package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
- package/dist/consumer/LiteSimpleConsumer.js +126 -0
- package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
- package/dist/consumer/LiteSimpleConsumerImpl.js +179 -0
- package/dist/consumer/LiteStandardConsumeService.d.ts +3 -1
- package/dist/consumer/LiteStandardConsumeService.js +4 -4
- package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
- package/dist/consumer/LiteSubscriptionManager.js +36 -27
- package/dist/consumer/OffsetOption.d.ts +1 -0
- package/dist/consumer/OffsetOption.js +10 -4
- package/dist/consumer/ProcessQueue.d.ts +17 -1
- package/dist/consumer/ProcessQueue.js +134 -91
- package/dist/consumer/PushConsumer.d.ts +17 -0
- package/dist/consumer/PushConsumer.js +117 -26
- package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
- package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
- package/dist/consumer/PushSubscriptionSettings.js +35 -8
- package/dist/consumer/SimpleConsumer.d.ts +5 -0
- package/dist/consumer/SimpleConsumer.js +33 -20
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +3 -1
- package/dist/consumer/StandardConsumeService.js +17 -8
- package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
- package/dist/consumer/index.d.ts +1 -0
- package/dist/consumer/index.js +2 -1
- package/dist/exception/InternalErrorException.d.ts +1 -0
- package/dist/exception/InternalErrorException.js +3 -2
- package/dist/exception/PayloadEmptyException.d.ts +20 -0
- package/dist/exception/PayloadEmptyException.js +28 -0
- package/dist/exception/StatusChecker.js +11 -4
- package/dist/exception/index.d.ts +1 -0
- package/dist/exception/index.js +2 -1
- package/dist/exception/receiveStatus.d.ts +18 -0
- package/dist/exception/receiveStatus.js +28 -0
- package/dist/hook/Attribute.d.ts +26 -0
- package/dist/hook/Attribute.js +37 -0
- package/dist/hook/AttributeKey.d.ts +32 -0
- package/dist/hook/AttributeKey.js +43 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +32 -0
- package/dist/hook/CompositedMessageInterceptor.js +87 -0
- package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
- package/dist/hook/InflightRequestCountInterceptor.js +44 -0
- package/dist/hook/MessageHookPoints.d.ts +54 -0
- package/dist/hook/MessageHookPoints.js +59 -0
- package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
- package/dist/hook/MessageHookPointsStatus.js +29 -0
- package/dist/hook/MessageInterceptor.d.ts +34 -0
- package/dist/hook/MessageInterceptor.js +19 -0
- package/dist/hook/MessageInterceptorContext.d.ts +31 -0
- package/dist/hook/MessageInterceptorContext.js +19 -0
- package/dist/hook/MessageInterceptorContextImpl.d.ts +40 -0
- package/dist/hook/MessageInterceptorContextImpl.js +71 -0
- package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
- package/dist/hook/MessageMeterInterceptor.js +149 -0
- package/dist/hook/index.d.ts +26 -0
- package/dist/hook/index.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/message/Message.js +26 -4
- package/dist/message/MessageView.js +15 -5
- package/dist/message/PublishingMessage.js +4 -4
- package/dist/metrics/ClientMeter.d.ts +42 -0
- package/dist/metrics/ClientMeter.js +75 -0
- package/dist/metrics/ClientMeterManager.d.ts +46 -0
- package/dist/metrics/ClientMeterManager.js +203 -0
- package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
- package/dist/metrics/EmptyGaugeObserver.js +34 -0
- package/dist/metrics/GaugeEnum.d.ts +32 -0
- package/dist/metrics/GaugeEnum.js +37 -0
- package/dist/metrics/GaugeObserver.d.ts +29 -0
- package/dist/metrics/GaugeObserver.js +19 -0
- package/dist/metrics/HistogramEnum.d.ts +54 -0
- package/dist/metrics/HistogramEnum.js +71 -0
- package/dist/metrics/InvocationStatus.d.ts +26 -0
- package/dist/metrics/InvocationStatus.js +33 -0
- package/dist/metrics/Metric.d.ts +30 -0
- package/dist/metrics/Metric.js +44 -0
- package/dist/metrics/MetricLabels.d.ts +27 -0
- package/dist/metrics/MetricLabels.js +34 -0
- package/dist/metrics/index.d.ts +25 -0
- package/dist/metrics/index.js +42 -0
- package/dist/producer/Producer.d.ts +8 -2
- package/dist/producer/Producer.js +83 -34
- package/dist/producer/PublishingLoadBalancer.js +11 -5
- package/dist/producer/PublishingSettings.js +1 -3
- package/dist/producer/SendReceipt.js +3 -2
- package/dist/producer/Transaction.d.ts +2 -1
- package/dist/producer/Transaction.js +9 -8
- package/dist/producer/index.d.ts +1 -0
- package/dist/producer/index.js +2 -2
- package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
- package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
- package/dist/retry/RetryPolicy.d.ts +1 -1
- package/dist/retry/index.d.ts +1 -0
- package/dist/retry/index.js +2 -1
- package/dist/route/Endpoints.d.ts +6 -0
- package/dist/route/Endpoints.js +69 -9
- package/dist/route/IpNameResolver.d.ts +28 -0
- package/dist/route/IpNameResolver.js +104 -0
- package/dist/route/MessageQueue.js +7 -2
- package/dist/route/TopicRouteData.d.ts +5 -0
- package/dist/route/TopicRouteData.js +22 -1
- package/dist/util/Semaphore.d.ts +47 -0
- package/dist/util/Semaphore.js +82 -0
- package/dist/util/index.d.ts +1 -1
- package/dist/util/index.js +9 -6
- package/dist/util/rpcContext.d.ts +20 -0
- package/dist/util/rpcContext.js +35 -0
- package/package.json +11 -4
- package/src/client/BaseClient.ts +392 -91
- package/src/client/Logger.ts +9 -5
- package/src/client/RpcClient.ts +62 -133
- package/src/client/RpcClientManager.ts +103 -60
- package/src/client/TelemetrySession.ts +112 -17
- package/src/consumer/ConsumeService.ts +16 -5
- package/src/consumer/ConsumeTask.ts +43 -3
- package/src/consumer/Consumer.ts +116 -35
- package/src/consumer/FifoConsumeService.ts +5 -3
- package/src/consumer/LiteConsumerSupport.ts +49 -0
- package/src/consumer/LiteFifoConsumeService.ts +5 -3
- package/src/consumer/LitePushConsumerImpl.ts +34 -23
- package/src/consumer/LiteSimpleConsumer.ts +251 -0
- package/src/consumer/LiteSimpleConsumerImpl.ts +214 -0
- package/src/consumer/LiteStandardConsumeService.ts +5 -3
- package/src/consumer/LiteSubscriptionManager.ts +66 -41
- package/src/consumer/OffsetOption.ts +10 -3
- package/src/consumer/ProcessQueue.ts +165 -107
- package/src/consumer/PushConsumer.ts +133 -24
- package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
- package/src/consumer/PushSubscriptionSettings.ts +40 -9
- package/src/consumer/SimpleConsumer.ts +39 -23
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +18 -7
- package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
- package/src/consumer/index.ts +1 -0
- package/src/exception/InternalErrorException.ts +3 -1
- package/src/exception/PayloadEmptyException.ts +25 -0
- package/src/exception/StatusChecker.ts +10 -2
- package/src/exception/index.ts +1 -0
- package/src/exception/receiveStatus.ts +25 -0
- package/src/hook/Attribute.ts +36 -0
- package/src/hook/AttributeKey.ts +42 -0
- package/src/hook/CompositedMessageInterceptor.ts +96 -0
- package/src/hook/InflightRequestCountInterceptor.ts +46 -0
- package/src/hook/MessageHookPoints.ts +55 -0
- package/src/hook/MessageHookPointsStatus.ts +25 -0
- package/src/hook/MessageInterceptor.ts +35 -0
- package/src/hook/MessageInterceptorContext.ts +33 -0
- package/src/hook/MessageInterceptorContextImpl.ts +83 -0
- package/src/hook/MessageMeterInterceptor.ts +162 -0
- package/src/hook/index.ts +27 -0
- package/src/index.ts +1 -0
- package/src/message/Message.ts +25 -3
- package/src/message/MessageView.ts +13 -4
- package/src/message/PublishingMessage.ts +3 -3
- package/src/metrics/ClientMeter.ts +81 -0
- package/src/metrics/ClientMeterManager.ts +227 -0
- package/src/metrics/EmptyGaugeObserver.ts +36 -0
- package/src/metrics/GaugeEnum.ts +33 -0
- package/src/metrics/GaugeObserver.ts +32 -0
- package/src/metrics/HistogramEnum.ts +69 -0
- package/src/metrics/InvocationStatus.ts +28 -0
- package/src/metrics/Metric.ts +42 -0
- package/src/metrics/MetricLabels.ts +31 -0
- package/src/metrics/index.ts +26 -0
- package/src/producer/Producer.ts +94 -38
- package/src/producer/PublishingLoadBalancer.ts +10 -4
- package/src/producer/PublishingSettings.ts +0 -2
- package/src/producer/SendReceipt.ts +2 -1
- package/src/producer/Transaction.ts +12 -11
- package/src/producer/index.ts +1 -1
- package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
- package/src/retry/RetryPolicy.ts +1 -1
- package/src/retry/index.ts +1 -0
- package/src/route/Endpoints.ts +65 -8
- package/src/route/IpNameResolver.ts +119 -0
- package/src/route/MessageQueue.ts +6 -1
- package/src/route/TopicRouteData.ts +22 -1
- package/src/util/Semaphore.ts +82 -0
- package/src/util/index.ts +8 -5
- package/src/util/rpcContext.ts +34 -0
|
@@ -16,13 +16,16 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { randomUUID } from 'node:crypto';
|
|
19
|
-
import { Code } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { Code, Status } 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 { getRequestId } from '../util/rpcContext';
|
|
23
24
|
import { ConsumeResult, ConsumeResultSuspend } from './ConsumeResult';
|
|
24
25
|
import { FilterExpression } from './FilterExpression';
|
|
25
26
|
import type { PushConsumer } from './PushConsumer';
|
|
27
|
+
import { Semaphore } from '../util/Semaphore';
|
|
28
|
+
import { ILogger, getDefaultLogger } from '../client/Logger';
|
|
26
29
|
|
|
27
30
|
const ACK_MESSAGE_FAILURE_BACKOFF_DELAY = 1000;
|
|
28
31
|
const CHANGE_INVISIBLE_DURATION_FAILURE_BACKOFF_DELAY = 1000;
|
|
@@ -31,9 +34,13 @@ const FORWARD_MESSAGE_TO_DLQ_FAILURE_BACKOFF_DELAY = 1000;
|
|
|
31
34
|
const RECEIVING_FLOW_CONTROL_BACKOFF_DELAY = 20;
|
|
32
35
|
const RECEIVING_FAILURE_BACKOFF_DELAY = 1000;
|
|
33
36
|
const RECEIVING_BACKOFF_DELAY_WHEN_CACHE_IS_FULL = 1000;
|
|
37
|
+
const RECEIVING_BACKOFF_DELAY_WHEN_PERMITS_EXHAUSTED = 20;
|
|
34
38
|
|
|
35
39
|
export class ProcessQueue {
|
|
36
40
|
readonly #consumer: PushConsumer;
|
|
41
|
+
readonly #logger: ILogger;
|
|
42
|
+
readonly #clientId: string;
|
|
43
|
+
readonly #liteConsumer: boolean;
|
|
37
44
|
#dropped = false;
|
|
38
45
|
readonly #mq: MessageQueue;
|
|
39
46
|
readonly #filterExpression: FilterExpression;
|
|
@@ -42,17 +49,47 @@ export class ProcessQueue {
|
|
|
42
49
|
#activityTime = Date.now();
|
|
43
50
|
#cacheFullTime = 0;
|
|
44
51
|
#aborted = false;
|
|
45
|
-
|
|
46
|
-
|
|
52
|
+
#inflightReceives = 0;
|
|
53
|
+
// Bounds in-flight messages per queue; mirrors Java ProcessQueueImpl permits.
|
|
54
|
+
readonly #semaphore: Semaphore;
|
|
55
|
+
|
|
56
|
+
// `context` carries the logger and identity of the owning consumer. PushConsumer always passes it;
|
|
57
|
+
// when omitted they are read from the consumer so existing three-argument calls keep working.
|
|
58
|
+
constructor(consumer: PushConsumer, mq: MessageQueue, filterExpression: FilterExpression,
|
|
59
|
+
context?: { logger: ILogger; clientId: string; liteConsumer: boolean }) {
|
|
60
|
+
context = context ?? ProcessQueue.#contextOf(consumer);
|
|
47
61
|
this.#consumer = consumer;
|
|
62
|
+
this.#logger = context.logger;
|
|
63
|
+
this.#clientId = context.clientId;
|
|
64
|
+
this.#liteConsumer = context.liteConsumer;
|
|
48
65
|
this.#mq = mq;
|
|
49
66
|
this.#filterExpression = filterExpression;
|
|
67
|
+
this.#semaphore = new Semaphore(consumer.getPushConsumerSettings().getConsumeConcurrentlyMax());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static #contextOf(consumer: PushConsumer) {
|
|
71
|
+
// logger and isLiteConsumer() are protected on the consumer; this is the only place
|
|
72
|
+
// that reads them, for callers constructing a process queue without a context.
|
|
73
|
+
const legacy = consumer as unknown as { logger?: ILogger; isLiteConsumer?: () => boolean };
|
|
74
|
+
return {
|
|
75
|
+
logger: legacy.logger ?? getDefaultLogger(),
|
|
76
|
+
clientId: consumer.clientId,
|
|
77
|
+
liteConsumer: legacy.isLiteConsumer?.() ?? false,
|
|
78
|
+
};
|
|
50
79
|
}
|
|
51
80
|
|
|
52
81
|
getMessageQueue(): MessageQueue {
|
|
53
82
|
return this.#mq;
|
|
54
83
|
}
|
|
55
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Topic of the underlying message queue, used as a metric attribute when
|
|
87
|
+
* aggregating the consumer gauges.
|
|
88
|
+
*/
|
|
89
|
+
get topic(): string {
|
|
90
|
+
return this.#mq.topic.name;
|
|
91
|
+
}
|
|
92
|
+
|
|
56
93
|
drop(): void {
|
|
57
94
|
this.#dropped = true;
|
|
58
95
|
}
|
|
@@ -77,21 +114,41 @@ export class ProcessQueue {
|
|
|
77
114
|
return;
|
|
78
115
|
}
|
|
79
116
|
for (const messageView of messageList) {
|
|
117
|
+
// Each in-flight message consumes one permit. The reception batch size is
|
|
118
|
+
// already bounded by availablePermits, so tryAcquire should always succeed;
|
|
119
|
+
// if it does not (defensive), stop caching to avoid over-committing.
|
|
120
|
+
if (!this.#semaphore.tryAcquire()) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
80
123
|
this.#cachedMessages.push(messageView);
|
|
81
124
|
this.#cachedMessagesBytes += messageView.body.length;
|
|
82
125
|
}
|
|
83
126
|
}
|
|
84
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Number of consumption permits still available on this process queue. When it
|
|
130
|
+
* reaches zero the receive loop applies backpressure (see #receiveMessageImmediately).
|
|
131
|
+
*/
|
|
132
|
+
availablePermits(): number {
|
|
133
|
+
return this.#semaphore.availablePermits;
|
|
134
|
+
}
|
|
135
|
+
|
|
85
136
|
#getReceptionBatchSize(): number {
|
|
86
137
|
const bufferSize = Math.max(
|
|
87
138
|
this.#consumer.cacheMessageCountThresholdPerQueue() - this.cachedMessagesCount(),
|
|
88
139
|
1,
|
|
89
140
|
);
|
|
90
|
-
|
|
141
|
+
// Bound the batch by the remaining consumption permits, mirroring Java's
|
|
142
|
+
// Math.max(consumeConcurrentlyMax - inflight, 1).
|
|
143
|
+
const permitSize = Math.max(this.#semaphore.availablePermits, 1);
|
|
144
|
+
return Math.min(bufferSize, permitSize, this.#consumer.getPushConsumerSettings().getReceiveBatchSize());
|
|
91
145
|
}
|
|
92
146
|
|
|
93
147
|
fetchMessageImmediately(): void {
|
|
94
|
-
(this.#
|
|
148
|
+
if (this.#dropped || this.#aborted) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.#logger.info('ProcessQueue.fetchMessageImmediately called, mq=%s@%s@%d',
|
|
95
152
|
this.#mq.topic.name, this.#mq.broker.name, this.#mq.queueId);
|
|
96
153
|
this.#receiveMessageImmediately();
|
|
97
154
|
}
|
|
@@ -104,9 +161,13 @@ export class ProcessQueue {
|
|
|
104
161
|
}
|
|
105
162
|
|
|
106
163
|
#receiveMessageLater(delay: number, attemptId?: string): void {
|
|
107
|
-
if (this.#aborted)
|
|
164
|
+
if (this.#dropped || this.#aborted) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
108
167
|
setTimeout(() => {
|
|
109
|
-
if (this.#aborted)
|
|
168
|
+
if (this.#dropped || this.#aborted) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
110
171
|
this.#receiveMessage(attemptId);
|
|
111
172
|
}, delay);
|
|
112
173
|
}
|
|
@@ -131,7 +192,16 @@ export class ProcessQueue {
|
|
|
131
192
|
}
|
|
132
193
|
|
|
133
194
|
#receiveMessageImmediately(attemptId?: string): void {
|
|
134
|
-
if (this.#aborted)
|
|
195
|
+
if (this.#dropped || this.#aborted) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// Apply backpressure: when every consumption permit is in use (i.e. the
|
|
199
|
+
// maximum number of messages is in-flight), stop fetching until messages are
|
|
200
|
+
// settled and permits are released. Mirrors Java's tryAcquirePermit gate.
|
|
201
|
+
if (this.#semaphore.availablePermits <= 0) {
|
|
202
|
+
this.#receiveMessageLater(RECEIVING_BACKOFF_DELAY_WHEN_PERMITS_EXHAUSTED, attemptId);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
135
205
|
attemptId = attemptId ?? this.#generateAttemptId();
|
|
136
206
|
try {
|
|
137
207
|
const batchSize = this.#getReceptionBatchSize();
|
|
@@ -141,30 +211,34 @@ export class ProcessQueue {
|
|
|
141
211
|
);
|
|
142
212
|
this.#activityTime = Date.now();
|
|
143
213
|
|
|
144
|
-
|
|
214
|
+
this.#logger.debug?.('Start to receive message, mq=%s@%s@%d, batchSize=%d, attemptId=%s',
|
|
145
215
|
this.#mq.topic.name, this.#mq.broker.name, this.#mq.queueId, batchSize, attemptId);
|
|
146
216
|
|
|
147
|
-
this.#consumer.receiveMessage(request, this.#mq, longPollingTimeout)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
217
|
+
const receiving = this.#consumer.receiveMessage(request, this.#mq, longPollingTimeout);
|
|
218
|
+
this.#inflightReceives++;
|
|
219
|
+
receiving.then(messages => {
|
|
220
|
+
if (messages.length > 0) {
|
|
221
|
+
this.#logger.debug?.('Received messages, count=%d, mq=%s@%s@%d',
|
|
222
|
+
messages.length, this.#mq.topic.name, this.#mq.broker.name, this.#mq.queueId);
|
|
223
|
+
}
|
|
224
|
+
this.#onReceiveMessageResult(messages);
|
|
225
|
+
})
|
|
155
226
|
.catch(err => {
|
|
156
|
-
|
|
227
|
+
this.#logger.error('Failed to receive message, mq=%s@%s@%d, error=%s',
|
|
157
228
|
this.#mq.topic.name, this.#mq.broker.name, this.#mq.queueId, err);
|
|
158
229
|
this.onReceiveMessageException(err, attemptId);
|
|
159
|
-
})
|
|
230
|
+
})
|
|
231
|
+
.finally(() => { this.#inflightReceives--; });
|
|
160
232
|
} catch (err) {
|
|
161
|
-
|
|
233
|
+
this.#logger.error('Exception in receiveMessage, mq=%s@%s@%d, error=%s',
|
|
162
234
|
this.#mq.topic.name, this.#mq.broker.name, this.#mq.queueId, err);
|
|
163
235
|
this.onReceiveMessageException(err as Error, attemptId);
|
|
164
236
|
}
|
|
165
237
|
}
|
|
166
238
|
|
|
167
239
|
#onReceiveMessageResult(messages: MessageView[]): void {
|
|
240
|
+
// Continue processing fetched messages after drop; after abort, leave them for server redelivery.
|
|
241
|
+
if (this.#aborted) return;
|
|
168
242
|
if (messages.length > 0) {
|
|
169
243
|
this.cacheMessages(messages);
|
|
170
244
|
this.#consumer.getConsumeService().consume(this, messages);
|
|
@@ -203,35 +277,51 @@ export class ProcessQueue {
|
|
|
203
277
|
});
|
|
204
278
|
}
|
|
205
279
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
280
|
+
// Log settlement failures and retry at fixed intervals; do not send initial or retry RPCs after abort.
|
|
281
|
+
// forgiveInvalidReceiptHandle stops retrying on INVALID_RECEIPT_HANDLE (ack and
|
|
282
|
+
// change invisible duration only; Java has no such branch for DLQ forwarding).
|
|
283
|
+
async #settleWithRetry(messageView: MessageView, operation: string,
|
|
284
|
+
rpc: () => Promise<{ getStatus(): { toObject(): Status.AsObject } | undefined }>,
|
|
285
|
+
backoffDelay: number, forgiveInvalidReceiptHandle: boolean): Promise<void> {
|
|
286
|
+
let attempt = 1;
|
|
287
|
+
while (!this.#aborted) {
|
|
288
|
+
try {
|
|
289
|
+
const response = await rpc();
|
|
290
|
+
const status = response.getStatus()?.toObject();
|
|
291
|
+
if (status?.code === Code.OK) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const requestId = getRequestId(response);
|
|
295
|
+
if (forgiveInvalidReceiptHandle && status?.code === Code.INVALID_RECEIPT_HANDLE) {
|
|
296
|
+
this.#logger.error(
|
|
297
|
+
'Failed to %s due to the invalid receipt handle, forgive to retry, attempt=%d, messageId=%s, '
|
|
298
|
+
+ 'requestId=%s, status message=[%s]',
|
|
299
|
+
operation, attempt, messageView.messageId, requestId, status.message,
|
|
300
|
+
);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
this.#logger.error(
|
|
304
|
+
'Failed to %s, would retry later, attempt=%d, messageId=%s, code=%s, requestId=%s, status message=[%s]',
|
|
305
|
+
operation, attempt, messageView.messageId, status?.code, requestId, status?.message,
|
|
306
|
+
);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
this.#logger.warn(
|
|
309
|
+
'Failed to %s, attempt=%d, messageId=%s, error=%s',
|
|
310
|
+
operation, attempt, messageView.messageId, err,
|
|
311
|
+
);
|
|
220
312
|
}
|
|
221
|
-
|
|
222
|
-
(this.#
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
);
|
|
226
|
-
await this.#ackMessageLater(messageView, attempt + 1);
|
|
313
|
+
attempt++;
|
|
314
|
+
if (this.#aborted) return;
|
|
315
|
+
await new Promise<void>(resolve => setTimeout(resolve, backoffDelay));
|
|
316
|
+
if (this.#aborted) return;
|
|
227
317
|
}
|
|
228
318
|
}
|
|
229
319
|
|
|
230
|
-
async #
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
320
|
+
async #ackMessage(messageView: MessageView): Promise<void> {
|
|
321
|
+
await this.#settleWithRetry(messageView, 'ack message',
|
|
322
|
+
() => this.#consumer.ackMessageViaRpc(messageView.endpoints,
|
|
323
|
+
this.#consumer.wrapAckMessageRequest(messageView), this.#consumer.requestTimeoutValue, messageView),
|
|
324
|
+
ACK_MESSAGE_FAILURE_BACKOFF_DELAY, true);
|
|
235
325
|
}
|
|
236
326
|
|
|
237
327
|
async #nackMessage(messageView: MessageView): Promise<void> {
|
|
@@ -240,54 +330,36 @@ export class ProcessQueue {
|
|
|
240
330
|
await this.#changeInvisibleDuration(messageView, delay);
|
|
241
331
|
}
|
|
242
332
|
|
|
243
|
-
async #changeInvisibleDuration(messageView: MessageView, duration: number
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const response = await this.#consumer.changeInvisibleDurationViaRpc(
|
|
247
|
-
endpoints,
|
|
333
|
+
async #changeInvisibleDuration(messageView: MessageView, duration: number): Promise<void> {
|
|
334
|
+
await this.#settleWithRetry(messageView, 'change invisible duration',
|
|
335
|
+
() => this.#consumer.changeInvisibleDurationViaRpc(messageView.endpoints,
|
|
248
336
|
this.#consumer.wrapChangeInvisibleDurationRequest(messageView, duration),
|
|
249
|
-
this.#consumer.requestTimeoutValue,
|
|
250
|
-
);
|
|
251
|
-
const status = response.getStatus()?.toObject();
|
|
252
|
-
if (status?.code === Code.INVALID_RECEIPT_HANDLE) {
|
|
253
|
-
return; // Forgive to retry
|
|
254
|
-
}
|
|
255
|
-
if (status?.code !== Code.OK) {
|
|
256
|
-
await this.#changeInvisibleDurationLater(messageView, duration, attempt + 1);
|
|
257
|
-
}
|
|
258
|
-
} catch (err) {
|
|
259
|
-
(this.#consumer as any).logger?.warn(
|
|
260
|
-
'Failed to change invisible duration, attempt=%d, duration=%dms, messageId=%s, error=%s',
|
|
261
|
-
attempt, duration, messageView.messageId, err,
|
|
262
|
-
);
|
|
263
|
-
await this.#changeInvisibleDurationLater(messageView, duration, attempt + 1);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async #changeInvisibleDurationLater(messageView: MessageView, duration: number, attempt: number): Promise<void> {
|
|
268
|
-
if (this.#aborted) return;
|
|
269
|
-
await new Promise<void>(resolve => setTimeout(resolve, CHANGE_INVISIBLE_DURATION_FAILURE_BACKOFF_DELAY));
|
|
270
|
-
if (this.#aborted) return;
|
|
271
|
-
await this.#changeInvisibleDuration(messageView, duration, attempt);
|
|
337
|
+
this.#consumer.requestTimeoutValue, messageView),
|
|
338
|
+
CHANGE_INVISIBLE_DURATION_FAILURE_BACKOFF_DELAY, true);
|
|
272
339
|
}
|
|
273
340
|
|
|
274
341
|
async eraseFifoMessage(messageView: MessageView, consumeResult: ConsumeResult): Promise<void> {
|
|
275
342
|
if (!messageView) {
|
|
276
343
|
return;
|
|
277
344
|
}
|
|
345
|
+
if (this.#aborted) {
|
|
346
|
+
// Once aborted, skip local retries and settlement RPCs, but still release the cache permit.
|
|
347
|
+
this.#evictCache(messageView);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
278
350
|
consumeResult = this.#convertSuspendResultIfNeeded(consumeResult);
|
|
279
351
|
this.#statsConsumptionResult(consumeResult);
|
|
280
352
|
const retryPolicy = this.#consumer.getRetryPolicy();
|
|
281
353
|
const maxAttempts = retryPolicy?.getMaxAttempts() ?? 1;
|
|
282
354
|
const attempt = messageView.deliveryAttempt ?? 1;
|
|
283
355
|
const messageId = messageView.messageId;
|
|
284
|
-
const clientId =
|
|
356
|
+
const clientId = this.#clientId;
|
|
285
357
|
|
|
286
358
|
if (consumeResult === ConsumeResult.FAILURE && attempt < maxAttempts) {
|
|
287
359
|
const nextAttemptDelay = retryPolicy?.getNextAttemptDelay(attempt) ?? 0;
|
|
288
360
|
// Increment delivery attempt before redelivering
|
|
289
361
|
messageView.incrementAndGetDeliveryAttempt();
|
|
290
|
-
|
|
362
|
+
this.#logger.debug?.(
|
|
291
363
|
'Prepare to redeliver the fifo message because of the consumption failure, maxAttempts=%d, '
|
|
292
364
|
+ 'attempt=%d, mq=%s, messageId=%s, nextAttemptDelay=%d, clientId=%s',
|
|
293
365
|
maxAttempts, messageView.deliveryAttempt, this.#mq, messageId, nextAttemptDelay, clientId,
|
|
@@ -302,14 +374,14 @@ export class ProcessQueue {
|
|
|
302
374
|
await this.#ackMessage(messageView);
|
|
303
375
|
} else if (consumeResult instanceof ConsumeResultSuspend) {
|
|
304
376
|
const suspendTimeMs = consumeResult.suspendTimeMs;
|
|
305
|
-
|
|
377
|
+
this.#logger.info(
|
|
306
378
|
'Suspend consumption, consumerGroup=%s, topic=%s, liteTopic=%s, messageId=%s, result=%s',
|
|
307
379
|
this.#consumer.getConsumerGroup(), messageView.topic, messageView.liteTopic ?? '',
|
|
308
380
|
messageId, consumeResult,
|
|
309
381
|
);
|
|
310
382
|
await this.#changeInvisibleDuration(messageView, suspendTimeMs);
|
|
311
383
|
} else {
|
|
312
|
-
|
|
384
|
+
this.#logger.info(
|
|
313
385
|
'Failed to consume fifo message finally, run out of attempt times, maxAttempts=%d, attempt=%d, '
|
|
314
386
|
+ 'mq=%s, messageId=%s, clientId=%s',
|
|
315
387
|
maxAttempts, attempt, this.#mq, messageId, clientId,
|
|
@@ -319,35 +391,13 @@ export class ProcessQueue {
|
|
|
319
391
|
this.#evictCache(messageView);
|
|
320
392
|
}
|
|
321
393
|
|
|
322
|
-
async #forwardToDeadLetterQueue(messageView: MessageView
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
endpoints,
|
|
394
|
+
async #forwardToDeadLetterQueue(messageView: MessageView): Promise<void> {
|
|
395
|
+
// A missing status is not OK either and is retried, mirroring Java ProcessQueueImpl.
|
|
396
|
+
await this.#settleWithRetry(messageView, 'forward message to dead letter queue',
|
|
397
|
+
() => this.#consumer.forwardMessageToDeadLetterQueueViaRpc(messageView.endpoints,
|
|
327
398
|
this.#consumer.wrapForwardMessageToDeadLetterQueueRequest(messageView),
|
|
328
|
-
this.#consumer.requestTimeoutValue,
|
|
329
|
-
);
|
|
330
|
-
const status = response.getStatus();
|
|
331
|
-
if (!status) {
|
|
332
|
-
throw new Error('Missing status in forward to dead letter queue response');
|
|
333
|
-
}
|
|
334
|
-
const statusObj = status.toObject();
|
|
335
|
-
if (statusObj.code !== Code.OK) {
|
|
336
|
-
await this.#forwardToDeadLetterQueueLater(messageView, attempt + 1);
|
|
337
|
-
}
|
|
338
|
-
} catch (err) {
|
|
339
|
-
if ((err as Error).message === 'Missing status in forward to dead letter queue response') {
|
|
340
|
-
throw err; // Re-throw critical error
|
|
341
|
-
}
|
|
342
|
-
await this.#forwardToDeadLetterQueueLater(messageView, attempt + 1);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
async #forwardToDeadLetterQueueLater(messageView: MessageView, attempt: number): Promise<void> {
|
|
347
|
-
if (this.#aborted) return;
|
|
348
|
-
await new Promise<void>(resolve => setTimeout(resolve, FORWARD_MESSAGE_TO_DLQ_FAILURE_BACKOFF_DELAY));
|
|
349
|
-
if (this.#aborted) return;
|
|
350
|
-
await this.#forwardToDeadLetterQueue(messageView, attempt);
|
|
399
|
+
this.#consumer.requestTimeoutValue, messageView),
|
|
400
|
+
FORWARD_MESSAGE_TO_DLQ_FAILURE_BACKOFF_DELAY, false);
|
|
351
401
|
}
|
|
352
402
|
|
|
353
403
|
discardMessage(messageView: MessageView): void {
|
|
@@ -367,13 +417,16 @@ export class ProcessQueue {
|
|
|
367
417
|
if (index !== -1) {
|
|
368
418
|
this.#cachedMessages.splice(index, 1);
|
|
369
419
|
this.#cachedMessagesBytes -= messageView.body.length;
|
|
420
|
+
// Return the consumption permit held by this now-settled message, so the
|
|
421
|
+
// receive loop can fetch again (backpressure release).
|
|
422
|
+
this.#semaphore.release();
|
|
370
423
|
}
|
|
371
424
|
}
|
|
372
425
|
|
|
373
426
|
#convertSuspendResultIfNeeded(consumeResult: ConsumeResult): ConsumeResult {
|
|
374
427
|
if (consumeResult instanceof ConsumeResultSuspend) {
|
|
375
|
-
if (!
|
|
376
|
-
|
|
428
|
+
if (!this.#liteConsumer) {
|
|
429
|
+
this.#logger.warn(
|
|
377
430
|
'Only LitePushConsumer support ConsumeResultSuspend! Convert to ConsumeResult.FAILURE, '
|
|
378
431
|
+ 'consumerGroup=%s, consumerType=%s',
|
|
379
432
|
this.#consumer.getConsumerGroup(), this.#consumer.constructor.name,
|
|
@@ -400,6 +453,11 @@ export class ProcessQueue {
|
|
|
400
453
|
return this.#cachedMessagesBytes;
|
|
401
454
|
}
|
|
402
455
|
|
|
456
|
+
// Release a removed queue only after receive callbacks finish and all cached messages are settled.
|
|
457
|
+
isDrained(): boolean {
|
|
458
|
+
return this.#inflightReceives === 0 && this.#cachedMessages.length === 0;
|
|
459
|
+
}
|
|
460
|
+
|
|
403
461
|
abort(): void {
|
|
404
462
|
this.#aborted = true;
|
|
405
463
|
}
|