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
|
@@ -21,13 +21,15 @@ import { ConsumeTask } from './ConsumeTask';
|
|
|
21
21
|
import { MessageListener } from './MessageListener';
|
|
22
22
|
import type { ProcessQueue } from './ProcessQueue';
|
|
23
23
|
|
|
24
|
+
type ResolveConsumeResult = (result: ConsumeResult) => void;
|
|
25
|
+
|
|
24
26
|
export abstract class ConsumeService {
|
|
25
27
|
protected readonly clientId: string;
|
|
26
28
|
readonly #messageListener: MessageListener;
|
|
27
29
|
readonly #messageInterceptor?: MessageInterceptor;
|
|
28
30
|
readonly #consumerGroup?: string;
|
|
29
31
|
#aborted = false;
|
|
30
|
-
#pendingTimers
|
|
32
|
+
#pendingTimers = new Map<NodeJS.Timeout, ResolveConsumeResult>();
|
|
31
33
|
|
|
32
34
|
constructor(clientId: string, messageListener: MessageListener,
|
|
33
35
|
messageInterceptor?: MessageInterceptor, consumerGroup?: string) {
|
|
@@ -58,7 +60,7 @@ export abstract class ConsumeService {
|
|
|
58
60
|
const result = await this.#executeTask(task);
|
|
59
61
|
resolve(result);
|
|
60
62
|
}, delay);
|
|
61
|
-
this.#pendingTimers.
|
|
63
|
+
this.#pendingTimers.set(timer, resolve);
|
|
62
64
|
});
|
|
63
65
|
}
|
|
64
66
|
|
|
@@ -71,8 +73,10 @@ export abstract class ConsumeService {
|
|
|
71
73
|
}
|
|
72
74
|
abort() {
|
|
73
75
|
this.#aborted = true;
|
|
74
|
-
for (const timer of this.#pendingTimers) {
|
|
76
|
+
for (const [ timer, resolve ] of this.#pendingTimers) {
|
|
75
77
|
clearTimeout(timer);
|
|
78
|
+
// Settle the waiter as well, otherwise the caller would hang forever.
|
|
79
|
+
resolve(ConsumeResult.FAILURE);
|
|
76
80
|
}
|
|
77
81
|
this.#pendingTimers.clear();
|
|
78
82
|
}
|
|
@@ -28,10 +28,10 @@ import { ConsumeResult } from './ConsumeResult';
|
|
|
28
28
|
import { MessageListener } from './MessageListener';
|
|
29
29
|
|
|
30
30
|
// Context keys exposed to interceptors, mirroring the Java ConsumeTask keys.
|
|
31
|
-
export const REMOTE_ADDR_CONTEXT_KEY = AttributeKey.create<string>('
|
|
32
|
-
export const MESSAGE_VIEW_CONTEXT_KEY = AttributeKey.create<MessageView>('
|
|
33
|
-
export const CONSUMER_GROUP_CONTEXT_KEY = AttributeKey.create<string>('
|
|
34
|
-
export const CONSUME_ERROR_CONTEXT_KEY = AttributeKey.create<unknown>('
|
|
31
|
+
export const REMOTE_ADDR_CONTEXT_KEY = AttributeKey.create<string>('remoteAddr');
|
|
32
|
+
export const MESSAGE_VIEW_CONTEXT_KEY = AttributeKey.create<MessageView>('messageView');
|
|
33
|
+
export const CONSUMER_GROUP_CONTEXT_KEY = AttributeKey.create<string>('consumerGroup');
|
|
34
|
+
export const CONSUME_ERROR_CONTEXT_KEY = AttributeKey.create<unknown>('consumeError');
|
|
35
35
|
|
|
36
36
|
export class ConsumeTask {
|
|
37
37
|
readonly #messageListener: MessageListener;
|
package/src/consumer/Consumer.ts
CHANGED
|
@@ -25,6 +25,8 @@ import { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
|
25
25
|
import { MessageView } from '../message';
|
|
26
26
|
import { MessageQueue } from '../route';
|
|
27
27
|
import { StatusChecker } from '../exception';
|
|
28
|
+
import { checkReceiveStatus } from '../exception/receiveStatus';
|
|
29
|
+
import { getRequestId } from '../util/rpcContext';
|
|
28
30
|
import { BaseClient, BaseClientOptions } from '../client';
|
|
29
31
|
import { createDuration, createResource } from '../util';
|
|
30
32
|
import {
|
|
@@ -50,7 +52,7 @@ export abstract class Consumer extends BaseClient {
|
|
|
50
52
|
protected wrapReceiveMessageRequest(batchSize: number, mq: MessageQueue,
|
|
51
53
|
filterExpression: FilterExpression, invisibleDuration: number, longPollingTimeout: number) {
|
|
52
54
|
return new ReceiveMessageRequest()
|
|
53
|
-
.setGroup(createResource(this.consumerGroup))
|
|
55
|
+
.setGroup(createResource(this.consumerGroup, this.namespace))
|
|
54
56
|
.setMessageQueue(mq.toProtobuf())
|
|
55
57
|
.setFilterExpression(filterExpression.toProtobuf())
|
|
56
58
|
.setLongPollingTimeout(createDuration(longPollingTimeout))
|
|
@@ -72,7 +74,7 @@ export abstract class Consumer extends BaseClient {
|
|
|
72
74
|
|
|
73
75
|
try {
|
|
74
76
|
this.logger.debug?.('Receiving messages from broker, topic=%s, endpoints=%s, batchSize=%d, clientId=%s',
|
|
75
|
-
request.getMessageQueue()?.getTopic()?.getName(), endpoints, request.getBatchSize(),
|
|
77
|
+
request.getMessageQueue()?.getTopic()?.getName(), endpoints, request.getBatchSize(), this.clientId);
|
|
76
78
|
|
|
77
79
|
const responses = await this.rpcClientManager.receiveMessage(endpoints, request, timeout);
|
|
78
80
|
const messageList: Message[] = [];
|
|
@@ -90,16 +92,16 @@ export abstract class Consumer extends BaseClient {
|
|
|
90
92
|
transportDeliveryTimestamp = response.getDeliveryTimestamp()?.toDate();
|
|
91
93
|
break;
|
|
92
94
|
default:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
+
this.logger.warn('Not recognized content for receive message response, mq=%s, clientId=%s, response=%j',
|
|
96
|
+
mq, this.clientId, response.toObject());
|
|
95
97
|
}
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
|
|
100
|
+
checkReceiveStatus(status, getRequestId(responses));
|
|
99
101
|
|
|
100
102
|
if (messageList.length > 0) {
|
|
101
103
|
this.logger.debug?.('Received %d messages successfully, topic=%s, endpoints=%s, clientId=%s',
|
|
102
|
-
messageList.length, request.getMessageQueue()?.getTopic()?.getName(), endpoints,
|
|
104
|
+
messageList.length, request.getMessageQueue()?.getTopic()?.getName(), endpoints, this.clientId);
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
const messages = messageList.map(message => new MessageView(message, mq, transportDeliveryTimestamp));
|
|
@@ -107,7 +109,7 @@ export abstract class Consumer extends BaseClient {
|
|
|
107
109
|
return messages;
|
|
108
110
|
} catch (err) {
|
|
109
111
|
this.logger.error('Failed to receive messages, topic=%s, endpoints=%s, clientId=%s, error=%s',
|
|
110
|
-
request.getMessageQueue()?.getTopic()?.getName(), endpoints,
|
|
112
|
+
request.getMessageQueue()?.getTopic()?.getName(), endpoints, this.clientId, err);
|
|
111
113
|
this.doAfter(MessageInterceptorContextImpl.withStatus(context, MessageHookPointsStatus.ERROR), []);
|
|
112
114
|
throw err;
|
|
113
115
|
}
|
|
@@ -116,8 +118,8 @@ export abstract class Consumer extends BaseClient {
|
|
|
116
118
|
protected async ackMessage(messageView: MessageView) {
|
|
117
119
|
const endpoints = messageView.endpoints;
|
|
118
120
|
const request = new AckMessageRequest()
|
|
119
|
-
.setGroup(createResource(this.consumerGroup))
|
|
120
|
-
.setTopic(createResource(messageView.topic));
|
|
121
|
+
.setGroup(createResource(this.consumerGroup, this.namespace))
|
|
122
|
+
.setTopic(createResource(messageView.topic, this.namespace));
|
|
121
123
|
const entry = request.addEntries()
|
|
122
124
|
.setMessageId(messageView.messageId)
|
|
123
125
|
.setReceiptHandle(messageView.receiptHandle);
|
|
@@ -131,10 +133,9 @@ export abstract class Consumer extends BaseClient {
|
|
|
131
133
|
let hookStatus = MessageHookPointsStatus.OK;
|
|
132
134
|
try {
|
|
133
135
|
const res = await this.rpcClientManager.ackMessage(endpoints, request, this.requestTimeout);
|
|
134
|
-
// FIXME: handle fail ack
|
|
135
136
|
const response = res.toObject();
|
|
136
137
|
hookStatus = response.status?.code === Code.OK ? MessageHookPointsStatus.OK : MessageHookPointsStatus.ERROR;
|
|
137
|
-
StatusChecker.check(response.status);
|
|
138
|
+
StatusChecker.check(response.status, getRequestId(res));
|
|
138
139
|
this.doAfter(MessageInterceptorContextImpl.withStatus(context, hookStatus), generalMessages);
|
|
139
140
|
return response.entriesList;
|
|
140
141
|
} catch (err) {
|
|
@@ -145,8 +146,8 @@ export abstract class Consumer extends BaseClient {
|
|
|
145
146
|
|
|
146
147
|
protected async invisibleDuration(messageView: MessageView, invisibleDuration: number) {
|
|
147
148
|
const request = new ChangeInvisibleDurationRequest()
|
|
148
|
-
.setGroup(createResource(this.consumerGroup))
|
|
149
|
-
.setTopic(createResource(messageView.topic))
|
|
149
|
+
.setGroup(createResource(this.consumerGroup, this.namespace))
|
|
150
|
+
.setTopic(createResource(messageView.topic, this.namespace))
|
|
150
151
|
.setReceiptHandle(messageView.receiptHandle)
|
|
151
152
|
.setInvisibleDuration(createDuration(invisibleDuration))
|
|
152
153
|
.setMessageId(messageView.messageId);
|
|
@@ -163,7 +164,7 @@ export abstract class Consumer extends BaseClient {
|
|
|
163
164
|
const res = await this.rpcClientManager.changeInvisibleDuration(messageView.endpoints, request, this.requestTimeout);
|
|
164
165
|
const response = res.toObject();
|
|
165
166
|
hookStatus = response.status?.code === Code.OK ? MessageHookPointsStatus.OK : MessageHookPointsStatus.ERROR;
|
|
166
|
-
StatusChecker.check(response.status);
|
|
167
|
+
StatusChecker.check(response.status, getRequestId(res));
|
|
167
168
|
this.doAfter(MessageInterceptorContextImpl.withStatus(context, hookStatus), generalMessages);
|
|
168
169
|
return response.receiptHandle;
|
|
169
170
|
} catch (err) {
|
|
@@ -241,7 +242,7 @@ export abstract class Consumer extends BaseClient {
|
|
|
241
242
|
* @return true if this is a LITE_PUSH_CONSUMER or LITE_SIMPLE_CONSUMER
|
|
242
243
|
*/
|
|
243
244
|
protected isLiteConsumer(): boolean {
|
|
244
|
-
const clientType =
|
|
245
|
+
const clientType = this.getClientType();
|
|
245
246
|
return clientType === ClientType.LITE_PUSH_CONSUMER || clientType === ClientType.LITE_SIMPLE_CONSUMER;
|
|
246
247
|
}
|
|
247
248
|
}
|
|
@@ -27,10 +27,10 @@ export class FifoConsumeService extends ConsumeService {
|
|
|
27
27
|
readonly #logger: ILogger;
|
|
28
28
|
|
|
29
29
|
constructor(clientId: string, messageListener: MessageListener, enableFifoConsumeAccelerator?: boolean,
|
|
30
|
-
messageInterceptor?: MessageInterceptor, consumerGroup?: string) {
|
|
30
|
+
messageInterceptor?: MessageInterceptor, consumerGroup?: string, logger?: ILogger) {
|
|
31
31
|
super(clientId, messageListener, messageInterceptor, consumerGroup);
|
|
32
32
|
this.#enableFifoConsumeAccelerator = enableFifoConsumeAccelerator ?? false;
|
|
33
|
-
this.#logger = getDefaultLogger();
|
|
33
|
+
this.#logger = logger ?? getDefaultLogger();
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
consume(pq: ProcessQueue, messageViews: MessageView[]): void {
|
|
@@ -0,0 +1,49 @@
|
|
|
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 { LiteSubscriptionManager } from './LiteSubscriptionManager';
|
|
19
|
+
import { OffsetOption } from './OffsetOption';
|
|
20
|
+
|
|
21
|
+
// Both Lite consumers share lifecycle ordering and entry validation; callers supply parent operations.
|
|
22
|
+
// Keep this helper out of consumer/index.ts exports and preserve existing Impl and manager entry points.
|
|
23
|
+
export class LiteConsumerSupport extends LiteSubscriptionManager {
|
|
24
|
+
async startConsumer(startParent: () => Promise<void>): Promise<void> {
|
|
25
|
+
await startParent();
|
|
26
|
+
this.startUp();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async shutdownConsumer(shutdownParent: () => Promise<void>): Promise<void> {
|
|
30
|
+
this.shutdown();
|
|
31
|
+
await shutdownParent();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async subscribeLite(liteTopic: string, offsetOption?: OffsetOption | null): Promise<void> {
|
|
35
|
+
this.validateArgument(liteTopic);
|
|
36
|
+
await super.subscribeLite(liteTopic, offsetOption ?? null);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
40
|
+
this.validateArgument(liteTopic);
|
|
41
|
+
await super.unsubscribeLite(liteTopic);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private validateArgument(liteTopic: string): void {
|
|
45
|
+
if (!liteTopic || liteTopic.trim().length === 0) {
|
|
46
|
+
throw new Error('liteTopic should not be blank');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -39,9 +39,9 @@ export class LiteFifoConsumeService extends FifoConsumeService {
|
|
|
39
39
|
readonly #logger: ILogger;
|
|
40
40
|
|
|
41
41
|
constructor(clientId: string, messageListener: MessageListener, enableFifoConsumeAccelerator?: boolean,
|
|
42
|
-
messageInterceptor?: MessageInterceptor, consumerGroup?: string) {
|
|
43
|
-
super(clientId, messageListener, enableFifoConsumeAccelerator, messageInterceptor, consumerGroup);
|
|
44
|
-
this.#logger = getDefaultLogger();
|
|
42
|
+
messageInterceptor?: MessageInterceptor, consumerGroup?: string, logger?: ILogger) {
|
|
43
|
+
super(clientId, messageListener, enableFifoConsumeAccelerator, messageInterceptor, consumerGroup, logger);
|
|
44
|
+
this.#logger = logger ?? getDefaultLogger();
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
18
|
+
import { ClientType, Settings as SettingsPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
19
|
import { NotifyUnsubscribeLiteCommand } from '../../proto/apache/rocketmq/v2/service_pb';
|
|
20
20
|
import { Resource } from '../route/Resource';
|
|
21
21
|
import { Endpoints, TopicRouteData } from '../route';
|
|
22
22
|
import { PushConsumer, PushConsumerOptions } from './PushConsumer';
|
|
23
23
|
import { LitePushConsumer } from './LitePushConsumer';
|
|
24
24
|
import { OffsetOption } from './OffsetOption';
|
|
25
|
-
import {
|
|
25
|
+
import { LiteConsumerSupport } from './LiteConsumerSupport';
|
|
26
26
|
import { FilterExpression } from './FilterExpression';
|
|
27
27
|
import { LiteFifoConsumeService } from './LiteFifoConsumeService';
|
|
28
28
|
import { LiteStandardConsumeService } from './LiteStandardConsumeService';
|
|
@@ -52,7 +52,7 @@ export interface LitePushConsumerOptions extends PushConsumerOptions {
|
|
|
52
52
|
* because it uses the assignment-based message delivery mechanism managed by the server.</p>
|
|
53
53
|
*/
|
|
54
54
|
export class LitePushConsumerImpl extends PushConsumer implements LitePushConsumer {
|
|
55
|
-
private readonly liteSubscriptionManager:
|
|
55
|
+
private readonly liteSubscriptionManager: LiteConsumerSupport;
|
|
56
56
|
private readonly bindTopic: Resource;
|
|
57
57
|
readonly #messageListener: MessageListener;
|
|
58
58
|
readonly #enableFifoConsumeAccelerator: boolean;
|
|
@@ -73,7 +73,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
73
73
|
this.bindTopic = new Resource(options.namespace, options.bindTopic);
|
|
74
74
|
|
|
75
75
|
const groupResource = new Resource(options.namespace, options.consumerGroup);
|
|
76
|
-
this.liteSubscriptionManager = new
|
|
76
|
+
this.liteSubscriptionManager = new LiteConsumerSupport(
|
|
77
77
|
this,
|
|
78
78
|
this.bindTopic,
|
|
79
79
|
groupResource,
|
|
@@ -96,10 +96,10 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
96
96
|
this.consumerGroup, this.clientId, this.#enableFifoConsumeAccelerator,
|
|
97
97
|
);
|
|
98
98
|
return new LiteFifoConsumeService(this.clientId, this.#messageListener, this.#enableFifoConsumeAccelerator,
|
|
99
|
-
this, this.consumerGroup);
|
|
99
|
+
this, this.consumerGroup, this.logger);
|
|
100
100
|
}
|
|
101
101
|
this.logger.info('Create lite standard consume service, consumerGroup=%s, clientId=%s', this.consumerGroup, this.clientId);
|
|
102
|
-
return new LiteStandardConsumeService(this.clientId, this.#messageListener, this, this.consumerGroup);
|
|
102
|
+
return new LiteStandardConsumeService(this.clientId, this.#messageListener, this, this.consumerGroup, this.logger);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
@@ -112,13 +112,11 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
|
-
*
|
|
116
|
-
* LitePushConsumer uses Lite Subscription mechanism instead of assignments.
|
|
115
|
+
* Route updates do not create queues; Lite Push uses the parent's assignment scans to maintain ProcessQueues.
|
|
117
116
|
*/
|
|
118
117
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
119
118
|
protected onTopicRouteDataUpdate(_topic: string, _topicRouteData: TopicRouteData) {
|
|
120
|
-
//
|
|
121
|
-
// They rely on LiteSubscriptionManager for message delivery
|
|
119
|
+
// LiteSubscriptionManager maintains subscriptions; server assignments determine receive queues.
|
|
122
120
|
}
|
|
123
121
|
|
|
124
122
|
/**
|
|
@@ -128,8 +126,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
128
126
|
* It must be called before the consumer can receive messages.</p>
|
|
129
127
|
*/
|
|
130
128
|
async startup() {
|
|
131
|
-
await super.startup();
|
|
132
|
-
this.liteSubscriptionManager.startUp();
|
|
129
|
+
await this.liteSubscriptionManager.startConsumer(() => super.startup());
|
|
133
130
|
}
|
|
134
131
|
|
|
135
132
|
/**
|
|
@@ -139,8 +136,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
139
136
|
* and stopping the lite subscription manager.</p>
|
|
140
137
|
*/
|
|
141
138
|
async shutdown() {
|
|
142
|
-
this.liteSubscriptionManager.shutdown();
|
|
143
|
-
await super.shutdown();
|
|
139
|
+
await this.liteSubscriptionManager.shutdownConsumer(() => super.shutdown());
|
|
144
140
|
}
|
|
145
141
|
|
|
146
142
|
/**
|
|
@@ -168,10 +164,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
168
164
|
async subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
|
|
169
165
|
|
|
170
166
|
async subscribeLite(liteTopic: string, offsetOption?: OffsetOption): Promise<void> {
|
|
171
|
-
|
|
172
|
-
throw new Error('liteTopic should not be blank');
|
|
173
|
-
}
|
|
174
|
-
await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption ?? null);
|
|
167
|
+
await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption);
|
|
175
168
|
}
|
|
176
169
|
|
|
177
170
|
/**
|
|
@@ -183,9 +176,6 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
183
176
|
* @param liteTopic - The name of the lite topic to unsubscribe from
|
|
184
177
|
*/
|
|
185
178
|
async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
186
|
-
if (!liteTopic || liteTopic.trim().length === 0) {
|
|
187
|
-
throw new Error('liteTopic should not be blank');
|
|
188
|
-
}
|
|
189
179
|
await this.liteSubscriptionManager.unsubscribeLite(liteTopic);
|
|
190
180
|
}
|
|
191
181
|
|
|
@@ -237,7 +227,10 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
|
|
|
237
227
|
* @param endpoints - The server endpoints
|
|
238
228
|
* @param settings - The settings configuration
|
|
239
229
|
*/
|
|
240
|
-
onSettingsCommand(endpoints:
|
|
230
|
+
onSettingsCommand(endpoints: Endpoints, settings: SettingsPB): void;
|
|
231
|
+
// Preserve the public broad overload; implement handling with the same protocol types as the parent.
|
|
232
|
+
onSettingsCommand(endpoints: any, settings: any): void;
|
|
233
|
+
onSettingsCommand(endpoints: Endpoints, settings: SettingsPB): void {
|
|
241
234
|
super.onSettingsCommand(endpoints, settings);
|
|
242
235
|
this.liteSubscriptionManager.sync(settings);
|
|
243
236
|
}
|
|
@@ -24,7 +24,8 @@ import { ILogger } from '../client/Logger';
|
|
|
24
24
|
import { RpcClientManager } from '../client/RpcClientManager';
|
|
25
25
|
import { SimpleConsumer, SimpleConsumerOptions } from './SimpleConsumer';
|
|
26
26
|
import { FilterExpression } from './FilterExpression';
|
|
27
|
-
import {
|
|
27
|
+
import { LiteSubscriptionHost } from './LiteSubscriptionManager';
|
|
28
|
+
import { LiteConsumerSupport } from './LiteConsumerSupport';
|
|
28
29
|
import { OffsetOption } from './OffsetOption';
|
|
29
30
|
import { LiteSimpleConsumer, LiteSimpleConsumerOptions } from './LiteSimpleConsumer';
|
|
30
31
|
|
|
@@ -51,7 +52,7 @@ export function firstReadableMasterQueue(topicRouteData: TopicRouteData): TopicR
|
|
|
51
52
|
export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
52
53
|
implements LiteSimpleConsumer, LiteSubscriptionHost {
|
|
53
54
|
readonly #bindTopic: Resource;
|
|
54
|
-
readonly #liteSubscriptionManager:
|
|
55
|
+
readonly #liteSubscriptionManager: LiteConsumerSupport;
|
|
55
56
|
|
|
56
57
|
constructor(options: LiteSimpleConsumerOptions) {
|
|
57
58
|
if (!options.bindTopic || options.bindTopic.trim().length === 0) {
|
|
@@ -68,7 +69,7 @@ export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
|
68
69
|
|
|
69
70
|
this.#bindTopic = new Resource(options.namespace, options.bindTopic);
|
|
70
71
|
const groupResource = new Resource(options.namespace, options.consumerGroup);
|
|
71
|
-
this.#liteSubscriptionManager = new
|
|
72
|
+
this.#liteSubscriptionManager = new LiteConsumerSupport(this, this.#bindTopic, groupResource);
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
/**
|
|
@@ -91,8 +92,7 @@ export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
|
91
92
|
* manager. It must be called before the consumer can receive messages.</p>
|
|
92
93
|
*/
|
|
93
94
|
async startup() {
|
|
94
|
-
await super.startup();
|
|
95
|
-
this.#liteSubscriptionManager.startUp();
|
|
95
|
+
await this.#liteSubscriptionManager.startConsumer(() => super.startup());
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
@@ -102,8 +102,7 @@ export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
|
102
102
|
* and stopping the lite subscription manager.</p>
|
|
103
103
|
*/
|
|
104
104
|
async shutdown() {
|
|
105
|
-
this.#liteSubscriptionManager.shutdown();
|
|
106
|
-
await super.shutdown();
|
|
105
|
+
await this.#liteSubscriptionManager.shutdownConsumer(() => super.shutdown());
|
|
107
106
|
}
|
|
108
107
|
|
|
109
108
|
async subscribeLite(liteTopic: string): Promise<void>;
|
|
@@ -118,10 +117,7 @@ export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
|
118
117
|
async subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
|
|
119
118
|
|
|
120
119
|
async subscribeLite(liteTopic: string, offsetOption?: OffsetOption): Promise<void> {
|
|
121
|
-
|
|
122
|
-
throw new Error('liteTopic should not be blank');
|
|
123
|
-
}
|
|
124
|
-
await this.#liteSubscriptionManager.subscribeLite(liteTopic, offsetOption ?? null);
|
|
120
|
+
await this.#liteSubscriptionManager.subscribeLite(liteTopic, offsetOption);
|
|
125
121
|
}
|
|
126
122
|
|
|
127
123
|
/**
|
|
@@ -130,9 +126,6 @@ export class LiteSimpleConsumerImpl extends SimpleConsumer
|
|
|
130
126
|
* @param liteTopic - The name of the lite topic to unsubscribe from
|
|
131
127
|
*/
|
|
132
128
|
async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
133
|
-
if (!liteTopic || liteTopic.trim().length === 0) {
|
|
134
|
-
throw new Error('liteTopic should not be blank');
|
|
135
|
-
}
|
|
136
129
|
await this.#liteSubscriptionManager.unsubscribeLite(liteTopic);
|
|
137
130
|
}
|
|
138
131
|
|
|
@@ -33,9 +33,9 @@ export class LiteStandardConsumeService extends ConsumeService {
|
|
|
33
33
|
readonly #logger: ILogger;
|
|
34
34
|
|
|
35
35
|
constructor(clientId: string, messageListener: MessageListener,
|
|
36
|
-
messageInterceptor?: MessageInterceptor, consumerGroup?: string) {
|
|
36
|
+
messageInterceptor?: MessageInterceptor, consumerGroup?: string, logger?: ILogger) {
|
|
37
37
|
super(clientId, messageListener, messageInterceptor, consumerGroup);
|
|
38
|
-
this.#logger = getDefaultLogger();
|
|
38
|
+
this.#logger = logger ?? getDefaultLogger();
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
consume(pq: ProcessQueue, messageViews: MessageView[]): void {
|
|
@@ -24,8 +24,9 @@ import {
|
|
|
24
24
|
import { Endpoints } from '../route';
|
|
25
25
|
import { ILogger } from '../client/Logger';
|
|
26
26
|
import { RpcClientManager } from '../client/RpcClientManager';
|
|
27
|
-
import { ClientException } from '../exception';
|
|
27
|
+
import { ClientException, LiteSubscriptionQuotaExceededException, StatusChecker } from '../exception';
|
|
28
28
|
import { Resource } from '../route/Resource';
|
|
29
|
+
import { getRequestId } from '../util/rpcContext';
|
|
29
30
|
import { OffsetOption } from './OffsetOption';
|
|
30
31
|
|
|
31
32
|
const SYNC_LITE_SUBSCRIPTION_INTERVAL = 30000; // 30 seconds
|
|
@@ -84,6 +85,9 @@ export class LiteSubscriptionManager {
|
|
|
84
85
|
* Start periodic sync of lite subscriptions.
|
|
85
86
|
*/
|
|
86
87
|
public startUp() {
|
|
88
|
+
if (this.syncTimer) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
87
91
|
// syncAll after startup for subscribeLite("*")
|
|
88
92
|
this.syncAllLiteSubscription();
|
|
89
93
|
this.syncTimer = setInterval(
|
|
@@ -152,7 +156,7 @@ export class LiteSubscriptionManager {
|
|
|
152
156
|
): Promise<void> {
|
|
153
157
|
// Check if consumer is running
|
|
154
158
|
if (!this.host.isRunning()) {
|
|
155
|
-
throw new
|
|
159
|
+
throw new Error('Consumer is not running');
|
|
156
160
|
}
|
|
157
161
|
|
|
158
162
|
// Already subscribed
|
|
@@ -197,7 +201,7 @@ export class LiteSubscriptionManager {
|
|
|
197
201
|
public async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
198
202
|
// Check if consumer is running
|
|
199
203
|
if (!this.host.isRunning()) {
|
|
200
|
-
throw new
|
|
204
|
+
throw new Error('Consumer is not running');
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
// Not subscribed
|
|
@@ -291,15 +295,8 @@ export class LiteSubscriptionManager {
|
|
|
291
295
|
await Promise.all(endpointsList.map(async endpoints => {
|
|
292
296
|
const response: SyncLiteSubscriptionResponse = await this.host.getRpcClientManager()
|
|
293
297
|
.syncLiteSubscription(endpoints, request, this.host.getRequestTimeout());
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const status = response.getStatus();
|
|
297
|
-
if (status && status.getCode() !== Code.OK) {
|
|
298
|
-
throw new ClientException(
|
|
299
|
-
status.getCode(),
|
|
300
|
-
`Failed to sync lite subscription to endpoints=${endpoints.facade}: ${status.getMessage()}`,
|
|
301
|
-
);
|
|
302
|
-
}
|
|
298
|
+
// Map the status to typed exceptions (e.g. LiteSubscriptionQuotaExceededException), as Java does.
|
|
299
|
+
StatusChecker.check(response.getStatus()?.toObject(), getRequestId(response));
|
|
303
300
|
}));
|
|
304
301
|
|
|
305
302
|
if (logger.info) {
|
|
@@ -363,8 +360,9 @@ export class LiteSubscriptionManager {
|
|
|
363
360
|
*/
|
|
364
361
|
private checkLiteSubscriptionQuota(delta: number) {
|
|
365
362
|
if (this.liteTopicSet.size + delta > this.liteSubscriptionQuota) {
|
|
366
|
-
throw new
|
|
367
|
-
|
|
363
|
+
throw new LiteSubscriptionQuotaExceededException(
|
|
364
|
+
Code.LITE_SUBSCRIPTION_QUOTA_EXCEEDED,
|
|
365
|
+
null,
|
|
368
366
|
`Lite subscription quota exceeded ${this.liteSubscriptionQuota}`,
|
|
369
367
|
);
|
|
370
368
|
}
|
|
@@ -63,6 +63,13 @@ export class OffsetOption {
|
|
|
63
63
|
this.value = value;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
private static toBigInt(value: number | bigint, name: string): bigint {
|
|
67
|
+
if (typeof value === 'number' && !Number.isSafeInteger(value)) {
|
|
68
|
+
throw new RangeError(`${name} must be a safe integer when provided as number`);
|
|
69
|
+
}
|
|
70
|
+
return typeof value === 'number' ? BigInt(value) : value;
|
|
71
|
+
}
|
|
72
|
+
|
|
66
73
|
/**
|
|
67
74
|
* Create an OffsetOption from a specific offset.
|
|
68
75
|
*
|
|
@@ -71,7 +78,7 @@ export class OffsetOption {
|
|
|
71
78
|
* @throws Error if offset is negative
|
|
72
79
|
*/
|
|
73
80
|
static ofOffset(offset: number | bigint): OffsetOption {
|
|
74
|
-
const value =
|
|
81
|
+
const value = OffsetOption.toBigInt(offset, 'offset');
|
|
75
82
|
if (value < BigInt(0)) {
|
|
76
83
|
throw new Error('offset must be greater than or equal to 0');
|
|
77
84
|
}
|
|
@@ -86,7 +93,7 @@ export class OffsetOption {
|
|
|
86
93
|
* @throws Error if tailN is negative
|
|
87
94
|
*/
|
|
88
95
|
static ofTailN(tailN: number | bigint): OffsetOption {
|
|
89
|
-
const value =
|
|
96
|
+
const value = OffsetOption.toBigInt(tailN, 'tailN');
|
|
90
97
|
if (value < BigInt(0)) {
|
|
91
98
|
throw new Error('tailN must be greater than or equal to 0');
|
|
92
99
|
}
|
|
@@ -101,7 +108,7 @@ export class OffsetOption {
|
|
|
101
108
|
* @throws Error if timestamp is negative
|
|
102
109
|
*/
|
|
103
110
|
static ofTimestamp(timestamp: number | bigint): OffsetOption {
|
|
104
|
-
const value =
|
|
111
|
+
const value = OffsetOption.toBigInt(timestamp, 'timestamp');
|
|
105
112
|
if (value < BigInt(0)) {
|
|
106
113
|
throw new Error('timestamp must be greater than or equal to 0');
|
|
107
114
|
}
|