rocketmq-client-nodejs 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/README.md +84 -1
  2. package/dist/client/BaseClient.d.ts +24 -2
  3. package/dist/client/BaseClient.js +173 -28
  4. package/dist/client/ClientId.js +2 -2
  5. package/dist/client/Logger.d.ts +1 -0
  6. package/dist/client/Logger.js +1 -1
  7. package/dist/client/RpcClient.d.ts +3 -1
  8. package/dist/client/RpcClient.js +23 -1
  9. package/dist/client/RpcClientManager.d.ts +3 -1
  10. package/dist/client/RpcClientManager.js +23 -4
  11. package/dist/client/TelemetrySession.d.ts +1 -0
  12. package/dist/client/TelemetrySession.js +47 -9
  13. package/dist/consumer/Assignment.d.ts +24 -0
  14. package/dist/consumer/Assignment.js +40 -0
  15. package/dist/consumer/Assignments.d.ts +25 -0
  16. package/dist/consumer/Assignments.js +55 -0
  17. package/dist/consumer/ConsumeResult.d.ts +20 -0
  18. package/dist/consumer/ConsumeResult.js +25 -0
  19. package/dist/consumer/ConsumeService.d.ts +28 -0
  20. package/dist/consumer/ConsumeService.js +53 -0
  21. package/dist/consumer/ConsumeTask.d.ts +24 -0
  22. package/dist/consumer/ConsumeTask.js +39 -0
  23. package/dist/consumer/Consumer.d.ts +19 -1
  24. package/dist/consumer/Consumer.js +64 -21
  25. package/dist/consumer/FifoConsumeService.d.ts +25 -0
  26. package/dist/consumer/FifoConsumeService.js +130 -0
  27. package/dist/consumer/FilterExpression.d.ts +2 -0
  28. package/dist/consumer/FilterExpression.js +18 -1
  29. package/dist/consumer/LitePushConsumer.d.ts +161 -0
  30. package/dist/consumer/LitePushConsumer.js +172 -0
  31. package/dist/consumer/LitePushConsumerImpl.d.ts +167 -0
  32. package/dist/consumer/LitePushConsumerImpl.js +193 -0
  33. package/dist/consumer/LiteSubscriptionManager.d.ts +97 -0
  34. package/dist/consumer/LiteSubscriptionManager.js +238 -0
  35. package/dist/consumer/MessageListener.d.ts +21 -0
  36. package/dist/consumer/MessageListener.js +19 -0
  37. package/dist/consumer/OffsetOption.d.ts +109 -0
  38. package/dist/consumer/OffsetOption.js +191 -0
  39. package/dist/consumer/ProcessQueue.d.ts +39 -0
  40. package/dist/consumer/ProcessQueue.js +293 -0
  41. package/dist/consumer/PushConsumer.d.ts +88 -0
  42. package/dist/consumer/PushConsumer.js +449 -0
  43. package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
  44. package/dist/consumer/PushSubscriptionSettings.js +97 -0
  45. package/dist/consumer/SimpleConsumer.d.ts +12 -1
  46. package/dist/consumer/SimpleConsumer.js +54 -10
  47. package/dist/consumer/SimpleSubscriptionSettings.d.ts +2 -2
  48. package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
  49. package/dist/consumer/StandardConsumeService.d.ts +25 -0
  50. package/dist/consumer/StandardConsumeService.js +50 -0
  51. package/dist/consumer/index.d.ts +15 -0
  52. package/dist/consumer/index.js +19 -1
  53. package/dist/exception/InternalErrorException.d.ts +5 -0
  54. package/dist/exception/InternalErrorException.js +10 -1
  55. package/dist/exception/LiteSubscriptionQuotaExceededException.d.ts +23 -0
  56. package/dist/exception/LiteSubscriptionQuotaExceededException.js +31 -0
  57. package/dist/exception/LiteTopicQuotaExceededException.d.ts +23 -0
  58. package/dist/exception/LiteTopicQuotaExceededException.js +31 -0
  59. package/dist/exception/StatusChecker.js +4 -1
  60. package/dist/exception/index.d.ts +2 -0
  61. package/dist/exception/index.js +3 -1
  62. package/dist/index.d.ts +1 -0
  63. package/dist/index.js +2 -1
  64. package/dist/message/Message.d.ts +8 -0
  65. package/dist/message/Message.js +39 -6
  66. package/dist/message/MessageId.d.ts +2 -0
  67. package/dist/message/MessageId.js +17 -1
  68. package/dist/message/MessageView.d.ts +86 -0
  69. package/dist/message/MessageView.js +115 -1
  70. package/dist/message/PublishingMessage.js +21 -5
  71. package/dist/producer/Producer.d.ts +21 -2
  72. package/dist/producer/Producer.js +120 -25
  73. package/dist/producer/PublishingLoadBalancer.d.ts +2 -0
  74. package/dist/producer/PublishingLoadBalancer.js +23 -1
  75. package/dist/producer/RecallReceipt.d.ts +25 -0
  76. package/dist/producer/RecallReceipt.js +34 -0
  77. package/dist/producer/SendReceipt.d.ts +2 -1
  78. package/dist/producer/SendReceipt.js +6 -3
  79. package/dist/producer/Transaction.js +7 -1
  80. package/dist/producer/index.d.ts +1 -0
  81. package/dist/producer/index.js +2 -1
  82. package/dist/route/Broker.d.ts +2 -0
  83. package/dist/route/Broker.js +21 -1
  84. package/dist/route/Endpoints.d.ts +2 -0
  85. package/dist/route/Endpoints.js +36 -1
  86. package/dist/route/MessageQueue.d.ts +2 -0
  87. package/dist/route/MessageQueue.js +23 -1
  88. package/dist/route/Resource.d.ts +54 -0
  89. package/dist/route/Resource.js +100 -0
  90. package/dist/route/TopicRouteData.d.ts +12 -0
  91. package/dist/route/TopicRouteData.js +37 -1
  92. package/dist/route/index.d.ts +1 -0
  93. package/dist/route/index.js +2 -1
  94. package/dist/util/index.d.ts +8 -0
  95. package/dist/util/index.js +20 -1
  96. package/package.json +2 -2
  97. package/proto/apache/rocketmq/v2/definition.proto +56 -0
  98. package/proto/apache/rocketmq/v2/definition_pb.d.ts +100 -0
  99. package/proto/apache/rocketmq/v2/definition_pb.js +570 -4
  100. package/proto/apache/rocketmq/v2/service.proto +65 -0
  101. package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +34 -0
  102. package/proto/apache/rocketmq/v2/service_grpc_pb.js +81 -4
  103. package/proto/apache/rocketmq/v2/service_pb.d.ts +211 -0
  104. package/proto/apache/rocketmq/v2/service_pb.js +1691 -25
  105. package/src/client/BaseClient.ts +196 -28
  106. package/src/client/ClientId.ts +1 -1
  107. package/src/client/Logger.ts +1 -0
  108. package/src/client/RpcClient.ts +26 -0
  109. package/src/client/RpcClientManager.ts +27 -3
  110. package/src/client/TelemetrySession.ts +54 -8
  111. package/src/consumer/Assignment.ts +40 -0
  112. package/src/consumer/Assignments.ts +55 -0
  113. package/src/consumer/ConsumeResult.ts +21 -0
  114. package/src/consumer/ConsumeService.ts +72 -0
  115. package/src/consumer/ConsumeTask.ts +39 -0
  116. package/src/consumer/Consumer.ts +76 -20
  117. package/src/consumer/FifoConsumeService.ts +146 -0
  118. package/src/consumer/FilterExpression.ts +17 -0
  119. package/src/consumer/LitePushConsumer.ts +256 -0
  120. package/src/consumer/LitePushConsumerImpl.ts +245 -0
  121. package/src/consumer/LiteSubscriptionManager.ts +354 -0
  122. package/src/consumer/MessageListener.ts +23 -0
  123. package/src/consumer/OffsetOption.ts +203 -0
  124. package/src/consumer/ProcessQueue.ts +339 -0
  125. package/src/consumer/PushConsumer.ts +563 -0
  126. package/src/consumer/PushSubscriptionSettings.ts +124 -0
  127. package/src/consumer/SimpleConsumer.ts +68 -11
  128. package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
  129. package/src/consumer/StandardConsumeService.ts +57 -0
  130. package/src/consumer/index.ts +15 -0
  131. package/src/exception/InternalErrorException.ts +10 -0
  132. package/src/exception/LiteSubscriptionQuotaExceededException.ts +28 -0
  133. package/src/exception/LiteTopicQuotaExceededException.ts +28 -0
  134. package/src/exception/StatusChecker.ts +3 -0
  135. package/src/exception/index.ts +2 -0
  136. package/src/index.ts +1 -0
  137. package/src/message/Message.ts +46 -5
  138. package/src/message/MessageId.ts +16 -0
  139. package/src/message/MessageView.ts +127 -0
  140. package/src/message/PublishingMessage.ts +20 -4
  141. package/src/producer/Producer.ts +144 -24
  142. package/src/producer/PublishingLoadBalancer.ts +21 -0
  143. package/src/producer/RecallReceipt.ts +32 -0
  144. package/src/producer/SendReceipt.ts +5 -2
  145. package/src/producer/Transaction.ts +16 -0
  146. package/src/producer/index.ts +1 -0
  147. package/src/route/Broker.ts +20 -0
  148. package/src/route/Endpoints.ts +32 -0
  149. package/src/route/MessageQueue.ts +23 -1
  150. package/src/route/Resource.ts +104 -0
  151. package/src/route/TopicRouteData.ts +37 -0
  152. package/src/route/index.ts +1 -0
  153. 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,354 @@
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
+ // syncAll after startup for subscribeLite("*")
66
+ this.syncAllLiteSubscription();
67
+ this.syncTimer = setInterval(
68
+ () => this.syncAllLiteSubscription(),
69
+ SYNC_LITE_SUBSCRIPTION_INTERVAL,
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Stop the sync timer.
75
+ */
76
+ public shutdown() {
77
+ // Clear sync timer
78
+ if (this.syncTimer) {
79
+ clearInterval(this.syncTimer);
80
+ this.syncTimer = undefined;
81
+ }
82
+ // Clear lite topic set to free memory
83
+ this.liteTopicSet.clear();
84
+ }
85
+
86
+ /**
87
+ * Get the bind topic name.
88
+ */
89
+ public getBindTopicName(): string {
90
+ return this.bindTopic.getName();
91
+ }
92
+
93
+ /**
94
+ * Get the consumer group name.
95
+ */
96
+ public getConsumerGroupName(): string {
97
+ return this.group.getName();
98
+ }
99
+
100
+ /**
101
+ * Get a copy of the lite topic set.
102
+ */
103
+ public getLiteTopicSet(): Set<string> {
104
+ return new Set(this.liteTopicSet);
105
+ }
106
+
107
+ /**
108
+ * Sync settings from server.
109
+ */
110
+ public sync(settings: any) {
111
+ if (!settings?.hasSubscription?.()) {
112
+ return;
113
+ }
114
+
115
+ const subscription = settings.getSubscription();
116
+ if (subscription.hasLiteSubscriptionQuota?.()) {
117
+ this.liteSubscriptionQuota = subscription.getLiteSubscriptionQuota();
118
+ }
119
+ if (subscription.hasMaxLiteTopicSize?.()) {
120
+ this.maxLiteTopicSize = subscription.getMaxLiteTopicSize();
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Subscribe to a lite topic.
126
+ */
127
+ public async subscribeLite(
128
+ liteTopic: string,
129
+ offsetOption?: OffsetOption | null,
130
+ ): Promise<void> {
131
+ // Check if consumer is running
132
+ if (!this.consumerImpl.isRunning()) {
133
+ throw new ClientException(500, 'Consumer is not running');
134
+ }
135
+
136
+ // Already subscribed
137
+ if (this.liteTopicSet.has(liteTopic)) {
138
+ return;
139
+ }
140
+
141
+ // Validate lite topic
142
+ this.validateLiteTopic(liteTopic);
143
+
144
+ // Check quota
145
+ this.checkLiteSubscriptionQuota(1);
146
+
147
+ try {
148
+ await this.syncLiteSubscription(
149
+ LiteSubscriptionAction.PARTIAL_ADD,
150
+ [ liteTopic ],
151
+ offsetOption ?? null,
152
+ );
153
+
154
+ this.liteTopicSet.add(liteTopic);
155
+ this.consumerImpl.getLogger().info(
156
+ 'SubscribeLite %s, topic=%s, group=%s, clientId=%s',
157
+ liteTopic,
158
+ this.getBindTopicName(),
159
+ this.getConsumerGroupName(),
160
+ this.consumerImpl.clientId,
161
+ );
162
+ } catch (error) {
163
+ this.consumerImpl.getLogger().error(
164
+ 'Failed to subscribeLite %s, error=%s',
165
+ liteTopic,
166
+ error,
167
+ );
168
+ throw error;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Unsubscribe from a lite topic.
174
+ */
175
+ public async unsubscribeLite(liteTopic: string): Promise<void> {
176
+ // Check if consumer is running
177
+ if (!this.consumerImpl.isRunning()) {
178
+ throw new ClientException(500, 'Consumer is not running');
179
+ }
180
+
181
+ // Not subscribed
182
+ if (!this.liteTopicSet.has(liteTopic)) {
183
+ return;
184
+ }
185
+
186
+ try {
187
+ await this.syncLiteSubscription(
188
+ LiteSubscriptionAction.PARTIAL_REMOVE,
189
+ [ liteTopic ],
190
+ null,
191
+ );
192
+
193
+ this.liteTopicSet.delete(liteTopic);
194
+ this.consumerImpl.getLogger().info(
195
+ 'UnsubscribeLite %s, topic=%s, group=%s, clientId=%s',
196
+ liteTopic,
197
+ this.getBindTopicName(),
198
+ this.getConsumerGroupName(),
199
+ this.consumerImpl.clientId,
200
+ );
201
+ } catch (error) {
202
+ this.consumerImpl.getLogger().error(
203
+ 'Failed to unsubscribeLite %s, error=%s',
204
+ liteTopic,
205
+ error,
206
+ );
207
+ throw error;
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Periodic sync of all lite subscriptions.
213
+ */
214
+ private async syncAllLiteSubscription() {
215
+ try {
216
+ this.checkLiteSubscriptionQuota(0);
217
+ await this.syncLiteSubscription(
218
+ LiteSubscriptionAction.COMPLETE_ADD,
219
+ Array.from(this.liteTopicSet),
220
+ null,
221
+ );
222
+ } catch (error) {
223
+ this.consumerImpl.getLogger().error(
224
+ 'Schedule syncAllLiteSubscription error, clientId=%s, error=%s',
225
+ this.consumerImpl.clientId,
226
+ error,
227
+ );
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Sync lite subscription with server.
233
+ */
234
+ private async syncLiteSubscription(
235
+ action: LiteSubscriptionAction,
236
+ liteTopics: string[],
237
+ offsetOption: OffsetOption | null,
238
+ ): Promise<void> {
239
+ const logger = this.consumerImpl.getLogger();
240
+ if (logger.debug) {
241
+ logger.debug(
242
+ 'SyncLiteSubscription: action=%s, liteTopics=[%s], offsetOption=%s',
243
+ LiteSubscriptionAction[action],
244
+ liteTopics.join(', '),
245
+ offsetOption?.toString() ?? 'null',
246
+ );
247
+ }
248
+
249
+ // Build the request
250
+ const request = new SyncLiteSubscriptionRequest()
251
+ .setAction(action)
252
+ .setTopic(this.bindTopic.toProtobuf())
253
+ .setGroup(this.group.toProtobuf())
254
+ .setLiteTopicSetList(liteTopics);
255
+
256
+ // Set offset option if provided
257
+ if (offsetOption) {
258
+ request.setOffsetOption(offsetOption.toProtobuf());
259
+ }
260
+
261
+ try {
262
+ // Call RPC client using public methods from LitePushConsumerImpl
263
+ const response: SyncLiteSubscriptionResponse = await this.consumerImpl.getRpcClientManager().syncLiteSubscription(
264
+ this.consumerImpl.getEndpoints(),
265
+ request,
266
+ this.consumerImpl.getRequestTimeout(),
267
+ );
268
+
269
+ // Handle response status
270
+ const status = response.getStatus();
271
+ if (status && status.getCode() !== Code.OK) {
272
+ throw new ClientException(
273
+ status.getCode(),
274
+ `Failed to sync lite subscription: ${status.getMessage()}`,
275
+ );
276
+ }
277
+
278
+ if (logger.info) {
279
+ logger.info(
280
+ 'SyncLiteSubscription success: action=%s, liteTopics=[%s], clientId=%s',
281
+ LiteSubscriptionAction[action],
282
+ liteTopics.join(', '),
283
+ this.consumerImpl.clientId,
284
+ );
285
+ }
286
+ } catch (error) {
287
+ if (logger.error) {
288
+ logger.error(
289
+ 'SyncLiteSubscription failed: action=%s, liteTopics=[%s], clientId=%s, error=%s',
290
+ LiteSubscriptionAction[action],
291
+ liteTopics.join(', '),
292
+ this.consumerImpl.clientId,
293
+ error,
294
+ );
295
+ }
296
+ throw error;
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Handle notify unsubscribe lite command from server.
302
+ */
303
+ public onNotifyUnsubscribeLiteCommand(command: NotifyUnsubscribeLiteCommand) {
304
+ const liteTopic = command.getLiteTopic();
305
+ this.consumerImpl.getLogger().info(
306
+ 'Notify unsubscribe lite: liteTopic=%s, group=%s, bindTopic=%s',
307
+ liteTopic,
308
+ this.getConsumerGroupName(),
309
+ this.getBindTopicName(),
310
+ );
311
+
312
+ if (liteTopic && liteTopic.trim().length > 0) {
313
+ this.liteTopicSet.delete(liteTopic);
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Validate lite topic name.
319
+ */
320
+ private validateLiteTopic(liteTopic: string, maxLength?: number) {
321
+ const maxLen = maxLength ?? this.maxLiteTopicSize;
322
+
323
+ if (!liteTopic || liteTopic.trim().length === 0) {
324
+ throw new Error('liteTopic is blank');
325
+ }
326
+
327
+ if (liteTopic.length > maxLen) {
328
+ throw new Error(
329
+ `liteTopic length exceeded max length ${maxLen}, liteTopic: ${liteTopic}`,
330
+ );
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Check lite subscription quota.
336
+ */
337
+ private checkLiteSubscriptionQuota(delta: number) {
338
+ if (this.liteTopicSet.size + delta > this.liteSubscriptionQuota) {
339
+ throw new ClientException(
340
+ 429,
341
+ `Lite subscription quota exceeded ${this.liteSubscriptionQuota}`,
342
+ );
343
+ }
344
+ }
345
+
346
+ /**
347
+ * Returns a string representation of this LiteSubscriptionManager.
348
+ *
349
+ * @return String representation
350
+ */
351
+ toString(): string {
352
+ return `LiteSubscriptionManager{bindTopic=${this.bindTopic.getName()}, group=${this.group.getName()}, liteTopicSet=[${Array.from(this.liteTopicSet).join(', ')}], liteSubscriptionQuota=${this.liteSubscriptionQuota}, maxLiteTopicSize=${this.maxLiteTopicSize}}`;
353
+ }
354
+ }
@@ -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
+ }