rocketmq-client-nodejs-bate 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +186 -0
- package/dist/client/BaseClient.d.ts +95 -0
- package/dist/client/BaseClient.js +324 -0
- package/dist/client/ClientId.d.ts +24 -0
- package/dist/client/ClientId.js +33 -0
- package/dist/client/Logger.d.ts +25 -0
- package/dist/client/Logger.js +33 -0
- package/dist/client/RpcClient.d.ts +48 -0
- package/dist/client/RpcClient.js +219 -0
- package/dist/client/RpcClientManager.d.ts +42 -0
- package/dist/client/RpcClientManager.js +144 -0
- package/dist/client/SessionCredentials.d.ts +21 -0
- package/dist/client/SessionCredentials.js +19 -0
- package/dist/client/Settings.d.ts +31 -0
- package/dist/client/Settings.js +40 -0
- package/dist/client/TelemetrySession.d.ts +27 -0
- package/dist/client/TelemetrySession.js +110 -0
- package/dist/client/UserAgent.d.ts +25 -0
- package/dist/client/UserAgent.js +47 -0
- package/dist/client/index.d.ts +26 -0
- package/dist/client/index.js +43 -0
- package/dist/consumer/Assignment.d.ts +23 -0
- package/dist/consumer/Assignment.js +40 -0
- package/dist/consumer/Assignments.d.ts +24 -0
- package/dist/consumer/Assignments.js +47 -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 +49 -0
- package/dist/consumer/ConsumeTask.d.ts +24 -0
- package/dist/consumer/ConsumeTask.js +40 -0
- package/dist/consumer/Consumer.d.ts +38 -0
- package/dist/consumer/Consumer.js +111 -0
- package/dist/consumer/FifoConsumeService.d.ts +25 -0
- package/dist/consumer/FifoConsumeService.js +123 -0
- package/dist/consumer/FilterExpression.d.ts +25 -0
- package/dist/consumer/FilterExpression.js +40 -0
- package/dist/consumer/MessageListener.d.ts +21 -0
- package/dist/consumer/MessageListener.js +19 -0
- package/dist/consumer/ProcessQueue.d.ts +39 -0
- package/dist/consumer/ProcessQueue.js +286 -0
- package/dist/consumer/PushConsumer.d.ts +68 -0
- package/dist/consumer/PushConsumer.js +377 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
- package/dist/consumer/PushSubscriptionSettings.js +97 -0
- package/dist/consumer/SimpleConsumer.d.ts +50 -0
- package/dist/consumer/SimpleConsumer.js +122 -0
- package/dist/consumer/SimpleSubscriptionSettings.d.ts +28 -0
- package/dist/consumer/SimpleSubscriptionSettings.js +57 -0
- package/dist/consumer/StandardConsumeService.d.ts +24 -0
- package/dist/consumer/StandardConsumeService.js +42 -0
- package/dist/consumer/SubscriptionLoadBalancer.d.ts +23 -0
- package/dist/consumer/SubscriptionLoadBalancer.js +46 -0
- package/dist/consumer/index.d.ts +32 -0
- package/dist/consumer/index.js +49 -0
- package/dist/exception/BadRequestException.d.ts +20 -0
- package/dist/exception/BadRequestException.js +28 -0
- package/dist/exception/ClientException.d.ts +20 -0
- package/dist/exception/ClientException.js +31 -0
- package/dist/exception/ForbiddenException.d.ts +20 -0
- package/dist/exception/ForbiddenException.js +28 -0
- package/dist/exception/InternalErrorException.d.ts +20 -0
- package/dist/exception/InternalErrorException.js +28 -0
- package/dist/exception/NotFoundException.d.ts +20 -0
- package/dist/exception/NotFoundException.js +28 -0
- package/dist/exception/PayloadTooLargeException.d.ts +20 -0
- package/dist/exception/PayloadTooLargeException.js +28 -0
- package/dist/exception/PaymentRequiredException.d.ts +20 -0
- package/dist/exception/PaymentRequiredException.js +28 -0
- package/dist/exception/ProxyTimeoutException.d.ts +20 -0
- package/dist/exception/ProxyTimeoutException.js +28 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.d.ts +20 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.js +28 -0
- package/dist/exception/StatusChecker.d.ts +20 -0
- package/dist/exception/StatusChecker.js +98 -0
- package/dist/exception/TooManyRequestsException.d.ts +20 -0
- package/dist/exception/TooManyRequestsException.js +28 -0
- package/dist/exception/UnauthorizedException.d.ts +20 -0
- package/dist/exception/UnauthorizedException.js +28 -0
- package/dist/exception/UnsupportedException.d.ts +20 -0
- package/dist/exception/UnsupportedException.js +28 -0
- package/dist/exception/index.d.ts +29 -0
- package/dist/exception/index.js +46 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +39 -0
- package/dist/message/Message.d.ts +38 -0
- package/dist/message/Message.js +57 -0
- package/dist/message/MessageId.d.ts +59 -0
- package/dist/message/MessageId.js +123 -0
- package/dist/message/MessageView.d.ts +38 -0
- package/dist/message/MessageView.js +90 -0
- package/dist/message/PublishingMessage.d.ts +30 -0
- package/dist/message/PublishingMessage.js +100 -0
- package/dist/message/index.d.ts +20 -0
- package/dist/message/index.js +37 -0
- package/dist/producer/Producer.d.ts +55 -0
- package/dist/producer/Producer.js +318 -0
- package/dist/producer/PublishingLoadBalancer.d.ts +24 -0
- package/dist/producer/PublishingLoadBalancer.js +82 -0
- package/dist/producer/PublishingSettings.d.ts +28 -0
- package/dist/producer/PublishingSettings.js +70 -0
- package/dist/producer/RecallReceipt.d.ts +25 -0
- package/dist/producer/RecallReceipt.js +34 -0
- package/dist/producer/SendReceipt.d.ts +29 -0
- package/dist/producer/SendReceipt.js +60 -0
- package/dist/producer/Transaction.d.ts +28 -0
- package/dist/producer/Transaction.js +70 -0
- package/dist/producer/TransactionChecker.d.ts +21 -0
- package/dist/producer/TransactionChecker.js +19 -0
- package/dist/producer/index.d.ts +22 -0
- package/dist/producer/index.js +40 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.d.ts +27 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +64 -0
- package/dist/retry/RetryPolicy.d.ts +46 -0
- package/dist/retry/RetryPolicy.js +19 -0
- package/dist/retry/index.d.ts +18 -0
- package/dist/retry/index.js +35 -0
- package/dist/route/Broker.d.ts +25 -0
- package/dist/route/Broker.js +40 -0
- package/dist/route/Endpoints.d.ts +30 -0
- package/dist/route/Endpoints.js +71 -0
- package/dist/route/MessageQueue.d.ts +27 -0
- package/dist/route/MessageQueue.js +47 -0
- package/dist/route/TopicRouteData.d.ts +24 -0
- package/dist/route/TopicRouteData.js +37 -0
- package/dist/route/index.d.ts +20 -0
- package/dist/route/index.js +37 -0
- package/dist/util/index.d.ts +35 -0
- package/dist/util/index.js +88 -0
- package/package.json +62 -0
- package/proto/apache/rocketmq/v2/admin.proto +43 -0
- package/proto/apache/rocketmq/v2/admin_grpc_pb.d.ts +41 -0
- package/proto/apache/rocketmq/v2/admin_grpc_pb.js +60 -0
- package/proto/apache/rocketmq/v2/admin_pb.d.ts +56 -0
- package/proto/apache/rocketmq/v2/admin_pb.js +340 -0
- package/proto/apache/rocketmq/v2/definition.proto +570 -0
- package/proto/apache/rocketmq/v2/definition_grpc_pb.js +1 -0
- package/proto/apache/rocketmq/v2/definition_pb.d.ts +885 -0
- package/proto/apache/rocketmq/v2/definition_pb.js +6141 -0
- package/proto/apache/rocketmq/v2/service.proto +443 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +294 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.js +637 -0
- package/proto/apache/rocketmq/v2/service_pb.d.ts +1249 -0
- package/proto/apache/rocketmq/v2/service_pb.js +9723 -0
- package/src/client/BaseClient.ts +404 -0
- package/src/client/ClientId.ts +31 -0
- package/src/client/Logger.ts +36 -0
- package/src/client/RpcClient.ts +258 -0
- package/src/client/RpcClientManager.ts +180 -0
- package/src/client/SessionCredentials.ts +22 -0
- package/src/client/Settings.ts +46 -0
- package/src/client/TelemetrySession.ts +130 -0
- package/src/client/UserAgent.ts +45 -0
- package/src/client/index.ts +27 -0
- package/src/consumer/Assignment.ts +39 -0
- package/src/consumer/Assignments.ts +46 -0
- package/src/consumer/ConsumeResult.ts +21 -0
- package/src/consumer/ConsumeService.ts +54 -0
- package/src/consumer/ConsumeTask.ts +40 -0
- package/src/consumer/Consumer.ts +129 -0
- package/src/consumer/FifoConsumeService.ts +136 -0
- package/src/consumer/FilterExpression.ts +42 -0
- package/src/consumer/MessageListener.ts +23 -0
- package/src/consumer/ProcessQueue.ts +326 -0
- package/src/consumer/PushConsumer.ts +473 -0
- package/src/consumer/PushSubscriptionSettings.ts +123 -0
- package/src/consumer/SimpleConsumer.ts +153 -0
- package/src/consumer/SimpleSubscriptionSettings.ts +64 -0
- package/src/consumer/StandardConsumeService.ts +44 -0
- package/src/consumer/SubscriptionLoadBalancer.ts +47 -0
- package/src/consumer/index.ts +33 -0
- package/src/exception/BadRequestException.ts +25 -0
- package/src/exception/ClientException.ts +29 -0
- package/src/exception/ForbiddenException.ts +25 -0
- package/src/exception/InternalErrorException.ts +25 -0
- package/src/exception/NotFoundException.ts +25 -0
- package/src/exception/PayloadTooLargeException.ts +25 -0
- package/src/exception/PaymentRequiredException.ts +25 -0
- package/src/exception/ProxyTimeoutException.ts +25 -0
- package/src/exception/RequestHeaderFieldsTooLargeException.ts +25 -0
- package/src/exception/StatusChecker.ts +94 -0
- package/src/exception/TooManyRequestsException.ts +25 -0
- package/src/exception/UnauthorizedException.ts +25 -0
- package/src/exception/UnsupportedException.ts +25 -0
- package/src/exception/index.ts +30 -0
- package/src/index.ts +23 -0
- package/src/message/Message.ts +67 -0
- package/src/message/MessageId.ts +123 -0
- package/src/message/MessageView.ts +94 -0
- package/src/message/PublishingMessage.ts +104 -0
- package/src/message/index.ts +21 -0
- package/src/producer/Producer.ts +388 -0
- package/src/producer/PublishingLoadBalancer.ts +85 -0
- package/src/producer/PublishingSettings.ts +78 -0
- package/src/producer/RecallReceipt.ts +32 -0
- package/src/producer/SendReceipt.ts +63 -0
- package/src/producer/Transaction.ts +86 -0
- package/src/producer/TransactionChecker.ts +23 -0
- package/src/producer/index.ts +24 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +76 -0
- package/src/retry/RetryPolicy.ts +51 -0
- package/src/retry/index.ts +19 -0
- package/src/route/Broker.ts +39 -0
- package/src/route/Endpoints.ts +70 -0
- package/src/route/MessageQueue.ts +49 -0
- package/src/route/TopicRouteData.ts +38 -0
- package/src/route/index.ts +21 -0
- package/src/util/index.ts +83 -0
|
@@ -0,0 +1,86 @@
|
|
|
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 { TransactionResolution } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { MessageOptions, PublishingMessage } from '../message';
|
|
20
|
+
import type { Producer } from './Producer';
|
|
21
|
+
import { SendReceipt } from './SendReceipt';
|
|
22
|
+
|
|
23
|
+
export class Transaction {
|
|
24
|
+
static readonly MAX_MESSAGE_NUM = 1;
|
|
25
|
+
readonly #producer: Producer;
|
|
26
|
+
readonly #messageMap = new Map<string, PublishingMessage>();
|
|
27
|
+
readonly #messageSendReceiptMap = new Map<string, SendReceipt>();
|
|
28
|
+
|
|
29
|
+
constructor(producer: Producer) {
|
|
30
|
+
this.#producer = producer;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
tryAddMessage(message: MessageOptions) {
|
|
34
|
+
if (this.#messageMap.size >= Transaction.MAX_MESSAGE_NUM) {
|
|
35
|
+
throw new TypeError(`Message in transaction has exceeded the threshold=${Transaction.MAX_MESSAGE_NUM}`);
|
|
36
|
+
}
|
|
37
|
+
const publishingMessage = new PublishingMessage(message, this.#producer.publishingSettings, true);
|
|
38
|
+
this.#messageMap.set(publishingMessage.messageId, publishingMessage);
|
|
39
|
+
return publishingMessage;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
tryAddReceipt(publishingMessage: PublishingMessage, sendReceipt: SendReceipt) {
|
|
43
|
+
if (!this.#messageMap.has(publishingMessage.messageId)) {
|
|
44
|
+
throw new TypeError('Message not in transaction');
|
|
45
|
+
}
|
|
46
|
+
this.#messageSendReceiptMap.set(publishingMessage.messageId, sendReceipt);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async commit() {
|
|
50
|
+
if (this.#messageSendReceiptMap.size === 0) {
|
|
51
|
+
throw new TypeError('Transactional message has not been sent yet');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const logger = (this.#producer as any).logger;
|
|
55
|
+
logger.info('Begin to commit transaction, messageCount=%d, clientId=%s',
|
|
56
|
+
this.#messageSendReceiptMap.size, (this.#producer as any).clientId);
|
|
57
|
+
|
|
58
|
+
for (const [ messageId, sendReceipt ] of this.#messageSendReceiptMap.entries()) {
|
|
59
|
+
const publishingMessage = this.#messageMap.get(messageId)!;
|
|
60
|
+
await this.#producer.endTransaction(sendReceipt.endpoints, publishingMessage,
|
|
61
|
+
sendReceipt.messageId, sendReceipt.transactionId, TransactionResolution.COMMIT);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
logger.info('Commit transaction successfully, messageCount=%d, clientId=%s',
|
|
65
|
+
this.#messageSendReceiptMap.size, (this.#producer as any).clientId);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async rollback() {
|
|
69
|
+
if (this.#messageSendReceiptMap.size === 0) {
|
|
70
|
+
throw new TypeError('Transactional message has not been sent yet');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const logger = (this.#producer as any).logger;
|
|
74
|
+
logger.info('Begin to rollback transaction, messageCount=%d, clientId=%s',
|
|
75
|
+
this.#messageSendReceiptMap.size, (this.#producer as any).clientId);
|
|
76
|
+
|
|
77
|
+
for (const [ messageId, sendReceipt ] of this.#messageSendReceiptMap.entries()) {
|
|
78
|
+
const publishingMessage = this.#messageMap.get(messageId)!;
|
|
79
|
+
await this.#producer.endTransaction(sendReceipt.endpoints, publishingMessage,
|
|
80
|
+
sendReceipt.messageId, sendReceipt.transactionId, TransactionResolution.ROLLBACK);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
logger.info('Rollback transaction successfully, messageCount=%d, clientId=%s',
|
|
84
|
+
this.#messageSendReceiptMap.size, (this.#producer as any).clientId);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -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 { TransactionResolution } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { MessageView } from '../message';
|
|
20
|
+
|
|
21
|
+
export interface TransactionChecker {
|
|
22
|
+
check(messageView: MessageView): Promise<TransactionResolution>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
export * from './Producer';
|
|
19
|
+
export * from './PublishingLoadBalancer';
|
|
20
|
+
export * from './PublishingSettings';
|
|
21
|
+
export * from './RecallReceipt';
|
|
22
|
+
export * from './SendReceipt';
|
|
23
|
+
// export * from './Transaction';
|
|
24
|
+
export * from './TransactionChecker';
|
|
@@ -0,0 +1,76 @@
|
|
|
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 assert = require('node:assert');
|
|
19
|
+
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
|
|
20
|
+
import {
|
|
21
|
+
RetryPolicy as RetryPolicyPB,
|
|
22
|
+
ExponentialBackoff,
|
|
23
|
+
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
24
|
+
import { RetryPolicy } from './RetryPolicy';
|
|
25
|
+
|
|
26
|
+
export class ExponentialBackoffRetryPolicy implements RetryPolicy {
|
|
27
|
+
#maxAttempts: number;
|
|
28
|
+
// seconds
|
|
29
|
+
#initialBackoff: number;
|
|
30
|
+
#maxBackoff: number;
|
|
31
|
+
#backoffMultiplier: number;
|
|
32
|
+
|
|
33
|
+
constructor(maxAttempts: number, initialBackoff = 0, maxBackoff = 0, backoffMultiplier = 1) {
|
|
34
|
+
this.#maxAttempts = maxAttempts;
|
|
35
|
+
this.#initialBackoff = initialBackoff;
|
|
36
|
+
this.#maxBackoff = maxBackoff;
|
|
37
|
+
this.#backoffMultiplier = backoffMultiplier;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static immediatelyRetryPolicy(maxAttempts: number) {
|
|
41
|
+
return new ExponentialBackoffRetryPolicy(maxAttempts, 0, 0, 1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getMaxAttempts(): number {
|
|
45
|
+
return this.#maxAttempts;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getNextAttemptDelay(attempt: number): number {
|
|
49
|
+
assert(attempt > 0, 'attempt must be positive');
|
|
50
|
+
const delay = Math.min(this.#initialBackoff * Math.pow(this.#backoffMultiplier, 1.0 * (attempt - 1)), this.#maxBackoff);
|
|
51
|
+
if (delay <= 0) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
return delay;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
inheritBackoff(retryPolicy: RetryPolicyPB): RetryPolicy {
|
|
58
|
+
assert(retryPolicy.getStrategyCase() === RetryPolicyPB.StrategyCase.EXPONENTIAL_BACKOFF,
|
|
59
|
+
'strategy must be exponential backoff');
|
|
60
|
+
const backoff = retryPolicy.getExponentialBackoff()!.toObject();
|
|
61
|
+
return new ExponentialBackoffRetryPolicy(this.#maxAttempts,
|
|
62
|
+
backoff.initial?.seconds,
|
|
63
|
+
backoff.max?.seconds,
|
|
64
|
+
backoff.multiplier);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
toProtobuf(): RetryPolicyPB {
|
|
68
|
+
return new RetryPolicyPB()
|
|
69
|
+
.setMaxAttempts(this.#maxAttempts)
|
|
70
|
+
.setExponentialBackoff(
|
|
71
|
+
new ExponentialBackoff()
|
|
72
|
+
.setInitial(new Duration().setSeconds(this.#initialBackoff))
|
|
73
|
+
.setMax(new Duration().setSeconds(this.#maxBackoff))
|
|
74
|
+
.setMultiplier(this.#backoffMultiplier));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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 { RetryPolicy as RetryPolicyPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Internal interface for retry policy.
|
|
22
|
+
*/
|
|
23
|
+
export interface RetryPolicy {
|
|
24
|
+
/**
|
|
25
|
+
* Get the max attempt times for retry.
|
|
26
|
+
*
|
|
27
|
+
* @return max attempt times.
|
|
28
|
+
*/
|
|
29
|
+
getMaxAttempts(): number;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get await time after current attempts, the attempt index starts at 1.
|
|
33
|
+
*
|
|
34
|
+
* @param attempt current attempt.
|
|
35
|
+
* @return await time in seconds.
|
|
36
|
+
*/
|
|
37
|
+
getNextAttemptDelay(attempt: number): number;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Update the retry backoff strategy and generate a new one.
|
|
41
|
+
*
|
|
42
|
+
* @param retryPolicy retry policy which contains the backoff strategy.
|
|
43
|
+
* @return the new retry policy.
|
|
44
|
+
*/
|
|
45
|
+
inheritBackoff(retryPolicy: RetryPolicyPB): RetryPolicy;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Convert to RetryPolicyPB
|
|
49
|
+
*/
|
|
50
|
+
toProtobuf(): RetryPolicyPB;
|
|
51
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
export * from './ExponentialBackoffRetryPolicy';
|
|
19
|
+
export * from './RetryPolicy';
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { Broker as BrokerPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { Endpoints } from './Endpoints';
|
|
20
|
+
|
|
21
|
+
export class Broker implements BrokerPB.AsObject {
|
|
22
|
+
name: string;
|
|
23
|
+
id: number;
|
|
24
|
+
endpoints: Endpoints;
|
|
25
|
+
|
|
26
|
+
constructor(broker: BrokerPB.AsObject) {
|
|
27
|
+
this.name = broker.name;
|
|
28
|
+
this.id = broker.id;
|
|
29
|
+
this.endpoints = new Endpoints(broker.endpoints!);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
toProtobuf() {
|
|
33
|
+
const broker = new BrokerPB();
|
|
34
|
+
broker.setName(this.name);
|
|
35
|
+
broker.setId(this.id);
|
|
36
|
+
broker.setEndpoints(this.endpoints.toProtobuf());
|
|
37
|
+
return broker;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { isIPv4, isIPv6 } from 'node:net';
|
|
19
|
+
import { Address, AddressScheme, Endpoints as EndpointsPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
20
|
+
|
|
21
|
+
const DEFAULT_PORT = 80;
|
|
22
|
+
|
|
23
|
+
export class Endpoints {
|
|
24
|
+
readonly addressesList: Address.AsObject[];
|
|
25
|
+
readonly scheme: AddressScheme;
|
|
26
|
+
/**
|
|
27
|
+
* URI path for grpc target, e.g:
|
|
28
|
+
* 127.0.0.1:10911[,127.0.0.2:10912]
|
|
29
|
+
*/
|
|
30
|
+
readonly facade: string;
|
|
31
|
+
|
|
32
|
+
constructor(endpoints: string | EndpointsPB.AsObject) {
|
|
33
|
+
if (typeof endpoints === 'string') {
|
|
34
|
+
const splits = endpoints.split(';');
|
|
35
|
+
this.addressesList = [];
|
|
36
|
+
for (const endpoint of splits) {
|
|
37
|
+
const [ host, port ] = endpoint.split(':');
|
|
38
|
+
if (isIPv4(host)) {
|
|
39
|
+
this.scheme = AddressScheme.IPV4;
|
|
40
|
+
} else if (isIPv6(host)) {
|
|
41
|
+
this.scheme = AddressScheme.IPV6;
|
|
42
|
+
} else {
|
|
43
|
+
this.scheme = AddressScheme.DOMAIN_NAME;
|
|
44
|
+
}
|
|
45
|
+
this.addressesList.push({ host, port: parseInt(port) || DEFAULT_PORT });
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
this.scheme = endpoints.scheme;
|
|
49
|
+
this.addressesList = endpoints.addressesList;
|
|
50
|
+
}
|
|
51
|
+
this.facade = this.addressesList.map(addr => `${addr.host}:${addr.port}`).join(',');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getGrpcTarget() {
|
|
55
|
+
return this.facade;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
toString() {
|
|
59
|
+
return this.facade;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
toProtobuf() {
|
|
63
|
+
const endpoints = new EndpointsPB();
|
|
64
|
+
endpoints.setScheme(this.scheme);
|
|
65
|
+
for (const address of this.addressesList) {
|
|
66
|
+
endpoints.addAddresses().setHost(address.host).setPort(address.port);
|
|
67
|
+
}
|
|
68
|
+
return endpoints;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -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 {
|
|
19
|
+
MessageQueue as MessageQueuePB, MessageType, Permission,
|
|
20
|
+
Resource,
|
|
21
|
+
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
22
|
+
import { createResource } from '../util';
|
|
23
|
+
import { Broker } from './Broker';
|
|
24
|
+
|
|
25
|
+
export class MessageQueue {
|
|
26
|
+
topic: Resource.AsObject;
|
|
27
|
+
queueId: number;
|
|
28
|
+
broker: Broker;
|
|
29
|
+
permission: Permission;
|
|
30
|
+
acceptMessageTypesList: MessageType[];
|
|
31
|
+
|
|
32
|
+
constructor(messageQueue: MessageQueuePB) {
|
|
33
|
+
this.topic = messageQueue.getTopic()!.toObject();
|
|
34
|
+
this.queueId = messageQueue.getId();
|
|
35
|
+
this.permission = messageQueue.getPermission();
|
|
36
|
+
this.acceptMessageTypesList = messageQueue.getAcceptMessageTypesList();
|
|
37
|
+
this.broker = new Broker(messageQueue.getBroker()!.toObject());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
toProtobuf() {
|
|
41
|
+
const messageQueue = new MessageQueuePB();
|
|
42
|
+
messageQueue.setId(this.queueId);
|
|
43
|
+
messageQueue.setTopic(createResource(this.topic.name));
|
|
44
|
+
messageQueue.setBroker(this.broker.toProtobuf());
|
|
45
|
+
messageQueue.setPermission(this.permission);
|
|
46
|
+
messageQueue.setAcceptMessageTypesList(this.acceptMessageTypesList);
|
|
47
|
+
return messageQueue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { MessageQueue as MessageQueuePB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { Endpoints } from './Endpoints';
|
|
20
|
+
import { MessageQueue } from './MessageQueue';
|
|
21
|
+
|
|
22
|
+
export class TopicRouteData {
|
|
23
|
+
readonly messageQueues: MessageQueue[] = [];
|
|
24
|
+
|
|
25
|
+
constructor(messageQueues: MessageQueuePB[]) {
|
|
26
|
+
for (const mq of messageQueues) {
|
|
27
|
+
this.messageQueues.push(new MessageQueue(mq));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getTotalEndpoints() {
|
|
32
|
+
const endpointsMap = new Map<string, Endpoints>();
|
|
33
|
+
for (const mq of this.messageQueues) {
|
|
34
|
+
endpointsMap.set(mq.broker.endpoints.facade, mq.broker.endpoints);
|
|
35
|
+
}
|
|
36
|
+
return Array.from(endpointsMap.values());
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export * from './Broker';
|
|
19
|
+
export * from './Endpoints';
|
|
20
|
+
export * from './MessageQueue';
|
|
21
|
+
export * from './TopicRouteData';
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { performance } from 'node:perf_hooks';
|
|
19
|
+
import { createHash, createHmac } from 'node:crypto';
|
|
20
|
+
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
|
|
21
|
+
import { crc32 } from '@node-rs/crc32';
|
|
22
|
+
import siphash24 from 'siphash24';
|
|
23
|
+
import { Resource } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
24
|
+
|
|
25
|
+
export const MASTER_BROKER_ID = 0;
|
|
26
|
+
|
|
27
|
+
export function getTimestamp() {
|
|
28
|
+
const timestamp = performance.timeOrigin + performance.now();
|
|
29
|
+
const seconds = Math.floor(timestamp / 1000);
|
|
30
|
+
const nanos = Math.floor((timestamp % 1000) * 1e6);
|
|
31
|
+
return { seconds, nanos, timestamp };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmss'Z'"
|
|
35
|
+
export function getRequestDateTime() {
|
|
36
|
+
// 2023-09-13T06:30:59.399Z => 20230913T063059Z
|
|
37
|
+
const now = new Date().toISOString().split('.')[0].replace(/[\-\:]/g, '');
|
|
38
|
+
return `${now}Z`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function sign(accessSecret: string, dateTime: string) {
|
|
42
|
+
const hmacSha1 = createHmac('sha1', accessSecret);
|
|
43
|
+
hmacSha1.update(dateTime);
|
|
44
|
+
return hmacSha1.digest('hex').toUpperCase();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createDuration(ms: number) {
|
|
48
|
+
const nanos = ms % 1000 * 1000000;
|
|
49
|
+
return new Duration()
|
|
50
|
+
.setSeconds(ms / 1000)
|
|
51
|
+
.setNanos(nanos);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createResource(name: string) {
|
|
55
|
+
return new Resource().setName(name);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function crc32CheckSum(bytes: Buffer) {
|
|
59
|
+
return `${crc32(bytes)}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function md5CheckSum(bytes: Uint8Array) {
|
|
63
|
+
return createHash('md5').update(bytes).digest('hex')
|
|
64
|
+
.toUpperCase();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function sha1CheckSum(bytes: Uint8Array) {
|
|
68
|
+
return createHash('sha1').update(bytes).digest('hex')
|
|
69
|
+
.toUpperCase();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// k0: 0x0706050403020100L, k1: 0x0f0e0d0c0b0a0908L
|
|
73
|
+
const SIP_HASH_24_KEY = Buffer.from([
|
|
74
|
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
75
|
+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
76
|
+
]);
|
|
77
|
+
/**
|
|
78
|
+
* Java: Hashing.sipHash24().hashBytes(messageGroup.getBytes(StandardCharsets.UTF_8)).asLong()
|
|
79
|
+
*/
|
|
80
|
+
export function calculateStringSipHash24(value: string) {
|
|
81
|
+
const hash = siphash24(Buffer.from(value), SIP_HASH_24_KEY);
|
|
82
|
+
return Buffer.from(hash).readBigUInt64BE();
|
|
83
|
+
}
|