rocketmq-client-nodejs 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -1
- package/dist/client/BaseClient.d.ts +24 -2
- package/dist/client/BaseClient.js +173 -28
- package/dist/client/ClientId.js +2 -2
- package/dist/client/Logger.d.ts +1 -0
- package/dist/client/Logger.js +1 -1
- package/dist/client/RpcClient.d.ts +3 -1
- package/dist/client/RpcClient.js +23 -1
- package/dist/client/RpcClientManager.d.ts +3 -1
- package/dist/client/RpcClientManager.js +23 -4
- package/dist/client/TelemetrySession.d.ts +1 -0
- package/dist/client/TelemetrySession.js +47 -9
- package/dist/consumer/Assignment.d.ts +24 -0
- package/dist/consumer/Assignment.js +40 -0
- package/dist/consumer/Assignments.d.ts +25 -0
- package/dist/consumer/Assignments.js +55 -0
- package/dist/consumer/ConsumeResult.d.ts +20 -0
- package/dist/consumer/ConsumeResult.js +25 -0
- package/dist/consumer/ConsumeService.d.ts +28 -0
- package/dist/consumer/ConsumeService.js +53 -0
- package/dist/consumer/ConsumeTask.d.ts +24 -0
- package/dist/consumer/ConsumeTask.js +39 -0
- package/dist/consumer/Consumer.d.ts +19 -1
- package/dist/consumer/Consumer.js +64 -21
- package/dist/consumer/FifoConsumeService.d.ts +25 -0
- package/dist/consumer/FifoConsumeService.js +130 -0
- package/dist/consumer/FilterExpression.d.ts +2 -0
- package/dist/consumer/FilterExpression.js +18 -1
- package/dist/consumer/LitePushConsumer.d.ts +161 -0
- package/dist/consumer/LitePushConsumer.js +172 -0
- package/dist/consumer/LitePushConsumerImpl.d.ts +167 -0
- package/dist/consumer/LitePushConsumerImpl.js +193 -0
- package/dist/consumer/LiteSubscriptionManager.d.ts +97 -0
- package/dist/consumer/LiteSubscriptionManager.js +236 -0
- package/dist/consumer/MessageListener.d.ts +21 -0
- package/dist/consumer/MessageListener.js +19 -0
- package/dist/consumer/OffsetOption.d.ts +109 -0
- package/dist/consumer/OffsetOption.js +191 -0
- package/dist/consumer/ProcessQueue.d.ts +39 -0
- package/dist/consumer/ProcessQueue.js +293 -0
- package/dist/consumer/PushConsumer.d.ts +88 -0
- package/dist/consumer/PushConsumer.js +449 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
- package/dist/consumer/PushSubscriptionSettings.js +97 -0
- package/dist/consumer/SimpleConsumer.d.ts +12 -1
- package/dist/consumer/SimpleConsumer.js +54 -10
- package/dist/consumer/SimpleSubscriptionSettings.d.ts +2 -2
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +25 -0
- package/dist/consumer/StandardConsumeService.js +50 -0
- package/dist/consumer/index.d.ts +15 -0
- package/dist/consumer/index.js +19 -1
- package/dist/exception/InternalErrorException.d.ts +5 -0
- package/dist/exception/InternalErrorException.js +10 -1
- package/dist/exception/LiteSubscriptionQuotaExceededException.d.ts +23 -0
- package/dist/exception/LiteSubscriptionQuotaExceededException.js +31 -0
- package/dist/exception/LiteTopicQuotaExceededException.d.ts +23 -0
- package/dist/exception/LiteTopicQuotaExceededException.js +31 -0
- package/dist/exception/StatusChecker.js +4 -1
- package/dist/exception/index.d.ts +2 -0
- package/dist/exception/index.js +3 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/message/Message.d.ts +8 -0
- package/dist/message/Message.js +39 -6
- package/dist/message/MessageId.d.ts +2 -0
- package/dist/message/MessageId.js +17 -1
- package/dist/message/MessageView.d.ts +86 -0
- package/dist/message/MessageView.js +115 -1
- package/dist/message/PublishingMessage.js +21 -5
- package/dist/producer/Producer.d.ts +21 -2
- package/dist/producer/Producer.js +120 -25
- package/dist/producer/PublishingLoadBalancer.d.ts +2 -0
- package/dist/producer/PublishingLoadBalancer.js +23 -1
- package/dist/producer/RecallReceipt.d.ts +25 -0
- package/dist/producer/RecallReceipt.js +34 -0
- package/dist/producer/SendReceipt.d.ts +2 -1
- package/dist/producer/SendReceipt.js +6 -3
- package/dist/producer/Transaction.js +7 -1
- package/dist/producer/index.d.ts +1 -0
- package/dist/producer/index.js +2 -1
- package/dist/route/Broker.d.ts +2 -0
- package/dist/route/Broker.js +21 -1
- package/dist/route/Endpoints.d.ts +2 -0
- package/dist/route/Endpoints.js +36 -1
- package/dist/route/MessageQueue.d.ts +2 -0
- package/dist/route/MessageQueue.js +23 -1
- package/dist/route/Resource.d.ts +54 -0
- package/dist/route/Resource.js +100 -0
- package/dist/route/TopicRouteData.d.ts +12 -0
- package/dist/route/TopicRouteData.js +37 -1
- package/dist/route/index.d.ts +1 -0
- package/dist/route/index.js +2 -1
- package/dist/util/index.d.ts +8 -0
- package/dist/util/index.js +20 -1
- package/package.json +2 -3
- package/proto/apache/rocketmq/v2/definition.proto +56 -0
- package/proto/apache/rocketmq/v2/definition_pb.d.ts +100 -0
- package/proto/apache/rocketmq/v2/definition_pb.js +570 -4
- package/proto/apache/rocketmq/v2/service.proto +65 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +34 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.js +81 -4
- package/proto/apache/rocketmq/v2/service_pb.d.ts +211 -0
- package/proto/apache/rocketmq/v2/service_pb.js +1691 -25
- package/src/client/BaseClient.ts +196 -28
- package/src/client/ClientId.ts +1 -1
- package/src/client/Logger.ts +1 -0
- package/src/client/RpcClient.ts +26 -0
- package/src/client/RpcClientManager.ts +27 -3
- package/src/client/TelemetrySession.ts +54 -8
- package/src/consumer/Assignment.ts +40 -0
- package/src/consumer/Assignments.ts +55 -0
- package/src/consumer/ConsumeResult.ts +21 -0
- package/src/consumer/ConsumeService.ts +72 -0
- package/src/consumer/ConsumeTask.ts +39 -0
- package/src/consumer/Consumer.ts +76 -20
- package/src/consumer/FifoConsumeService.ts +146 -0
- package/src/consumer/FilterExpression.ts +17 -0
- package/src/consumer/LitePushConsumer.ts +256 -0
- package/src/consumer/LitePushConsumerImpl.ts +245 -0
- package/src/consumer/LiteSubscriptionManager.ts +352 -0
- package/src/consumer/MessageListener.ts +23 -0
- package/src/consumer/OffsetOption.ts +203 -0
- package/src/consumer/ProcessQueue.ts +339 -0
- package/src/consumer/PushConsumer.ts +563 -0
- package/src/consumer/PushSubscriptionSettings.ts +124 -0
- package/src/consumer/SimpleConsumer.ts +68 -11
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +57 -0
- package/src/consumer/index.ts +15 -0
- package/src/exception/InternalErrorException.ts +10 -0
- package/src/exception/LiteSubscriptionQuotaExceededException.ts +28 -0
- package/src/exception/LiteTopicQuotaExceededException.ts +28 -0
- package/src/exception/StatusChecker.ts +3 -0
- package/src/exception/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/message/Message.ts +46 -5
- package/src/message/MessageId.ts +16 -0
- package/src/message/MessageView.ts +127 -0
- package/src/message/PublishingMessage.ts +20 -4
- package/src/producer/Producer.ts +144 -24
- package/src/producer/PublishingLoadBalancer.ts +21 -0
- package/src/producer/RecallReceipt.ts +32 -0
- package/src/producer/SendReceipt.ts +5 -2
- package/src/producer/Transaction.ts +16 -0
- package/src/producer/index.ts +1 -0
- package/src/route/Broker.ts +20 -0
- package/src/route/Endpoints.ts +32 -0
- package/src/route/MessageQueue.ts +23 -1
- package/src/route/Resource.ts +104 -0
- package/src/route/TopicRouteData.ts +37 -0
- package/src/route/index.ts +1 -0
- package/src/util/index.ts +19 -0
|
@@ -0,0 +1,245 @@
|
|
|
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 { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { NotifyUnsubscribeLiteCommand } from '../../proto/apache/rocketmq/v2/service_pb';
|
|
20
|
+
import { Resource } from '../route/Resource';
|
|
21
|
+
import { Endpoints, TopicRouteData } from '../route';
|
|
22
|
+
import { PushConsumer, PushConsumerOptions } from './PushConsumer';
|
|
23
|
+
import { LitePushConsumer } from './LitePushConsumer';
|
|
24
|
+
import { OffsetOption } from './OffsetOption';
|
|
25
|
+
import { LiteSubscriptionManager } from './LiteSubscriptionManager';
|
|
26
|
+
import { FilterExpression } from './FilterExpression';
|
|
27
|
+
|
|
28
|
+
export interface LitePushConsumerOptions extends PushConsumerOptions {
|
|
29
|
+
bindTopic: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Implementation of LitePushConsumer.
|
|
34
|
+
*
|
|
35
|
+
* <p>LitePushConsumer extends PushConsumer to provide lightweight message consumption
|
|
36
|
+
* with reduced metadata and storage overhead. It supports dynamic subscription
|
|
37
|
+
* management for lite topics.</p>
|
|
38
|
+
*
|
|
39
|
+
* <p>Key features:</p>
|
|
40
|
+
* <ul>
|
|
41
|
+
* <li>Dynamic lite topic subscription/unsubscription</li>
|
|
42
|
+
* <li>Quota management for lite subscriptions</li>
|
|
43
|
+
* <li>Reduced resource consumption compared to standard PushConsumer</li>
|
|
44
|
+
* <li>Automatic synchronization with server settings</li>
|
|
45
|
+
* </ul>
|
|
46
|
+
*
|
|
47
|
+
* <p>Note: Unlike LiteSimpleConsumer, LitePushConsumer does not perform route optimization
|
|
48
|
+
* because it uses the assignment-based message delivery mechanism managed by the server.</p>
|
|
49
|
+
*/
|
|
50
|
+
export class LitePushConsumerImpl extends PushConsumer implements LitePushConsumer {
|
|
51
|
+
private readonly liteSubscriptionManager: LiteSubscriptionManager;
|
|
52
|
+
private readonly bindTopic: Resource;
|
|
53
|
+
|
|
54
|
+
constructor(options: LitePushConsumerOptions) {
|
|
55
|
+
// Create subscription expressions with bind topic
|
|
56
|
+
const subscriptions = new Map<string, FilterExpression>();
|
|
57
|
+
subscriptions.set(options.bindTopic, FilterExpression.SUB_ALL);
|
|
58
|
+
|
|
59
|
+
super({
|
|
60
|
+
...options,
|
|
61
|
+
subscriptions,
|
|
62
|
+
} as PushConsumerOptions);
|
|
63
|
+
|
|
64
|
+
this.bindTopic = new Resource(options.namespace, options.bindTopic);
|
|
65
|
+
|
|
66
|
+
const groupResource = new Resource(options.namespace, options.consumerGroup);
|
|
67
|
+
this.liteSubscriptionManager = new LiteSubscriptionManager(
|
|
68
|
+
this,
|
|
69
|
+
this.bindTopic,
|
|
70
|
+
groupResource,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get the client type.
|
|
76
|
+
*
|
|
77
|
+
* @return The client type identifier for lite push consumer
|
|
78
|
+
*/
|
|
79
|
+
protected getClientType(): ClientType {
|
|
80
|
+
return ClientType.LITE_PUSH_CONSUMER;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Override to disable standard assignment scanning for lite consumers.
|
|
85
|
+
* LitePushConsumer uses Lite Subscription mechanism instead of assignments.
|
|
86
|
+
*/
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
88
|
+
protected onTopicRouteDataUpdate(_topic: string, _topicRouteData: TopicRouteData) {
|
|
89
|
+
// Lite consumers do not use standard assignment mechanism
|
|
90
|
+
// They rely on LiteSubscriptionManager for message delivery
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Start up the consumer.
|
|
95
|
+
*
|
|
96
|
+
* <p>This method initializes the consumer and starts the lite subscription manager.
|
|
97
|
+
* It must be called before the consumer can receive messages.</p>
|
|
98
|
+
*/
|
|
99
|
+
async startup() {
|
|
100
|
+
await super.startup();
|
|
101
|
+
this.liteSubscriptionManager.startUp();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Shutdown the consumer.
|
|
106
|
+
*
|
|
107
|
+
* <p>This method gracefully shuts down the consumer, releasing all resources
|
|
108
|
+
* and stopping the lite subscription manager.</p>
|
|
109
|
+
*/
|
|
110
|
+
async shutdown() {
|
|
111
|
+
this.liteSubscriptionManager.shutdown();
|
|
112
|
+
await super.shutdown();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Subscribe to a lite topic.
|
|
117
|
+
*
|
|
118
|
+
* <p>The subscribeLite() method initiates network requests and performs quota verification,
|
|
119
|
+
* so it may fail. It's important to handle potential errors. Possible failure scenarios include:</p>
|
|
120
|
+
* <ul>
|
|
121
|
+
* <li>Network request errors, which can be retried.</li>
|
|
122
|
+
* <li>Quota verification failures (LiteSubscriptionQuotaExceededException).
|
|
123
|
+
* In this case, evaluate whether the quota is insufficient and promptly unsubscribe
|
|
124
|
+
* from unused subscriptions using unsubscribeLite() to free up resources.</li>
|
|
125
|
+
* </ul>
|
|
126
|
+
*
|
|
127
|
+
* @param liteTopic - The name of the lite topic to subscribe
|
|
128
|
+
*/
|
|
129
|
+
async subscribeLite(liteTopic: string): Promise<void>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Subscribe to a lite topic with offset option to specify the consume from offset.
|
|
133
|
+
*
|
|
134
|
+
* @param liteTopic - The name of the lite topic to subscribe
|
|
135
|
+
* @param offsetOption - The consume from offset option
|
|
136
|
+
*/
|
|
137
|
+
async subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
|
|
138
|
+
|
|
139
|
+
async subscribeLite(liteTopic: string, offsetOption?: OffsetOption): Promise<void> {
|
|
140
|
+
await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption ?? null);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Unsubscribe from a lite topic.
|
|
145
|
+
*
|
|
146
|
+
* <p>This method removes the subscription and notifies the server.
|
|
147
|
+
* After unsubscribing, the consumer will no longer receive messages from this topic.</p>
|
|
148
|
+
*
|
|
149
|
+
* @param liteTopic - The name of the lite topic to unsubscribe from
|
|
150
|
+
*/
|
|
151
|
+
async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
152
|
+
await this.liteSubscriptionManager.unsubscribeLite(liteTopic);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Get the lite topic set.
|
|
157
|
+
*
|
|
158
|
+
* <p>Returns an immutable set of currently subscribed lite topics.
|
|
159
|
+
* This set reflects the current state of subscriptions managed by the lite subscription manager.</p>
|
|
160
|
+
*
|
|
161
|
+
* @return Set of lite topic names
|
|
162
|
+
*/
|
|
163
|
+
getLiteTopicSet(): Set<string> {
|
|
164
|
+
return this.liteSubscriptionManager.getLiteTopicSet();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Get the load balancing group for the consumer.
|
|
169
|
+
*
|
|
170
|
+
* <p>The consumer group is used for load balancing across multiple consumer instances.
|
|
171
|
+
* All consumers in the same group will share the message load.</p>
|
|
172
|
+
*
|
|
173
|
+
* @return Consumer group name
|
|
174
|
+
*/
|
|
175
|
+
getConsumerGroup(): string {
|
|
176
|
+
return this.liteSubscriptionManager.getConsumerGroupName();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Handle notify unsubscribe lite command from server.
|
|
181
|
+
*
|
|
182
|
+
* <p>This method is called when the server sends a notification to unsubscribe
|
|
183
|
+
* from a lite topic, typically due to quota violations or administrative actions.</p>
|
|
184
|
+
*
|
|
185
|
+
* @param command - The unsubscribe command from the server
|
|
186
|
+
*/
|
|
187
|
+
onNotifyUnsubscribeLiteCommand(command: NotifyUnsubscribeLiteCommand) {
|
|
188
|
+
this.liteSubscriptionManager.onNotifyUnsubscribeLiteCommand(command);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Handle settings command from server.
|
|
193
|
+
*
|
|
194
|
+
* <p>This method processes configuration updates from the server and synchronizes
|
|
195
|
+
* the lite subscription manager with the latest settings.</p>
|
|
196
|
+
*
|
|
197
|
+
* @param endpoints - The server endpoints
|
|
198
|
+
* @param settings - The settings configuration
|
|
199
|
+
*/
|
|
200
|
+
onSettingsCommand(endpoints: any, settings: any) {
|
|
201
|
+
super.onSettingsCommand(endpoints, settings);
|
|
202
|
+
this.liteSubscriptionManager.sync(settings);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Close the consumer.
|
|
207
|
+
*/
|
|
208
|
+
async close(): Promise<void> {
|
|
209
|
+
await this.shutdown();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Get the logger.
|
|
214
|
+
*/
|
|
215
|
+
getLogger() {
|
|
216
|
+
return this.logger;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get the RPC client manager (protected access for internal use).
|
|
221
|
+
*
|
|
222
|
+
* @internal
|
|
223
|
+
*/
|
|
224
|
+
getRpcClientManager() {
|
|
225
|
+
return this.rpcClientManager;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Get the endpoints (protected access for internal use).
|
|
230
|
+
*
|
|
231
|
+
* @internal
|
|
232
|
+
*/
|
|
233
|
+
getEndpoints(): Endpoints {
|
|
234
|
+
return this.endpoints;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Get the request timeout (protected access for internal use).
|
|
239
|
+
*
|
|
240
|
+
* @internal
|
|
241
|
+
*/
|
|
242
|
+
getRequestTimeout(): number {
|
|
243
|
+
return this.requestTimeout;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
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 { Code, LiteSubscriptionAction } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import {
|
|
20
|
+
NotifyUnsubscribeLiteCommand,
|
|
21
|
+
SyncLiteSubscriptionRequest,
|
|
22
|
+
SyncLiteSubscriptionResponse,
|
|
23
|
+
} from '../../proto/apache/rocketmq/v2/service_pb';
|
|
24
|
+
import { ClientException } from '../exception';
|
|
25
|
+
import { Resource } from '../route';
|
|
26
|
+
import { LitePushConsumerImpl } from './LitePushConsumerImpl';
|
|
27
|
+
import { OffsetOption } from './OffsetOption';
|
|
28
|
+
|
|
29
|
+
const SYNC_LITE_SUBSCRIPTION_INTERVAL = 30000; // 30 seconds
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Manages lite topic subscriptions for LitePushConsumer.
|
|
33
|
+
*
|
|
34
|
+
* <p>LiteSubscriptionManager handles:
|
|
35
|
+
* - Maintaining the set of subscribed lite topics
|
|
36
|
+
* - Periodic synchronization of lite subscriptions with server
|
|
37
|
+
* - Quota management and verification
|
|
38
|
+
* - Handling unsubscribe commands from server</p>
|
|
39
|
+
*/
|
|
40
|
+
export class LiteSubscriptionManager {
|
|
41
|
+
private readonly consumerImpl: LitePushConsumerImpl;
|
|
42
|
+
private readonly bindTopic: Resource;
|
|
43
|
+
private readonly group: Resource;
|
|
44
|
+
private readonly liteTopicSet = new Set<string>();
|
|
45
|
+
private liteSubscriptionQuota: number;
|
|
46
|
+
private maxLiteTopicSize: number;
|
|
47
|
+
private syncTimer?: NodeJS.Timeout;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
consumerImpl: LitePushConsumerImpl,
|
|
51
|
+
bindTopic: Resource,
|
|
52
|
+
group: Resource,
|
|
53
|
+
) {
|
|
54
|
+
this.consumerImpl = consumerImpl;
|
|
55
|
+
this.bindTopic = bindTopic;
|
|
56
|
+
this.group = group;
|
|
57
|
+
this.liteSubscriptionQuota = 100; // Default quota
|
|
58
|
+
this.maxLiteTopicSize = 64; // Default max length
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Start periodic sync of lite subscriptions.
|
|
63
|
+
*/
|
|
64
|
+
public startUp() {
|
|
65
|
+
this.syncTimer = setInterval(
|
|
66
|
+
() => this.syncAllLiteSubscription(),
|
|
67
|
+
SYNC_LITE_SUBSCRIPTION_INTERVAL,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Stop the sync timer.
|
|
73
|
+
*/
|
|
74
|
+
public shutdown() {
|
|
75
|
+
// Clear sync timer
|
|
76
|
+
if (this.syncTimer) {
|
|
77
|
+
clearInterval(this.syncTimer);
|
|
78
|
+
this.syncTimer = undefined;
|
|
79
|
+
}
|
|
80
|
+
// Clear lite topic set to free memory
|
|
81
|
+
this.liteTopicSet.clear();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get the bind topic name.
|
|
86
|
+
*/
|
|
87
|
+
public getBindTopicName(): string {
|
|
88
|
+
return this.bindTopic.getName();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the consumer group name.
|
|
93
|
+
*/
|
|
94
|
+
public getConsumerGroupName(): string {
|
|
95
|
+
return this.group.getName();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get a copy of the lite topic set.
|
|
100
|
+
*/
|
|
101
|
+
public getLiteTopicSet(): Set<string> {
|
|
102
|
+
return new Set(this.liteTopicSet);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Sync settings from server.
|
|
107
|
+
*/
|
|
108
|
+
public sync(settings: any) {
|
|
109
|
+
if (!settings?.hasSubscription?.()) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const subscription = settings.getSubscription();
|
|
114
|
+
if (subscription.hasLiteSubscriptionQuota?.()) {
|
|
115
|
+
this.liteSubscriptionQuota = subscription.getLiteSubscriptionQuota();
|
|
116
|
+
}
|
|
117
|
+
if (subscription.hasMaxLiteTopicSize?.()) {
|
|
118
|
+
this.maxLiteTopicSize = subscription.getMaxLiteTopicSize();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Subscribe to a lite topic.
|
|
124
|
+
*/
|
|
125
|
+
public async subscribeLite(
|
|
126
|
+
liteTopic: string,
|
|
127
|
+
offsetOption?: OffsetOption | null,
|
|
128
|
+
): Promise<void> {
|
|
129
|
+
// Check if consumer is running
|
|
130
|
+
if (!this.consumerImpl.isRunning()) {
|
|
131
|
+
throw new ClientException(500, 'Consumer is not running');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Already subscribed
|
|
135
|
+
if (this.liteTopicSet.has(liteTopic)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Validate lite topic
|
|
140
|
+
this.validateLiteTopic(liteTopic);
|
|
141
|
+
|
|
142
|
+
// Check quota
|
|
143
|
+
this.checkLiteSubscriptionQuota(1);
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await this.syncLiteSubscription(
|
|
147
|
+
LiteSubscriptionAction.PARTIAL_ADD,
|
|
148
|
+
[ liteTopic ],
|
|
149
|
+
offsetOption ?? null,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
this.liteTopicSet.add(liteTopic);
|
|
153
|
+
this.consumerImpl.getLogger().info(
|
|
154
|
+
'SubscribeLite %s, topic=%s, group=%s, clientId=%s',
|
|
155
|
+
liteTopic,
|
|
156
|
+
this.getBindTopicName(),
|
|
157
|
+
this.getConsumerGroupName(),
|
|
158
|
+
this.consumerImpl.clientId,
|
|
159
|
+
);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
this.consumerImpl.getLogger().error(
|
|
162
|
+
'Failed to subscribeLite %s, error=%s',
|
|
163
|
+
liteTopic,
|
|
164
|
+
error,
|
|
165
|
+
);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Unsubscribe from a lite topic.
|
|
172
|
+
*/
|
|
173
|
+
public async unsubscribeLite(liteTopic: string): Promise<void> {
|
|
174
|
+
// Check if consumer is running
|
|
175
|
+
if (!this.consumerImpl.isRunning()) {
|
|
176
|
+
throw new ClientException(500, 'Consumer is not running');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Not subscribed
|
|
180
|
+
if (!this.liteTopicSet.has(liteTopic)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
await this.syncLiteSubscription(
|
|
186
|
+
LiteSubscriptionAction.PARTIAL_REMOVE,
|
|
187
|
+
[ liteTopic ],
|
|
188
|
+
null,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
this.liteTopicSet.delete(liteTopic);
|
|
192
|
+
this.consumerImpl.getLogger().info(
|
|
193
|
+
'UnsubscribeLite %s, topic=%s, group=%s, clientId=%s',
|
|
194
|
+
liteTopic,
|
|
195
|
+
this.getBindTopicName(),
|
|
196
|
+
this.getConsumerGroupName(),
|
|
197
|
+
this.consumerImpl.clientId,
|
|
198
|
+
);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
this.consumerImpl.getLogger().error(
|
|
201
|
+
'Failed to unsubscribeLite %s, error=%s',
|
|
202
|
+
liteTopic,
|
|
203
|
+
error,
|
|
204
|
+
);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Periodic sync of all lite subscriptions.
|
|
211
|
+
*/
|
|
212
|
+
private async syncAllLiteSubscription() {
|
|
213
|
+
try {
|
|
214
|
+
this.checkLiteSubscriptionQuota(0);
|
|
215
|
+
await this.syncLiteSubscription(
|
|
216
|
+
LiteSubscriptionAction.COMPLETE_ADD,
|
|
217
|
+
Array.from(this.liteTopicSet),
|
|
218
|
+
null,
|
|
219
|
+
);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
this.consumerImpl.getLogger().error(
|
|
222
|
+
'Schedule syncAllLiteSubscription error, clientId=%s, error=%s',
|
|
223
|
+
this.consumerImpl.clientId,
|
|
224
|
+
error,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Sync lite subscription with server.
|
|
231
|
+
*/
|
|
232
|
+
private async syncLiteSubscription(
|
|
233
|
+
action: LiteSubscriptionAction,
|
|
234
|
+
liteTopics: string[],
|
|
235
|
+
offsetOption: OffsetOption | null,
|
|
236
|
+
): Promise<void> {
|
|
237
|
+
const logger = this.consumerImpl.getLogger();
|
|
238
|
+
if (logger.debug) {
|
|
239
|
+
logger.debug(
|
|
240
|
+
'SyncLiteSubscription: action=%s, liteTopics=[%s], offsetOption=%s',
|
|
241
|
+
LiteSubscriptionAction[action],
|
|
242
|
+
liteTopics.join(', '),
|
|
243
|
+
offsetOption?.toString() ?? 'null',
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Build the request
|
|
248
|
+
const request = new SyncLiteSubscriptionRequest()
|
|
249
|
+
.setAction(action)
|
|
250
|
+
.setTopic(this.bindTopic.toProtobuf())
|
|
251
|
+
.setGroup(this.group.toProtobuf())
|
|
252
|
+
.setLiteTopicSetList(liteTopics);
|
|
253
|
+
|
|
254
|
+
// Set offset option if provided
|
|
255
|
+
if (offsetOption) {
|
|
256
|
+
request.setOffsetOption(offsetOption.toProtobuf());
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
// Call RPC client using public methods from LitePushConsumerImpl
|
|
261
|
+
const response: SyncLiteSubscriptionResponse = await this.consumerImpl.getRpcClientManager().syncLiteSubscription(
|
|
262
|
+
this.consumerImpl.getEndpoints(),
|
|
263
|
+
request,
|
|
264
|
+
this.consumerImpl.getRequestTimeout(),
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
// Handle response status
|
|
268
|
+
const status = response.getStatus();
|
|
269
|
+
if (status && status.getCode() !== Code.OK) {
|
|
270
|
+
throw new ClientException(
|
|
271
|
+
status.getCode(),
|
|
272
|
+
`Failed to sync lite subscription: ${status.getMessage()}`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (logger.info) {
|
|
277
|
+
logger.info(
|
|
278
|
+
'SyncLiteSubscription success: action=%s, liteTopics=[%s], clientId=%s',
|
|
279
|
+
LiteSubscriptionAction[action],
|
|
280
|
+
liteTopics.join(', '),
|
|
281
|
+
this.consumerImpl.clientId,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (logger.error) {
|
|
286
|
+
logger.error(
|
|
287
|
+
'SyncLiteSubscription failed: action=%s, liteTopics=[%s], clientId=%s, error=%s',
|
|
288
|
+
LiteSubscriptionAction[action],
|
|
289
|
+
liteTopics.join(', '),
|
|
290
|
+
this.consumerImpl.clientId,
|
|
291
|
+
error,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
throw error;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Handle notify unsubscribe lite command from server.
|
|
300
|
+
*/
|
|
301
|
+
public onNotifyUnsubscribeLiteCommand(command: NotifyUnsubscribeLiteCommand) {
|
|
302
|
+
const liteTopic = command.getLiteTopic();
|
|
303
|
+
this.consumerImpl.getLogger().info(
|
|
304
|
+
'Notify unsubscribe lite: liteTopic=%s, group=%s, bindTopic=%s',
|
|
305
|
+
liteTopic,
|
|
306
|
+
this.getConsumerGroupName(),
|
|
307
|
+
this.getBindTopicName(),
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
if (liteTopic && liteTopic.trim().length > 0) {
|
|
311
|
+
this.liteTopicSet.delete(liteTopic);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Validate lite topic name.
|
|
317
|
+
*/
|
|
318
|
+
private validateLiteTopic(liteTopic: string, maxLength?: number) {
|
|
319
|
+
const maxLen = maxLength ?? this.maxLiteTopicSize;
|
|
320
|
+
|
|
321
|
+
if (!liteTopic || liteTopic.trim().length === 0) {
|
|
322
|
+
throw new Error('liteTopic is blank');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (liteTopic.length > maxLen) {
|
|
326
|
+
throw new Error(
|
|
327
|
+
`liteTopic length exceeded max length ${maxLen}, liteTopic: ${liteTopic}`,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Check lite subscription quota.
|
|
334
|
+
*/
|
|
335
|
+
private checkLiteSubscriptionQuota(delta: number) {
|
|
336
|
+
if (this.liteTopicSet.size + delta > this.liteSubscriptionQuota) {
|
|
337
|
+
throw new ClientException(
|
|
338
|
+
429,
|
|
339
|
+
`Lite subscription quota exceeded ${this.liteSubscriptionQuota}`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Returns a string representation of this LiteSubscriptionManager.
|
|
346
|
+
*
|
|
347
|
+
* @return String representation
|
|
348
|
+
*/
|
|
349
|
+
toString(): string {
|
|
350
|
+
return `LiteSubscriptionManager{bindTopic=${this.bindTopic.getName()}, group=${this.group.getName()}, liteTopicSet=[${Array.from(this.liteTopicSet).join(', ')}], liteSubscriptionQuota=${this.liteSubscriptionQuota}, maxLiteTopicSize=${this.maxLiteTopicSize}}`;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { MessageView } from '../message';
|
|
19
|
+
import { ConsumeResult } from './ConsumeResult';
|
|
20
|
+
|
|
21
|
+
export interface MessageListener {
|
|
22
|
+
consume(messageView: MessageView): ConsumeResult | Promise<ConsumeResult>;
|
|
23
|
+
}
|