rocketmq-client-nodejs-bate 1.0.12

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 (209) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +186 -0
  3. package/dist/client/BaseClient.d.ts +95 -0
  4. package/dist/client/BaseClient.js +324 -0
  5. package/dist/client/ClientId.d.ts +24 -0
  6. package/dist/client/ClientId.js +33 -0
  7. package/dist/client/Logger.d.ts +25 -0
  8. package/dist/client/Logger.js +33 -0
  9. package/dist/client/RpcClient.d.ts +48 -0
  10. package/dist/client/RpcClient.js +219 -0
  11. package/dist/client/RpcClientManager.d.ts +42 -0
  12. package/dist/client/RpcClientManager.js +144 -0
  13. package/dist/client/SessionCredentials.d.ts +21 -0
  14. package/dist/client/SessionCredentials.js +19 -0
  15. package/dist/client/Settings.d.ts +31 -0
  16. package/dist/client/Settings.js +40 -0
  17. package/dist/client/TelemetrySession.d.ts +27 -0
  18. package/dist/client/TelemetrySession.js +110 -0
  19. package/dist/client/UserAgent.d.ts +25 -0
  20. package/dist/client/UserAgent.js +47 -0
  21. package/dist/client/index.d.ts +26 -0
  22. package/dist/client/index.js +43 -0
  23. package/dist/consumer/Assignment.d.ts +23 -0
  24. package/dist/consumer/Assignment.js +40 -0
  25. package/dist/consumer/Assignments.d.ts +24 -0
  26. package/dist/consumer/Assignments.js +47 -0
  27. package/dist/consumer/ConsumeResult.d.ts +20 -0
  28. package/dist/consumer/ConsumeResult.js +25 -0
  29. package/dist/consumer/ConsumeService.d.ts +28 -0
  30. package/dist/consumer/ConsumeService.js +49 -0
  31. package/dist/consumer/ConsumeTask.d.ts +24 -0
  32. package/dist/consumer/ConsumeTask.js +40 -0
  33. package/dist/consumer/Consumer.d.ts +38 -0
  34. package/dist/consumer/Consumer.js +111 -0
  35. package/dist/consumer/FifoConsumeService.d.ts +25 -0
  36. package/dist/consumer/FifoConsumeService.js +123 -0
  37. package/dist/consumer/FilterExpression.d.ts +25 -0
  38. package/dist/consumer/FilterExpression.js +40 -0
  39. package/dist/consumer/MessageListener.d.ts +21 -0
  40. package/dist/consumer/MessageListener.js +19 -0
  41. package/dist/consumer/ProcessQueue.d.ts +39 -0
  42. package/dist/consumer/ProcessQueue.js +286 -0
  43. package/dist/consumer/PushConsumer.d.ts +68 -0
  44. package/dist/consumer/PushConsumer.js +377 -0
  45. package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
  46. package/dist/consumer/PushSubscriptionSettings.js +97 -0
  47. package/dist/consumer/SimpleConsumer.d.ts +50 -0
  48. package/dist/consumer/SimpleConsumer.js +122 -0
  49. package/dist/consumer/SimpleSubscriptionSettings.d.ts +28 -0
  50. package/dist/consumer/SimpleSubscriptionSettings.js +57 -0
  51. package/dist/consumer/StandardConsumeService.d.ts +24 -0
  52. package/dist/consumer/StandardConsumeService.js +42 -0
  53. package/dist/consumer/SubscriptionLoadBalancer.d.ts +23 -0
  54. package/dist/consumer/SubscriptionLoadBalancer.js +46 -0
  55. package/dist/consumer/index.d.ts +32 -0
  56. package/dist/consumer/index.js +49 -0
  57. package/dist/exception/BadRequestException.d.ts +20 -0
  58. package/dist/exception/BadRequestException.js +28 -0
  59. package/dist/exception/ClientException.d.ts +20 -0
  60. package/dist/exception/ClientException.js +31 -0
  61. package/dist/exception/ForbiddenException.d.ts +20 -0
  62. package/dist/exception/ForbiddenException.js +28 -0
  63. package/dist/exception/InternalErrorException.d.ts +20 -0
  64. package/dist/exception/InternalErrorException.js +28 -0
  65. package/dist/exception/NotFoundException.d.ts +20 -0
  66. package/dist/exception/NotFoundException.js +28 -0
  67. package/dist/exception/PayloadTooLargeException.d.ts +20 -0
  68. package/dist/exception/PayloadTooLargeException.js +28 -0
  69. package/dist/exception/PaymentRequiredException.d.ts +20 -0
  70. package/dist/exception/PaymentRequiredException.js +28 -0
  71. package/dist/exception/ProxyTimeoutException.d.ts +20 -0
  72. package/dist/exception/ProxyTimeoutException.js +28 -0
  73. package/dist/exception/RequestHeaderFieldsTooLargeException.d.ts +20 -0
  74. package/dist/exception/RequestHeaderFieldsTooLargeException.js +28 -0
  75. package/dist/exception/StatusChecker.d.ts +20 -0
  76. package/dist/exception/StatusChecker.js +98 -0
  77. package/dist/exception/TooManyRequestsException.d.ts +20 -0
  78. package/dist/exception/TooManyRequestsException.js +28 -0
  79. package/dist/exception/UnauthorizedException.d.ts +20 -0
  80. package/dist/exception/UnauthorizedException.js +28 -0
  81. package/dist/exception/UnsupportedException.d.ts +20 -0
  82. package/dist/exception/UnsupportedException.js +28 -0
  83. package/dist/exception/index.d.ts +29 -0
  84. package/dist/exception/index.js +46 -0
  85. package/dist/index.d.ts +22 -0
  86. package/dist/index.js +39 -0
  87. package/dist/message/Message.d.ts +38 -0
  88. package/dist/message/Message.js +57 -0
  89. package/dist/message/MessageId.d.ts +59 -0
  90. package/dist/message/MessageId.js +123 -0
  91. package/dist/message/MessageView.d.ts +38 -0
  92. package/dist/message/MessageView.js +90 -0
  93. package/dist/message/PublishingMessage.d.ts +30 -0
  94. package/dist/message/PublishingMessage.js +100 -0
  95. package/dist/message/index.d.ts +20 -0
  96. package/dist/message/index.js +37 -0
  97. package/dist/producer/Producer.d.ts +55 -0
  98. package/dist/producer/Producer.js +318 -0
  99. package/dist/producer/PublishingLoadBalancer.d.ts +24 -0
  100. package/dist/producer/PublishingLoadBalancer.js +82 -0
  101. package/dist/producer/PublishingSettings.d.ts +28 -0
  102. package/dist/producer/PublishingSettings.js +70 -0
  103. package/dist/producer/RecallReceipt.d.ts +25 -0
  104. package/dist/producer/RecallReceipt.js +34 -0
  105. package/dist/producer/SendReceipt.d.ts +29 -0
  106. package/dist/producer/SendReceipt.js +60 -0
  107. package/dist/producer/Transaction.d.ts +28 -0
  108. package/dist/producer/Transaction.js +70 -0
  109. package/dist/producer/TransactionChecker.d.ts +21 -0
  110. package/dist/producer/TransactionChecker.js +19 -0
  111. package/dist/producer/index.d.ts +22 -0
  112. package/dist/producer/index.js +40 -0
  113. package/dist/retry/ExponentialBackoffRetryPolicy.d.ts +27 -0
  114. package/dist/retry/ExponentialBackoffRetryPolicy.js +64 -0
  115. package/dist/retry/RetryPolicy.d.ts +46 -0
  116. package/dist/retry/RetryPolicy.js +19 -0
  117. package/dist/retry/index.d.ts +18 -0
  118. package/dist/retry/index.js +35 -0
  119. package/dist/route/Broker.d.ts +25 -0
  120. package/dist/route/Broker.js +40 -0
  121. package/dist/route/Endpoints.d.ts +30 -0
  122. package/dist/route/Endpoints.js +71 -0
  123. package/dist/route/MessageQueue.d.ts +27 -0
  124. package/dist/route/MessageQueue.js +47 -0
  125. package/dist/route/TopicRouteData.d.ts +24 -0
  126. package/dist/route/TopicRouteData.js +37 -0
  127. package/dist/route/index.d.ts +20 -0
  128. package/dist/route/index.js +37 -0
  129. package/dist/util/index.d.ts +35 -0
  130. package/dist/util/index.js +88 -0
  131. package/package.json +62 -0
  132. package/proto/apache/rocketmq/v2/admin.proto +43 -0
  133. package/proto/apache/rocketmq/v2/admin_grpc_pb.d.ts +41 -0
  134. package/proto/apache/rocketmq/v2/admin_grpc_pb.js +60 -0
  135. package/proto/apache/rocketmq/v2/admin_pb.d.ts +56 -0
  136. package/proto/apache/rocketmq/v2/admin_pb.js +340 -0
  137. package/proto/apache/rocketmq/v2/definition.proto +570 -0
  138. package/proto/apache/rocketmq/v2/definition_grpc_pb.js +1 -0
  139. package/proto/apache/rocketmq/v2/definition_pb.d.ts +885 -0
  140. package/proto/apache/rocketmq/v2/definition_pb.js +6141 -0
  141. package/proto/apache/rocketmq/v2/service.proto +443 -0
  142. package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +294 -0
  143. package/proto/apache/rocketmq/v2/service_grpc_pb.js +637 -0
  144. package/proto/apache/rocketmq/v2/service_pb.d.ts +1249 -0
  145. package/proto/apache/rocketmq/v2/service_pb.js +9723 -0
  146. package/src/client/BaseClient.ts +404 -0
  147. package/src/client/ClientId.ts +31 -0
  148. package/src/client/Logger.ts +36 -0
  149. package/src/client/RpcClient.ts +258 -0
  150. package/src/client/RpcClientManager.ts +180 -0
  151. package/src/client/SessionCredentials.ts +22 -0
  152. package/src/client/Settings.ts +46 -0
  153. package/src/client/TelemetrySession.ts +130 -0
  154. package/src/client/UserAgent.ts +45 -0
  155. package/src/client/index.ts +27 -0
  156. package/src/consumer/Assignment.ts +39 -0
  157. package/src/consumer/Assignments.ts +46 -0
  158. package/src/consumer/ConsumeResult.ts +21 -0
  159. package/src/consumer/ConsumeService.ts +54 -0
  160. package/src/consumer/ConsumeTask.ts +40 -0
  161. package/src/consumer/Consumer.ts +129 -0
  162. package/src/consumer/FifoConsumeService.ts +136 -0
  163. package/src/consumer/FilterExpression.ts +42 -0
  164. package/src/consumer/MessageListener.ts +23 -0
  165. package/src/consumer/ProcessQueue.ts +326 -0
  166. package/src/consumer/PushConsumer.ts +473 -0
  167. package/src/consumer/PushSubscriptionSettings.ts +123 -0
  168. package/src/consumer/SimpleConsumer.ts +153 -0
  169. package/src/consumer/SimpleSubscriptionSettings.ts +64 -0
  170. package/src/consumer/StandardConsumeService.ts +44 -0
  171. package/src/consumer/SubscriptionLoadBalancer.ts +47 -0
  172. package/src/consumer/index.ts +33 -0
  173. package/src/exception/BadRequestException.ts +25 -0
  174. package/src/exception/ClientException.ts +29 -0
  175. package/src/exception/ForbiddenException.ts +25 -0
  176. package/src/exception/InternalErrorException.ts +25 -0
  177. package/src/exception/NotFoundException.ts +25 -0
  178. package/src/exception/PayloadTooLargeException.ts +25 -0
  179. package/src/exception/PaymentRequiredException.ts +25 -0
  180. package/src/exception/ProxyTimeoutException.ts +25 -0
  181. package/src/exception/RequestHeaderFieldsTooLargeException.ts +25 -0
  182. package/src/exception/StatusChecker.ts +94 -0
  183. package/src/exception/TooManyRequestsException.ts +25 -0
  184. package/src/exception/UnauthorizedException.ts +25 -0
  185. package/src/exception/UnsupportedException.ts +25 -0
  186. package/src/exception/index.ts +30 -0
  187. package/src/index.ts +23 -0
  188. package/src/message/Message.ts +67 -0
  189. package/src/message/MessageId.ts +123 -0
  190. package/src/message/MessageView.ts +94 -0
  191. package/src/message/PublishingMessage.ts +104 -0
  192. package/src/message/index.ts +21 -0
  193. package/src/producer/Producer.ts +388 -0
  194. package/src/producer/PublishingLoadBalancer.ts +85 -0
  195. package/src/producer/PublishingSettings.ts +78 -0
  196. package/src/producer/RecallReceipt.ts +32 -0
  197. package/src/producer/SendReceipt.ts +63 -0
  198. package/src/producer/Transaction.ts +86 -0
  199. package/src/producer/TransactionChecker.ts +23 -0
  200. package/src/producer/index.ts +24 -0
  201. package/src/retry/ExponentialBackoffRetryPolicy.ts +76 -0
  202. package/src/retry/RetryPolicy.ts +51 -0
  203. package/src/retry/index.ts +19 -0
  204. package/src/route/Broker.ts +39 -0
  205. package/src/route/Endpoints.ts +70 -0
  206. package/src/route/MessageQueue.ts +49 -0
  207. package/src/route/TopicRouteData.ts +38 -0
  208. package/src/route/index.ts +21 -0
  209. package/src/util/index.ts +83 -0
@@ -0,0 +1,326 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { randomUUID } from 'node:crypto';
19
+ import { Code } from '../../proto/apache/rocketmq/v2/definition_pb';
20
+ import { MessageView } from '../message';
21
+ import { MessageQueue } from '../route';
22
+ import { TooManyRequestsException } from '../exception';
23
+ import { ConsumeResult } from './ConsumeResult';
24
+ import { FilterExpression } from './FilterExpression';
25
+ import type { PushConsumer } from './PushConsumer';
26
+
27
+ const ACK_MESSAGE_FAILURE_BACKOFF_DELAY = 1000;
28
+ const CHANGE_INVISIBLE_DURATION_FAILURE_BACKOFF_DELAY = 1000;
29
+ const FORWARD_MESSAGE_TO_DLQ_FAILURE_BACKOFF_DELAY = 1000;
30
+
31
+ const RECEIVING_FLOW_CONTROL_BACKOFF_DELAY = 20;
32
+ const RECEIVING_FAILURE_BACKOFF_DELAY = 1000;
33
+ const RECEIVING_BACKOFF_DELAY_WHEN_CACHE_IS_FULL = 1000;
34
+
35
+ export class ProcessQueue {
36
+ readonly #consumer: PushConsumer;
37
+ #dropped = false;
38
+ readonly #mq: MessageQueue;
39
+ readonly #filterExpression: FilterExpression;
40
+ readonly #cachedMessages: MessageView[] = [];
41
+ #cachedMessagesBytes = 0;
42
+ #activityTime = Date.now();
43
+ #cacheFullTime = 0;
44
+ #aborted = false;
45
+
46
+ constructor(consumer: PushConsumer, mq: MessageQueue, filterExpression: FilterExpression) {
47
+ this.#consumer = consumer;
48
+ this.#mq = mq;
49
+ this.#filterExpression = filterExpression;
50
+ }
51
+
52
+ getMessageQueue(): MessageQueue {
53
+ return this.#mq;
54
+ }
55
+
56
+ drop(): void {
57
+ this.#dropped = true;
58
+ }
59
+
60
+ expired(): boolean {
61
+ const longPollingTimeout = this.#consumer.getPushConsumerSettings().getLongPollingTimeout();
62
+ const requestTimeout = this.#consumer.requestTimeoutValue;
63
+ const maxIdleDuration = (longPollingTimeout + requestTimeout) * 3;
64
+ const idleDuration = Date.now() - this.#activityTime;
65
+ if (idleDuration < maxIdleDuration) {
66
+ return false;
67
+ }
68
+ const afterCacheFullDuration = Date.now() - this.#cacheFullTime;
69
+ if (afterCacheFullDuration < maxIdleDuration) {
70
+ return false;
71
+ }
72
+ return true;
73
+ }
74
+
75
+ cacheMessages(messageList: MessageView[]): void {
76
+ for (const messageView of messageList) {
77
+ this.#cachedMessages.push(messageView);
78
+ this.#cachedMessagesBytes += messageView.body.length;
79
+ }
80
+ }
81
+
82
+ #getReceptionBatchSize(): number {
83
+ const bufferSize = Math.max(
84
+ this.#consumer.cacheMessageCountThresholdPerQueue() - this.cachedMessagesCount(),
85
+ 1,
86
+ );
87
+ return Math.min(bufferSize, this.#consumer.getPushConsumerSettings().getReceiveBatchSize());
88
+ }
89
+
90
+ fetchMessageImmediately(): void {
91
+ this.#receiveMessageImmediately();
92
+ }
93
+
94
+ onReceiveMessageException(t: Error, attemptId?: string): void {
95
+ const delay = t instanceof TooManyRequestsException
96
+ ? RECEIVING_FLOW_CONTROL_BACKOFF_DELAY
97
+ : RECEIVING_FAILURE_BACKOFF_DELAY;
98
+ this.#receiveMessageLater(delay, attemptId);
99
+ }
100
+
101
+ #receiveMessageLater(delay: number, attemptId?: string): void {
102
+ if (this.#aborted) return;
103
+ setTimeout(() => {
104
+ if (this.#aborted) return;
105
+ this.#receiveMessage(attemptId);
106
+ }, delay);
107
+ }
108
+
109
+ #generateAttemptId(): string {
110
+ return randomUUID();
111
+ }
112
+
113
+ receiveMessage(attemptId?: string): void {
114
+ this.#receiveMessage(attemptId);
115
+ }
116
+
117
+ #receiveMessage(attemptId?: string): void {
118
+ if (this.#dropped) {
119
+ return;
120
+ }
121
+ if (this.#isCacheFull()) {
122
+ this.#receiveMessageLater(RECEIVING_BACKOFF_DELAY_WHEN_CACHE_IS_FULL, attemptId);
123
+ return;
124
+ }
125
+ this.#receiveMessageImmediately(attemptId);
126
+ }
127
+
128
+ #receiveMessageImmediately(attemptId?: string): void {
129
+ if (this.#aborted) return;
130
+ attemptId = attemptId ?? this.#generateAttemptId();
131
+ try {
132
+ const batchSize = this.#getReceptionBatchSize();
133
+ const longPollingTimeout = this.#consumer.getPushConsumerSettings().getLongPollingTimeout();
134
+ const request = this.#consumer.wrapPushReceiveMessageRequest(
135
+ batchSize, this.#mq, this.#filterExpression, longPollingTimeout, attemptId,
136
+ );
137
+ this.#activityTime = Date.now();
138
+
139
+ this.#consumer.receiveMessage(request, this.#mq, longPollingTimeout)
140
+ .then(messages => {
141
+ this.#onReceiveMessageResult(messages);
142
+ })
143
+ .catch(err => {
144
+ this.onReceiveMessageException(err, attemptId);
145
+ });
146
+ } catch (err) {
147
+ this.onReceiveMessageException(err as Error, attemptId);
148
+ }
149
+ }
150
+
151
+ #onReceiveMessageResult(messages: MessageView[]): void {
152
+ if (messages.length > 0) {
153
+ this.cacheMessages(messages);
154
+ this.#consumer.getConsumeService().consume(this, messages);
155
+ }
156
+ this.#receiveMessage();
157
+ }
158
+
159
+ #isCacheFull(): boolean {
160
+ const cacheMessageCountThreshold = this.#consumer.cacheMessageCountThresholdPerQueue();
161
+ const actualCount = this.cachedMessagesCount();
162
+ if (cacheMessageCountThreshold <= actualCount) {
163
+ this.#cacheFullTime = Date.now();
164
+ return true;
165
+ }
166
+
167
+ const cacheMessageBytesThreshold = this.#consumer.cacheMessageBytesThresholdPerQueue();
168
+ if (cacheMessageBytesThreshold <= this.#cachedMessagesBytes) {
169
+ this.#cacheFullTime = Date.now();
170
+ return true;
171
+ }
172
+
173
+ return false;
174
+ }
175
+
176
+ eraseMessage(messageView: MessageView, consumeResult: ConsumeResult): void {
177
+ const task = consumeResult === ConsumeResult.SUCCESS
178
+ ? this.#ackMessage(messageView)
179
+ : this.#nackMessage(messageView);
180
+ task.finally(() => {
181
+ this.#evictCache(messageView);
182
+ });
183
+ }
184
+
185
+ async #ackMessage(messageView: MessageView, attempt = 1): Promise<void> {
186
+ try {
187
+ const endpoints = messageView.endpoints;
188
+ const response = await this.#consumer.ackMessageViaRpc(
189
+ endpoints,
190
+ this.#consumer.wrapAckMessageRequest(messageView),
191
+ this.#consumer.requestTimeoutValue,
192
+ );
193
+ const status = response.getStatus()?.toObject();
194
+ if (status?.code === Code.INVALID_RECEIPT_HANDLE) {
195
+ return; // Forgive to retry
196
+ }
197
+ if (status?.code !== Code.OK) {
198
+ await this.#ackMessageLater(messageView, attempt + 1);
199
+ }
200
+ } catch {
201
+ await this.#ackMessageLater(messageView, attempt + 1);
202
+ }
203
+ }
204
+
205
+ async #ackMessageLater(messageView: MessageView, attempt: number): Promise<void> {
206
+ if (this.#aborted) return;
207
+ await new Promise<void>(resolve => setTimeout(resolve, ACK_MESSAGE_FAILURE_BACKOFF_DELAY));
208
+ if (this.#aborted) return;
209
+ await this.#ackMessage(messageView, attempt);
210
+ }
211
+
212
+ async #nackMessage(messageView: MessageView): Promise<void> {
213
+ const retryPolicy = this.#consumer.getRetryPolicy();
214
+ const delay = retryPolicy?.getNextAttemptDelay(messageView.deliveryAttempt ?? 1) ?? 0;
215
+ await this.#changeInvisibleDuration(messageView, delay);
216
+ }
217
+
218
+ async #changeInvisibleDuration(messageView: MessageView, duration: number, attempt = 1): Promise<void> {
219
+ try {
220
+ const endpoints = messageView.endpoints;
221
+ const response = await this.#consumer.changeInvisibleDurationViaRpc(
222
+ endpoints,
223
+ this.#consumer.wrapChangeInvisibleDurationRequest(messageView, duration),
224
+ this.#consumer.requestTimeoutValue,
225
+ );
226
+ const status = response.getStatus()?.toObject();
227
+ if (status?.code === Code.INVALID_RECEIPT_HANDLE) {
228
+ return; // Forgive to retry
229
+ }
230
+ if (status?.code !== Code.OK) {
231
+ await this.#changeInvisibleDurationLater(messageView, duration, attempt + 1);
232
+ }
233
+ } catch {
234
+ await this.#changeInvisibleDurationLater(messageView, duration, attempt + 1);
235
+ }
236
+ }
237
+
238
+ async #changeInvisibleDurationLater(messageView: MessageView, duration: number, attempt: number): Promise<void> {
239
+ if (this.#aborted) return;
240
+ await new Promise<void>(resolve => setTimeout(resolve, CHANGE_INVISIBLE_DURATION_FAILURE_BACKOFF_DELAY));
241
+ if (this.#aborted) return;
242
+ await this.#changeInvisibleDuration(messageView, duration, attempt);
243
+ }
244
+
245
+ async eraseFifoMessage(messageView: MessageView, consumeResult: ConsumeResult): Promise<void> {
246
+ const retryPolicy = this.#consumer.getRetryPolicy();
247
+ const maxAttempts = retryPolicy?.getMaxAttempts() ?? 1;
248
+ let attempt = messageView.deliveryAttempt ?? 1;
249
+
250
+ if (consumeResult === ConsumeResult.FAILURE && attempt < maxAttempts) {
251
+ const nextAttemptDelay = retryPolicy?.getNextAttemptDelay(attempt) ?? 0;
252
+ // Redeliver the fifo message
253
+ const result = await this.#consumer.getConsumeService().consumeMessage(messageView, nextAttemptDelay);
254
+ await this.eraseFifoMessage(messageView, result);
255
+ } else {
256
+ const task = consumeResult === ConsumeResult.SUCCESS
257
+ ? this.#ackMessage(messageView)
258
+ : this.#forwardToDeadLetterQueue(messageView);
259
+ await task;
260
+ this.#evictCache(messageView);
261
+ }
262
+ }
263
+
264
+ async #forwardToDeadLetterQueue(messageView: MessageView, attempt = 1): Promise<void> {
265
+ try {
266
+ const endpoints = messageView.endpoints;
267
+ const response = await this.#consumer.forwardMessageToDeadLetterQueueViaRpc(
268
+ endpoints,
269
+ this.#consumer.wrapForwardMessageToDeadLetterQueueRequest(messageView),
270
+ this.#consumer.requestTimeoutValue,
271
+ );
272
+ const status = response.getStatus();
273
+ if (!status) {
274
+ throw new Error('Missing status in forward to dead letter queue response');
275
+ }
276
+ const statusObj = status.toObject();
277
+ if (statusObj.code !== Code.OK) {
278
+ await this.#forwardToDeadLetterQueueLater(messageView, attempt + 1);
279
+ }
280
+ } catch (err) {
281
+ if ((err as Error).message === 'Missing status in forward to dead letter queue response') {
282
+ throw err; // Re-throw critical error
283
+ }
284
+ await this.#forwardToDeadLetterQueueLater(messageView, attempt + 1);
285
+ }
286
+ }
287
+
288
+ async #forwardToDeadLetterQueueLater(messageView: MessageView, attempt: number): Promise<void> {
289
+ if (this.#aborted) return;
290
+ await new Promise<void>(resolve => setTimeout(resolve, FORWARD_MESSAGE_TO_DLQ_FAILURE_BACKOFF_DELAY));
291
+ if (this.#aborted) return;
292
+ await this.#forwardToDeadLetterQueue(messageView, attempt);
293
+ }
294
+
295
+ discardMessage(messageView: MessageView): void {
296
+ this.#nackMessage(messageView).finally(() => {
297
+ this.#evictCache(messageView);
298
+ });
299
+ }
300
+
301
+ discardFifoMessage(messageView: MessageView): void {
302
+ this.#forwardToDeadLetterQueue(messageView).finally(() => {
303
+ this.#evictCache(messageView);
304
+ });
305
+ }
306
+
307
+ #evictCache(messageView: MessageView): void {
308
+ const index = this.#cachedMessages.indexOf(messageView);
309
+ if (index !== -1) {
310
+ this.#cachedMessages.splice(index, 1);
311
+ this.#cachedMessagesBytes -= messageView.body.length;
312
+ }
313
+ }
314
+
315
+ cachedMessagesCount(): number {
316
+ return this.#cachedMessages.length;
317
+ }
318
+
319
+ cachedMessageBytes(): number {
320
+ return this.#cachedMessagesBytes;
321
+ }
322
+
323
+ abort(): void {
324
+ this.#aborted = true;
325
+ }
326
+ }