rocketmq-client-nodejs 1.1.0 → 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/dist/client/BaseClient.d.ts +8 -0
- package/dist/client/BaseClient.js +197 -93
- package/dist/client/Logger.js +9 -6
- package/dist/client/RpcClient.js +29 -133
- package/dist/client/RpcClientManager.d.ts +17 -17
- package/dist/client/RpcClientManager.js +38 -50
- package/dist/client/TelemetrySession.js +68 -13
- package/dist/consumer/ConsumeService.js +6 -4
- package/dist/consumer/ConsumeTask.js +5 -5
- package/dist/consumer/Consumer.js +13 -13
- package/dist/consumer/FifoConsumeService.d.ts +2 -1
- package/dist/consumer/FifoConsumeService.js +3 -3
- package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
- package/dist/consumer/LiteConsumerSupport.js +47 -0
- package/dist/consumer/LiteFifoConsumeService.d.ts +2 -1
- package/dist/consumer/LiteFifoConsumeService.js +4 -4
- package/dist/consumer/LitePushConsumerImpl.d.ts +3 -3
- package/dist/consumer/LitePushConsumerImpl.js +10 -29
- package/dist/consumer/LiteSimpleConsumerImpl.js +6 -14
- package/dist/consumer/LiteStandardConsumeService.d.ts +2 -1
- package/dist/consumer/LiteStandardConsumeService.js +3 -3
- package/dist/consumer/LiteSubscriptionManager.js +10 -9
- package/dist/consumer/OffsetOption.d.ts +1 -0
- package/dist/consumer/OffsetOption.js +10 -4
- package/dist/consumer/ProcessQueue.d.ts +7 -1
- package/dist/consumer/ProcessQueue.js +95 -90
- package/dist/consumer/PushConsumer.js +79 -27
- package/dist/consumer/PushSubscriptionSettings.js +3 -3
- package/dist/consumer/SimpleConsumer.d.ts +5 -0
- package/dist/consumer/SimpleConsumer.js +24 -20
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +2 -1
- package/dist/consumer/StandardConsumeService.js +16 -7
- package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
- package/dist/exception/InternalErrorException.d.ts +1 -0
- package/dist/exception/InternalErrorException.js +3 -2
- package/dist/exception/StatusChecker.js +4 -4
- package/dist/exception/receiveStatus.d.ts +18 -0
- package/dist/exception/receiveStatus.js +28 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +2 -1
- package/dist/hook/CompositedMessageInterceptor.js +8 -5
- package/dist/hook/MessageInterceptorContextImpl.d.ts +1 -0
- package/dist/hook/MessageInterceptorContextImpl.js +18 -4
- package/dist/message/Message.js +26 -4
- package/dist/message/MessageView.js +15 -5
- package/dist/message/PublishingMessage.js +4 -4
- package/dist/metrics/ClientMeterManager.d.ts +4 -0
- package/dist/metrics/ClientMeterManager.js +95 -19
- package/dist/producer/Producer.d.ts +8 -2
- package/dist/producer/Producer.js +66 -45
- package/dist/producer/PublishingLoadBalancer.js +3 -3
- 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/route/Endpoints.js +17 -9
- 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/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 +6 -3
- package/src/client/BaseClient.ts +211 -100
- package/src/client/Logger.ts +9 -5
- package/src/client/RpcClient.ts +48 -132
- package/src/client/RpcClientManager.ts +76 -52
- package/src/client/TelemetrySession.ts +70 -13
- package/src/consumer/ConsumeService.ts +7 -3
- package/src/consumer/ConsumeTask.ts +4 -4
- package/src/consumer/Consumer.ts +16 -15
- package/src/consumer/FifoConsumeService.ts +2 -2
- package/src/consumer/LiteConsumerSupport.ts +49 -0
- package/src/consumer/LiteFifoConsumeService.ts +3 -3
- package/src/consumer/LitePushConsumerImpl.ts +15 -22
- package/src/consumer/LiteSimpleConsumerImpl.ts +7 -14
- package/src/consumer/LiteStandardConsumeService.ts +2 -2
- package/src/consumer/LiteSubscriptionManager.ts +12 -14
- package/src/consumer/OffsetOption.ts +10 -3
- package/src/consumer/ProcessQueue.ts +123 -108
- package/src/consumer/PushConsumer.ts +83 -26
- package/src/consumer/PushSubscriptionSettings.ts +2 -2
- package/src/consumer/SimpleConsumer.ts +30 -23
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +15 -6
- package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
- package/src/exception/InternalErrorException.ts +3 -1
- package/src/exception/StatusChecker.ts +3 -2
- package/src/exception/receiveStatus.ts +25 -0
- package/src/hook/CompositedMessageInterceptor.ts +8 -4
- package/src/hook/MessageInterceptorContextImpl.ts +18 -3
- package/src/message/Message.ts +25 -3
- package/src/message/MessageView.ts +13 -4
- package/src/message/PublishingMessage.ts +3 -3
- package/src/metrics/ClientMeterManager.ts +100 -18
- package/src/producer/Producer.ts +71 -46
- package/src/producer/PublishingLoadBalancer.ts +2 -2
- 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/route/Endpoints.ts +16 -8
- package/src/route/MessageQueue.ts +6 -1
- package/src/route/TopicRouteData.ts +22 -1
- package/src/util/index.ts +8 -5
- package/src/util/rpcContext.ts +34 -0
package/src/client/RpcClient.ts
CHANGED
|
@@ -81,8 +81,20 @@ export class RpcClient {
|
|
|
81
81
|
return this.#client;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
// Shared unary-call plumbing: refresh activity, apply the deadline and settle
|
|
85
|
+
// the promise from the gRPC callback, mirroring the per-method wrappers before.
|
|
86
|
+
#unary<Req, Res>(duration: number,
|
|
87
|
+
call: (request: Req, metadata: Metadata, options: { deadline: number },
|
|
88
|
+
callback: (e: Error | null, res: Res) => void) => void,
|
|
89
|
+
request: Req, metadata: Metadata): Promise<Res> {
|
|
90
|
+
this.#activityTime = Date.now();
|
|
91
|
+
const deadline = Date.now() + duration;
|
|
92
|
+
return new Promise<Res>((resolve, reject) => {
|
|
93
|
+
call(request, metadata, { deadline }, (e, res) => {
|
|
94
|
+
if (e) return reject(e);
|
|
95
|
+
resolve(res);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
86
98
|
}
|
|
87
99
|
|
|
88
100
|
idleDuration() {
|
|
@@ -100,54 +112,25 @@ export class RpcClient {
|
|
|
100
112
|
* @param metadata gRPC request header metadata.
|
|
101
113
|
* @param duration request max duration in milliseconds.
|
|
102
114
|
*/
|
|
103
|
-
async queryRoute(request: QueryRouteRequest, metadata: Metadata, duration: number) {
|
|
104
|
-
|
|
105
|
-
const deadline = this.#getDeadline(duration);
|
|
106
|
-
return new Promise<QueryRouteResponse>((resolve, reject) => {
|
|
107
|
-
client.queryRoute(request, metadata, { deadline }, (e, res) => {
|
|
108
|
-
if (e) return reject(e);
|
|
109
|
-
resolve(res);
|
|
110
|
-
});
|
|
111
|
-
});
|
|
115
|
+
async queryRoute(request: QueryRouteRequest, metadata: Metadata, duration: number): Promise<QueryRouteResponse> {
|
|
116
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.queryRoute(req, md, opts, cb), request, metadata);
|
|
112
117
|
}
|
|
113
118
|
|
|
114
|
-
async heartbeat(request: HeartbeatRequest, metadata: Metadata, duration: number) {
|
|
115
|
-
|
|
116
|
-
const deadline = this.#getDeadline(duration);
|
|
117
|
-
return new Promise<HeartbeatResponse>((resolve, reject) => {
|
|
118
|
-
client.heartbeat(request, metadata, { deadline }, (e, res) => {
|
|
119
|
-
if (e) return reject(e);
|
|
120
|
-
resolve(res);
|
|
121
|
-
});
|
|
122
|
-
});
|
|
119
|
+
async heartbeat(request: HeartbeatRequest, metadata: Metadata, duration: number): Promise<HeartbeatResponse> {
|
|
120
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.heartbeat(req, md, opts, cb), request, metadata);
|
|
123
121
|
}
|
|
124
122
|
|
|
125
|
-
async sendMessage(request: SendMessageRequest, metadata: Metadata, duration: number) {
|
|
126
|
-
|
|
127
|
-
const deadline = this.#getDeadline(duration);
|
|
128
|
-
return new Promise<SendMessageResponse>((resolve, reject) => {
|
|
129
|
-
client.sendMessage(request, metadata, { deadline }, (e, res) => {
|
|
130
|
-
if (e) return reject(e);
|
|
131
|
-
resolve(res);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
123
|
+
async sendMessage(request: SendMessageRequest, metadata: Metadata, duration: number): Promise<SendMessageResponse> {
|
|
124
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.sendMessage(req, md, opts, cb), request, metadata);
|
|
134
125
|
}
|
|
135
126
|
|
|
136
|
-
async queryAssignment(request: QueryAssignmentRequest, metadata: Metadata, duration: number) {
|
|
137
|
-
|
|
138
|
-
const deadline = this.#getDeadline(duration);
|
|
139
|
-
return new Promise<QueryAssignmentResponse>((resolve, reject) => {
|
|
140
|
-
client.queryAssignment(request, metadata, { deadline }, (e, res) => {
|
|
141
|
-
if (e) return reject(e);
|
|
142
|
-
resolve(res);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
127
|
+
async queryAssignment(request: QueryAssignmentRequest, metadata: Metadata, duration: number): Promise<QueryAssignmentResponse> {
|
|
128
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.queryAssignment(req, md, opts, cb), request, metadata);
|
|
145
129
|
}
|
|
146
130
|
|
|
147
131
|
async receiveMessage(request: ReceiveMessageRequest, metadata: Metadata, duration: number) {
|
|
148
132
|
const client = this.#getAndActivityRpcClient();
|
|
149
|
-
const
|
|
150
|
-
const readable = client.receiveMessage(request, metadata, { deadline });
|
|
133
|
+
const readable = client.receiveMessage(request, metadata, { deadline: Date.now() + duration });
|
|
151
134
|
const responses: ReceiveMessageResponse[] = [];
|
|
152
135
|
for await (const res of readable) {
|
|
153
136
|
responses.push(res);
|
|
@@ -155,33 +138,19 @@ export class RpcClient {
|
|
|
155
138
|
return responses;
|
|
156
139
|
}
|
|
157
140
|
|
|
158
|
-
async ackMessage(request: AckMessageRequest, metadata: Metadata, duration: number) {
|
|
159
|
-
|
|
160
|
-
const deadline = this.#getDeadline(duration);
|
|
161
|
-
return new Promise<AckMessageResponse>((resolve, reject) => {
|
|
162
|
-
client.ackMessage(request, metadata, { deadline }, (e, res) => {
|
|
163
|
-
if (e) return reject(e);
|
|
164
|
-
resolve(res);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
141
|
+
async ackMessage(request: AckMessageRequest, metadata: Metadata, duration: number): Promise<AckMessageResponse> {
|
|
142
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.ackMessage(req, md, opts, cb), request, metadata);
|
|
167
143
|
}
|
|
168
144
|
|
|
169
145
|
async forwardMessageToDeadLetterQueue(request: ForwardMessageToDeadLetterQueueRequest,
|
|
170
|
-
metadata: Metadata, duration: number) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return new Promise<ForwardMessageToDeadLetterQueueResponse>((resolve, reject) => {
|
|
174
|
-
client.forwardMessageToDeadLetterQueue(request, metadata, { deadline }, (e, res) => {
|
|
175
|
-
if (e) return reject(e);
|
|
176
|
-
resolve(res);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
146
|
+
metadata: Metadata, duration: number): Promise<ForwardMessageToDeadLetterQueueResponse> {
|
|
147
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.forwardMessageToDeadLetterQueue(req, md, opts, cb),
|
|
148
|
+
request, metadata);
|
|
179
149
|
}
|
|
180
150
|
|
|
181
151
|
async pullMessage(request: PullMessageRequest, metadata: Metadata, duration: number) {
|
|
182
152
|
const client = this.#getAndActivityRpcClient();
|
|
183
|
-
const
|
|
184
|
-
const readable = client.pullMessage(request, metadata, { deadline });
|
|
153
|
+
const readable = client.pullMessage(request, metadata, { deadline: Date.now() + duration });
|
|
185
154
|
const responses: PullMessageResponse[] = [];
|
|
186
155
|
for await (const res of readable) {
|
|
187
156
|
responses.push(res);
|
|
@@ -189,48 +158,20 @@ export class RpcClient {
|
|
|
189
158
|
return responses;
|
|
190
159
|
}
|
|
191
160
|
|
|
192
|
-
async updateOffset(request: UpdateOffsetRequest, metadata: Metadata, duration: number) {
|
|
193
|
-
|
|
194
|
-
const deadline = this.#getDeadline(duration);
|
|
195
|
-
return new Promise<UpdateOffsetResponse>((resolve, reject) => {
|
|
196
|
-
client.updateOffset(request, metadata, { deadline }, (e, res) => {
|
|
197
|
-
if (e) return reject(e);
|
|
198
|
-
resolve(res);
|
|
199
|
-
});
|
|
200
|
-
});
|
|
161
|
+
async updateOffset(request: UpdateOffsetRequest, metadata: Metadata, duration: number): Promise<UpdateOffsetResponse> {
|
|
162
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.updateOffset(req, md, opts, cb), request, metadata);
|
|
201
163
|
}
|
|
202
164
|
|
|
203
|
-
async getOffset(request: GetOffsetRequest, metadata: Metadata, duration: number) {
|
|
204
|
-
|
|
205
|
-
const deadline = this.#getDeadline(duration);
|
|
206
|
-
return new Promise<GetOffsetResponse>((resolve, reject) => {
|
|
207
|
-
client.getOffset(request, metadata, { deadline }, (e, res) => {
|
|
208
|
-
if (e) return reject(e);
|
|
209
|
-
resolve(res);
|
|
210
|
-
});
|
|
211
|
-
});
|
|
165
|
+
async getOffset(request: GetOffsetRequest, metadata: Metadata, duration: number): Promise<GetOffsetResponse> {
|
|
166
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.getOffset(req, md, opts, cb), request, metadata);
|
|
212
167
|
}
|
|
213
168
|
|
|
214
|
-
async queryOffset(request: QueryOffsetRequest, metadata: Metadata, duration: number) {
|
|
215
|
-
|
|
216
|
-
const deadline = this.#getDeadline(duration);
|
|
217
|
-
return new Promise<QueryOffsetResponse>((resolve, reject) => {
|
|
218
|
-
client.queryOffset(request, metadata, { deadline }, (e, res) => {
|
|
219
|
-
if (e) return reject(e);
|
|
220
|
-
resolve(res);
|
|
221
|
-
});
|
|
222
|
-
});
|
|
169
|
+
async queryOffset(request: QueryOffsetRequest, metadata: Metadata, duration: number): Promise<QueryOffsetResponse> {
|
|
170
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.queryOffset(req, md, opts, cb), request, metadata);
|
|
223
171
|
}
|
|
224
172
|
|
|
225
|
-
async endTransaction(request: EndTransactionRequest, metadata: Metadata, duration: number) {
|
|
226
|
-
|
|
227
|
-
const deadline = this.#getDeadline(duration);
|
|
228
|
-
return new Promise<EndTransactionResponse>((resolve, reject) => {
|
|
229
|
-
client.endTransaction(request, metadata, { deadline }, (e, res) => {
|
|
230
|
-
if (e) return reject(e);
|
|
231
|
-
resolve(res);
|
|
232
|
-
});
|
|
233
|
-
});
|
|
173
|
+
async endTransaction(request: EndTransactionRequest, metadata: Metadata, duration: number): Promise<EndTransactionResponse> {
|
|
174
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.endTransaction(req, md, opts, cb), request, metadata);
|
|
234
175
|
}
|
|
235
176
|
|
|
236
177
|
telemetry(metadata: Metadata) {
|
|
@@ -238,47 +179,22 @@ export class RpcClient {
|
|
|
238
179
|
return client.telemetry(metadata);
|
|
239
180
|
}
|
|
240
181
|
|
|
241
|
-
async notifyClientTermination(request: NotifyClientTerminationRequest, metadata: Metadata, duration: number) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return new Promise<NotifyClientTerminationResponse>((resolve, reject) => {
|
|
245
|
-
client.notifyClientTermination(request, metadata, { deadline }, (e, res) => {
|
|
246
|
-
if (e) return reject(e);
|
|
247
|
-
resolve(res);
|
|
248
|
-
});
|
|
249
|
-
});
|
|
182
|
+
async notifyClientTermination(request: NotifyClientTerminationRequest, metadata: Metadata, duration: number): Promise<NotifyClientTerminationResponse> {
|
|
183
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.notifyClientTermination(req, md, opts, cb),
|
|
184
|
+
request, metadata);
|
|
250
185
|
}
|
|
251
186
|
|
|
252
|
-
async changeInvisibleDuration(request: ChangeInvisibleDurationRequest, metadata: Metadata, duration: number) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return new Promise<ChangeInvisibleDurationResponse>((resolve, reject) => {
|
|
256
|
-
client.changeInvisibleDuration(request, metadata, { deadline }, (e, res) => {
|
|
257
|
-
if (e) return reject(e);
|
|
258
|
-
resolve(res);
|
|
259
|
-
});
|
|
260
|
-
});
|
|
187
|
+
async changeInvisibleDuration(request: ChangeInvisibleDurationRequest, metadata: Metadata, duration: number): Promise<ChangeInvisibleDurationResponse> {
|
|
188
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.changeInvisibleDuration(req, md, opts, cb),
|
|
189
|
+
request, metadata);
|
|
261
190
|
}
|
|
262
191
|
|
|
263
|
-
async recallMessage(request: RecallMessageRequest, metadata: Metadata, duration: number) {
|
|
264
|
-
|
|
265
|
-
const deadline = this.#getDeadline(duration);
|
|
266
|
-
return new Promise<RecallMessageResponse>((resolve, reject) => {
|
|
267
|
-
client.recallMessage(request, metadata, { deadline }, (e, res) => {
|
|
268
|
-
if (e) return reject(e);
|
|
269
|
-
resolve(res);
|
|
270
|
-
});
|
|
271
|
-
});
|
|
192
|
+
async recallMessage(request: RecallMessageRequest, metadata: Metadata, duration: number): Promise<RecallMessageResponse> {
|
|
193
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.recallMessage(req, md, opts, cb), request, metadata);
|
|
272
194
|
}
|
|
273
195
|
|
|
274
|
-
async syncLiteSubscription(request: SyncLiteSubscriptionRequest, metadata: Metadata, duration: number) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return new Promise<SyncLiteSubscriptionResponse>((resolve, reject) => {
|
|
278
|
-
client.syncLiteSubscription(request, metadata, { deadline }, (e, res) => {
|
|
279
|
-
if (e) return reject(e);
|
|
280
|
-
resolve(res);
|
|
281
|
-
});
|
|
282
|
-
});
|
|
196
|
+
async syncLiteSubscription(request: SyncLiteSubscriptionRequest, metadata: Metadata, duration: number): Promise<SyncLiteSubscriptionResponse> {
|
|
197
|
+
return this.#unary(duration, (req, md, opts, cb) => this.#client.syncLiteSubscription(req, md, opts, cb),
|
|
198
|
+
request, metadata);
|
|
283
199
|
}
|
|
284
200
|
}
|
|
@@ -18,17 +18,40 @@
|
|
|
18
18
|
import { Metadata } from '@grpc/grpc-js';
|
|
19
19
|
import {
|
|
20
20
|
AckMessageRequest,
|
|
21
|
+
AckMessageResponse,
|
|
21
22
|
ChangeInvisibleDurationRequest,
|
|
23
|
+
ChangeInvisibleDurationResponse,
|
|
22
24
|
EndTransactionRequest,
|
|
25
|
+
EndTransactionResponse,
|
|
23
26
|
ForwardMessageToDeadLetterQueueRequest,
|
|
27
|
+
ForwardMessageToDeadLetterQueueResponse,
|
|
24
28
|
GetOffsetRequest,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
GetOffsetResponse,
|
|
30
|
+
HeartbeatRequest,
|
|
31
|
+
HeartbeatResponse,
|
|
32
|
+
NotifyClientTerminationRequest,
|
|
33
|
+
NotifyClientTerminationResponse,
|
|
34
|
+
PullMessageRequest,
|
|
35
|
+
PullMessageResponse,
|
|
36
|
+
QueryAssignmentRequest,
|
|
37
|
+
QueryAssignmentResponse,
|
|
38
|
+
QueryOffsetRequest,
|
|
39
|
+
QueryOffsetResponse,
|
|
40
|
+
QueryRouteRequest,
|
|
41
|
+
QueryRouteResponse,
|
|
42
|
+
ReceiveMessageRequest,
|
|
43
|
+
ReceiveMessageResponse,
|
|
44
|
+
SendMessageRequest,
|
|
45
|
+
SendMessageResponse,
|
|
46
|
+
UpdateOffsetRequest,
|
|
47
|
+
UpdateOffsetResponse,
|
|
28
48
|
RecallMessageRequest,
|
|
49
|
+
RecallMessageResponse,
|
|
29
50
|
SyncLiteSubscriptionRequest,
|
|
51
|
+
SyncLiteSubscriptionResponse,
|
|
30
52
|
} from '../../proto/apache/rocketmq/v2/service_pb';
|
|
31
53
|
import { Endpoints } from '../route';
|
|
54
|
+
import { withRequestId } from '../util/rpcContext';
|
|
32
55
|
import { ILogger } from './Logger';
|
|
33
56
|
import { RpcClient } from './RpcClient';
|
|
34
57
|
import type { BaseClient } from './BaseClient';
|
|
@@ -44,6 +67,10 @@ export class RpcClientManager {
|
|
|
44
67
|
#baseClient: BaseClient;
|
|
45
68
|
#logger: ILogger;
|
|
46
69
|
#clearIdleRpcClientsTimer: NodeJS.Timeout;
|
|
70
|
+
// Terminal state set by close(): afterwards no RPC client may be recreated,
|
|
71
|
+
// otherwise residual callbacks (e.g. a receive loop that escaped shutdown)
|
|
72
|
+
// would lazily rebuild gRPC channels and keep hitting the remote.
|
|
73
|
+
#closed = false;
|
|
47
74
|
|
|
48
75
|
constructor(baseClient: BaseClient, logger: ILogger) {
|
|
49
76
|
this.#baseClient = baseClient;
|
|
@@ -55,6 +82,9 @@ export class RpcClientManager {
|
|
|
55
82
|
this.#clearIdleRpcClientsTimer = setInterval(() => {
|
|
56
83
|
this.#clearIdleRpcClients();
|
|
57
84
|
}, RPC_CLIENT_IDLE_CHECK_PERIOD);
|
|
85
|
+
// Housekeeping only: a client that is constructed but never started must
|
|
86
|
+
// not keep the process alive.
|
|
87
|
+
this.#clearIdleRpcClientsTimer.unref();
|
|
58
88
|
}
|
|
59
89
|
|
|
60
90
|
#clearIdleRpcClients() {
|
|
@@ -63,13 +93,16 @@ export class RpcClientManager {
|
|
|
63
93
|
if (idleDuration > RPC_CLIENT_MAX_IDLE_DURATION) {
|
|
64
94
|
rpcClient.close();
|
|
65
95
|
this.#rpcClients.delete(facade);
|
|
66
|
-
this.#logger.info('[RpcClientManager] Rpc client has been idle for a long time, endpoints=%s, idleDuration=%
|
|
96
|
+
this.#logger.info('[RpcClientManager] Rpc client has been idle for a long time, endpoints=%s, idleDuration=%dms, maxIdleDuration=%dms, clientId=%s',
|
|
67
97
|
facade, idleDuration, RPC_CLIENT_MAX_IDLE_DURATION, this.#baseClient.clientId);
|
|
68
98
|
}
|
|
69
99
|
}
|
|
70
100
|
}
|
|
71
101
|
|
|
72
102
|
#getRpcClient(endpoints: Endpoints) {
|
|
103
|
+
if (this.#closed) {
|
|
104
|
+
throw new Error(`RpcClientManager is already closed, will not re-establish connection, endpoints=${endpoints.facade}, clientId=${this.#baseClient.clientId}`);
|
|
105
|
+
}
|
|
73
106
|
const facade = endpoints.facade;
|
|
74
107
|
let rpcClient = this.#rpcClients.get(facade);
|
|
75
108
|
if (!rpcClient) {
|
|
@@ -95,6 +128,9 @@ export class RpcClientManager {
|
|
|
95
128
|
}
|
|
96
129
|
|
|
97
130
|
close() {
|
|
131
|
+
// Mark closed first so no RPC client can be lazily recreated afterwards
|
|
132
|
+
this.#closed = true;
|
|
133
|
+
|
|
98
134
|
// Clear idle check timer first
|
|
99
135
|
if (this.#clearIdleRpcClientsTimer) {
|
|
100
136
|
clearInterval(this.#clearIdleRpcClientsTimer);
|
|
@@ -112,105 +148,93 @@ export class RpcClientManager {
|
|
|
112
148
|
this.#rpcClients.clear();
|
|
113
149
|
}
|
|
114
150
|
|
|
115
|
-
|
|
151
|
+
// Shared plumbing for unary calls: resolve the channel, build request metadata
|
|
152
|
+
// and associate the response with its request id.
|
|
153
|
+
async #call<T extends object>(endpoints: Endpoints,
|
|
154
|
+
invoke: (rpcClient: RpcClient, metadata: Metadata) => Promise<T>): Promise<T> {
|
|
116
155
|
const rpcClient = this.#getRpcClient(endpoints);
|
|
117
156
|
const metadata = this.#baseClient.getRequestMetadata();
|
|
118
|
-
return await rpcClient
|
|
157
|
+
return withRequestId(await invoke(rpcClient, metadata), metadata);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async queryRoute(endpoints: Endpoints, request: QueryRouteRequest, duration: number) {
|
|
161
|
+
return this.#call<QueryRouteResponse>(endpoints, (client, metadata) => client.queryRoute(request, metadata, duration));
|
|
119
162
|
}
|
|
120
163
|
|
|
121
164
|
async heartbeat(endpoints: Endpoints, request: HeartbeatRequest, duration: number) {
|
|
122
|
-
|
|
123
|
-
const metadata = this.#baseClient.getRequestMetadata();
|
|
124
|
-
return await rpcClient.heartbeat(request, metadata, duration);
|
|
165
|
+
return this.#call<HeartbeatResponse>(endpoints, (client, metadata) => client.heartbeat(request, metadata, duration));
|
|
125
166
|
}
|
|
126
167
|
|
|
127
168
|
async sendMessage(endpoints: Endpoints, request: SendMessageRequest, duration: number) {
|
|
128
|
-
|
|
129
|
-
const metadata = this.#baseClient.getRequestMetadata();
|
|
130
|
-
return await rpcClient.sendMessage(request, metadata, duration);
|
|
169
|
+
return this.#call<SendMessageResponse>(endpoints, (client, metadata) => client.sendMessage(request, metadata, duration));
|
|
131
170
|
}
|
|
132
171
|
|
|
133
172
|
async queryAssignment(endpoints: Endpoints, request: QueryAssignmentRequest, duration: number) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return await rpcClient.queryAssignment(request, metadata, duration);
|
|
173
|
+
return this.#call<QueryAssignmentResponse>(endpoints,
|
|
174
|
+
(client, metadata) => client.queryAssignment(request, metadata, duration));
|
|
137
175
|
}
|
|
138
176
|
|
|
139
177
|
async receiveMessage(endpoints: Endpoints, request: ReceiveMessageRequest, duration: number) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return await rpcClient.receiveMessage(request, metadata, duration);
|
|
178
|
+
return this.#call<ReceiveMessageResponse[]>(endpoints,
|
|
179
|
+
(client, metadata) => client.receiveMessage(request, metadata, duration));
|
|
143
180
|
}
|
|
144
181
|
|
|
145
182
|
async ackMessage(endpoints: Endpoints, request: AckMessageRequest, duration: number) {
|
|
146
|
-
|
|
147
|
-
const metadata = this.#baseClient.getRequestMetadata();
|
|
148
|
-
return await rpcClient.ackMessage(request, metadata, duration);
|
|
183
|
+
return this.#call<AckMessageResponse>(endpoints, (client, metadata) => client.ackMessage(request, metadata, duration));
|
|
149
184
|
}
|
|
150
185
|
|
|
151
186
|
async forwardMessageToDeadLetterQueue(endpoints: Endpoints, request: ForwardMessageToDeadLetterQueueRequest,
|
|
152
187
|
duration: number) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return await rpcClient.forwardMessageToDeadLetterQueue(request, metadata, duration);
|
|
188
|
+
return this.#call<ForwardMessageToDeadLetterQueueResponse>(endpoints,
|
|
189
|
+
(client, metadata) => client.forwardMessageToDeadLetterQueue(request, metadata, duration));
|
|
156
190
|
}
|
|
157
191
|
|
|
158
192
|
async pullMessage(endpoints: Endpoints, request: PullMessageRequest, duration: number) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return await rpcClient.pullMessage(request, metadata, duration);
|
|
193
|
+
return this.#call<PullMessageResponse[]>(endpoints,
|
|
194
|
+
(client, metadata) => client.pullMessage(request, metadata, duration));
|
|
162
195
|
}
|
|
163
196
|
|
|
164
197
|
async updateOffset(endpoints: Endpoints, request: UpdateOffsetRequest, duration: number) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return await rpcClient.updateOffset(request, metadata, duration);
|
|
198
|
+
return this.#call<UpdateOffsetResponse>(endpoints,
|
|
199
|
+
(client, metadata) => client.updateOffset(request, metadata, duration));
|
|
168
200
|
}
|
|
169
201
|
|
|
170
202
|
async getOffset(endpoints: Endpoints, request: GetOffsetRequest, duration: number) {
|
|
171
|
-
|
|
172
|
-
const metadata = this.#baseClient.getRequestMetadata();
|
|
173
|
-
return await rpcClient.getOffset(request, metadata, duration);
|
|
203
|
+
return this.#call<GetOffsetResponse>(endpoints, (client, metadata) => client.getOffset(request, metadata, duration));
|
|
174
204
|
}
|
|
175
205
|
|
|
176
206
|
async queryOffset(endpoints: Endpoints, request: QueryOffsetRequest, duration: number) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return await rpcClient.queryOffset(request, metadata, duration);
|
|
207
|
+
return this.#call<QueryOffsetResponse>(endpoints,
|
|
208
|
+
(client, metadata) => client.queryOffset(request, metadata, duration));
|
|
180
209
|
}
|
|
181
210
|
|
|
182
211
|
async endTransaction(endpoints: Endpoints, request: EndTransactionRequest, duration: number) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return await rpcClient.endTransaction(request, metadata, duration);
|
|
212
|
+
return this.#call<EndTransactionResponse>(endpoints,
|
|
213
|
+
(client, metadata) => client.endTransaction(request, metadata, duration));
|
|
186
214
|
}
|
|
187
215
|
|
|
188
216
|
telemetry(endpoints: Endpoints, metadata: Metadata) {
|
|
189
217
|
const rpcClient = this.#getRpcClient(endpoints);
|
|
190
|
-
return rpcClient.telemetry(metadata);
|
|
218
|
+
return withRequestId(rpcClient.telemetry(metadata), metadata);
|
|
191
219
|
}
|
|
192
220
|
|
|
193
221
|
async changeInvisibleDuration(endpoints: Endpoints, request: ChangeInvisibleDurationRequest, duration: number) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return await rpcClient.changeInvisibleDuration(request, metadata, duration);
|
|
222
|
+
return this.#call<ChangeInvisibleDurationResponse>(endpoints,
|
|
223
|
+
(client, metadata) => client.changeInvisibleDuration(request, metadata, duration));
|
|
197
224
|
}
|
|
198
225
|
|
|
199
226
|
async notifyClientTermination(endpoints: Endpoints, request: NotifyClientTerminationRequest, duration: number) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return await rpcClient.notifyClientTermination(request, metadata, duration);
|
|
227
|
+
return this.#call<NotifyClientTerminationResponse>(endpoints,
|
|
228
|
+
(client, metadata) => client.notifyClientTermination(request, metadata, duration));
|
|
203
229
|
}
|
|
204
230
|
|
|
205
231
|
async recallMessage(endpoints: Endpoints, request: RecallMessageRequest, duration: number) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return await rpcClient.recallMessage(request, metadata, duration);
|
|
232
|
+
return this.#call<RecallMessageResponse>(endpoints,
|
|
233
|
+
(client, metadata) => client.recallMessage(request, metadata, duration));
|
|
209
234
|
}
|
|
210
235
|
|
|
211
236
|
async syncLiteSubscription(endpoints: Endpoints, request: SyncLiteSubscriptionRequest, duration: number) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return await rpcClient.syncLiteSubscription(request, metadata, duration);
|
|
237
|
+
return this.#call<SyncLiteSubscriptionResponse>(endpoints,
|
|
238
|
+
(client, metadata) => client.syncLiteSubscription(request, metadata, duration));
|
|
215
239
|
}
|
|
216
240
|
}
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
import { ClientDuplexStream } from '@grpc/grpc-js';
|
|
19
19
|
import { TelemetryCommand } from '../../proto/apache/rocketmq/v2/service_pb';
|
|
20
20
|
import { Endpoints } from '../route';
|
|
21
|
+
import { getRequestId, setRequestId } from '../util/rpcContext';
|
|
21
22
|
import type { BaseClient } from './BaseClient';
|
|
22
23
|
import { ILogger } from './Logger';
|
|
23
24
|
|
|
@@ -25,10 +26,14 @@ export class TelemetrySession {
|
|
|
25
26
|
#endpoints: Endpoints;
|
|
26
27
|
#baseClient: BaseClient;
|
|
27
28
|
#logger: ILogger;
|
|
28
|
-
|
|
29
|
+
// Live stream, or undefined while closed (released or awaiting reconnect).
|
|
30
|
+
#stream?: ClientDuplexStream<TelemetryCommand, TelemetryCommand>;
|
|
29
31
|
#isRefreshing = false;
|
|
30
32
|
#released = false;
|
|
31
33
|
#reconnectTimer?: NodeJS.Timeout;
|
|
34
|
+
// Consecutive failed reconnect attempts, driving the exponential backoff
|
|
35
|
+
// delay; reset once the stream is proven alive by inbound data.
|
|
36
|
+
#reconnectAttempts = 0;
|
|
32
37
|
|
|
33
38
|
constructor(baseClient: BaseClient, endpoints: Endpoints, logger: ILogger) {
|
|
34
39
|
this.#endpoints = endpoints;
|
|
@@ -54,9 +59,17 @@ export class TelemetrySession {
|
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
#closeStream() {
|
|
62
|
+
const stream = this.#stream;
|
|
63
|
+
if (!stream) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this.#stream = undefined;
|
|
57
67
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
stream.removeAllListeners();
|
|
69
|
+
// The remote may still finish the call with a non-OK status after the
|
|
70
|
+
// half-close; an 'error' event without any listener would crash the process.
|
|
71
|
+
stream.on('error', () => undefined);
|
|
72
|
+
stream.end();
|
|
60
73
|
} catch (e) {
|
|
61
74
|
this.#logger.warn('Failed to release telemetry session gracefully, endpoints=%s, clientId=%s, error=%s',
|
|
62
75
|
this.#endpoints, this.#baseClient.clientId, e instanceof Error ? e.message : String(e));
|
|
@@ -64,7 +77,19 @@ export class TelemetrySession {
|
|
|
64
77
|
}
|
|
65
78
|
|
|
66
79
|
write(command: TelemetryCommand) {
|
|
67
|
-
this.#stream
|
|
80
|
+
const stream = this.#stream;
|
|
81
|
+
if (!stream) {
|
|
82
|
+
// Released, or waiting for reconnect: the renewed stream syncs settings itself.
|
|
83
|
+
this.#logger.warn('Telemetry stream is not available, drop the command, endpoints=%s, clientId=%s, released=%s',
|
|
84
|
+
this.#endpoints, this.#baseClient.clientId, this.#released);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
stream.write(command);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
this.#logger.error('Failed to write telemetry command, endpoints=%s, clientId=%s, error=%s',
|
|
91
|
+
this.#endpoints, this.#baseClient.clientId, e instanceof Error ? e.message : String(e));
|
|
92
|
+
}
|
|
68
93
|
}
|
|
69
94
|
|
|
70
95
|
syncSettings() {
|
|
@@ -93,6 +118,9 @@ export class TelemetrySession {
|
|
|
93
118
|
} catch (err) {
|
|
94
119
|
this.#logger.error('Failed to refresh telemetry session, endpoints=%s, clientId=%s, error=%s',
|
|
95
120
|
this.#endpoints, this.#baseClient.clientId, err);
|
|
121
|
+
// The stream is gone and the renewal failed; keep retrying in the
|
|
122
|
+
// background instead of leaving the session permanently dead.
|
|
123
|
+
this.#scheduleRenewStream();
|
|
96
124
|
} finally {
|
|
97
125
|
this.#isRefreshing = false;
|
|
98
126
|
}
|
|
@@ -102,10 +130,11 @@ export class TelemetrySession {
|
|
|
102
130
|
try {
|
|
103
131
|
this.#logger.debug?.('Creating telemetry stream, endpoints=%s, clientId=%s, inited=%s',
|
|
104
132
|
this.#endpoints, this.#baseClient.clientId, inited);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
133
|
+
const stream = this.#baseClient.createTelemetryStream(this.#endpoints);
|
|
134
|
+
stream.on('data', this.#onData.bind(this));
|
|
135
|
+
stream.once('error', this.#onError.bind(this));
|
|
136
|
+
stream.once('end', this.#onEnd.bind(this));
|
|
137
|
+
this.#stream = stream;
|
|
109
138
|
if (!inited) {
|
|
110
139
|
this.#logger.info('Syncing settings to new stream, endpoints=%s, clientId=%s',
|
|
111
140
|
this.#endpoints, this.#baseClient.clientId);
|
|
@@ -119,6 +148,19 @@ export class TelemetrySession {
|
|
|
119
148
|
}
|
|
120
149
|
|
|
121
150
|
#onData(command: TelemetryCommand) {
|
|
151
|
+
// Inbound data proves the stream is alive: reset the reconnect backoff.
|
|
152
|
+
this.#reconnectAttempts = 0;
|
|
153
|
+
try {
|
|
154
|
+
this.#dispatch(command);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
// Mirrors Java ClientSessionImpl#onNext: a failing handler must not
|
|
157
|
+
// escape the stream callback as an uncaughtException.
|
|
158
|
+
this.#logger.error('[Bug] Unexpected exception raised while receiving command from remote, endpoints=%s, clientId=%s, error=%s',
|
|
159
|
+
this.#endpoints, this.#baseClient.clientId, e instanceof Error ? e.message : String(e));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#dispatch(command: TelemetryCommand) {
|
|
122
164
|
const endpoints = this.#endpoints;
|
|
123
165
|
const clientId = this.#baseClient.clientId;
|
|
124
166
|
const commandCase = command.getCommandCase();
|
|
@@ -163,27 +205,42 @@ export class TelemetrySession {
|
|
|
163
205
|
this.#logger.warn('Receive unrecognized command from remote, endpoints=%s, commandCase=%j, command=%j, clientId=%s',
|
|
164
206
|
endpoints, commandCase, commandObj, clientId);
|
|
165
207
|
// should telemetry session start fail
|
|
208
|
+
if (commandObj.status) setRequestId(commandObj.status, getRequestId(this.#stream));
|
|
166
209
|
this.#baseClient.onUnknownCommand(endpoints, commandObj.status!);
|
|
167
210
|
}
|
|
168
211
|
}
|
|
169
212
|
}
|
|
170
213
|
|
|
171
214
|
/**
|
|
172
|
-
* Schedule a telemetry stream renewal
|
|
173
|
-
*
|
|
174
|
-
*
|
|
215
|
+
* Schedule a telemetry stream renewal with exponential backoff (1s, 2s,
|
|
216
|
+
* 4s, ... capped at 60s). The timer is tracked so that it can be cancelled
|
|
217
|
+
* on release(), and a pending timer doubles as a guard to prevent
|
|
218
|
+
* error/end events from scheduling duplicate reconnects.
|
|
175
219
|
*/
|
|
176
220
|
#scheduleRenewStream() {
|
|
177
221
|
if (this.#released || this.#reconnectTimer) {
|
|
178
222
|
return;
|
|
179
223
|
}
|
|
224
|
+
const delay = Math.min(1000 * 2 ** this.#reconnectAttempts, 60000);
|
|
225
|
+
this.#reconnectAttempts++;
|
|
180
226
|
this.#reconnectTimer = setTimeout(() => {
|
|
181
227
|
this.#reconnectTimer = undefined;
|
|
182
228
|
if (this.#released) {
|
|
183
229
|
return;
|
|
184
230
|
}
|
|
185
|
-
|
|
186
|
-
|
|
231
|
+
try {
|
|
232
|
+
this.#renewStream(false);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
// Never rethrow here: an exception escaping a timer callback becomes
|
|
235
|
+
// an uncaughtException, and without this catch a single transient
|
|
236
|
+
// stream-creation failure would permanently end the reconnect loop
|
|
237
|
+
// and the session would never be able to re-register settings.
|
|
238
|
+
this.#logger.error('Failed to renew telemetry stream, will retry, endpoints=%s, clientId=%s, attempt=%d, error=%s',
|
|
239
|
+
this.#endpoints, this.#baseClient.clientId, this.#reconnectAttempts,
|
|
240
|
+
err instanceof Error ? err.message : String(err));
|
|
241
|
+
this.#scheduleRenewStream();
|
|
242
|
+
}
|
|
243
|
+
}, delay);
|
|
187
244
|
}
|
|
188
245
|
|
|
189
246
|
#onError(err: Error) {
|