rocketmq-client-nodejs 1.0.8 → 1.1.0
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 +61 -0
- package/dist/client/BaseClient.d.ts +36 -1
- package/dist/client/BaseClient.js +176 -12
- package/dist/client/RpcClient.js +14 -2
- package/dist/client/RpcClientManager.d.ts +5 -0
- package/dist/client/RpcClientManager.js +25 -8
- package/dist/client/TelemetrySession.js +45 -10
- package/dist/consumer/ConsumeService.d.ts +2 -1
- package/dist/consumer/ConsumeService.js +7 -3
- package/dist/consumer/ConsumeTask.d.ts +6 -1
- package/dist/consumer/ConsumeTask.js +36 -5
- package/dist/consumer/Consumer.d.ts +13 -5
- package/dist/consumer/Consumer.js +85 -24
- package/dist/consumer/FifoConsumeService.d.ts +2 -1
- package/dist/consumer/FifoConsumeService.js +3 -3
- package/dist/consumer/LiteFifoConsumeService.d.ts +2 -1
- package/dist/consumer/LiteFifoConsumeService.js +3 -3
- package/dist/consumer/LitePushConsumerImpl.d.ts +12 -1
- package/dist/consumer/LitePushConsumerImpl.js +19 -4
- package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
- package/dist/consumer/LiteSimpleConsumer.js +126 -0
- package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
- package/dist/consumer/LiteSimpleConsumerImpl.js +187 -0
- package/dist/consumer/LiteStandardConsumeService.d.ts +2 -1
- package/dist/consumer/LiteStandardConsumeService.js +3 -3
- package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
- package/dist/consumer/LiteSubscriptionManager.js +32 -24
- package/dist/consumer/ProcessQueue.d.ts +10 -0
- package/dist/consumer/ProcessQueue.js +43 -5
- package/dist/consumer/PushConsumer.d.ts +17 -0
- package/dist/consumer/PushConsumer.js +43 -4
- package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
- package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
- package/dist/consumer/PushSubscriptionSettings.js +33 -6
- package/dist/consumer/SimpleConsumer.js +10 -1
- package/dist/consumer/StandardConsumeService.d.ts +2 -1
- package/dist/consumer/StandardConsumeService.js +3 -3
- package/dist/consumer/index.d.ts +1 -0
- package/dist/consumer/index.js +2 -1
- package/dist/exception/PayloadEmptyException.d.ts +20 -0
- package/dist/exception/PayloadEmptyException.js +28 -0
- package/dist/exception/StatusChecker.js +8 -1
- package/dist/exception/index.d.ts +1 -0
- package/dist/exception/index.js +2 -1
- package/dist/hook/Attribute.d.ts +26 -0
- package/dist/hook/Attribute.js +37 -0
- package/dist/hook/AttributeKey.d.ts +32 -0
- package/dist/hook/AttributeKey.js +43 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +31 -0
- package/dist/hook/CompositedMessageInterceptor.js +84 -0
- package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
- package/dist/hook/InflightRequestCountInterceptor.js +44 -0
- package/dist/hook/MessageHookPoints.d.ts +54 -0
- package/dist/hook/MessageHookPoints.js +59 -0
- package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
- package/dist/hook/MessageHookPointsStatus.js +29 -0
- package/dist/hook/MessageInterceptor.d.ts +34 -0
- package/dist/hook/MessageInterceptor.js +19 -0
- package/dist/hook/MessageInterceptorContext.d.ts +31 -0
- package/dist/hook/MessageInterceptorContext.js +19 -0
- package/dist/hook/MessageInterceptorContextImpl.d.ts +39 -0
- package/dist/hook/MessageInterceptorContextImpl.js +57 -0
- package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
- package/dist/hook/MessageMeterInterceptor.js +149 -0
- package/dist/hook/index.d.ts +26 -0
- package/dist/hook/index.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/metrics/ClientMeter.d.ts +42 -0
- package/dist/metrics/ClientMeter.js +75 -0
- package/dist/metrics/ClientMeterManager.d.ts +42 -0
- package/dist/metrics/ClientMeterManager.js +127 -0
- package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
- package/dist/metrics/EmptyGaugeObserver.js +34 -0
- package/dist/metrics/GaugeEnum.d.ts +32 -0
- package/dist/metrics/GaugeEnum.js +37 -0
- package/dist/metrics/GaugeObserver.d.ts +29 -0
- package/dist/metrics/GaugeObserver.js +19 -0
- package/dist/metrics/HistogramEnum.d.ts +54 -0
- package/dist/metrics/HistogramEnum.js +71 -0
- package/dist/metrics/InvocationStatus.d.ts +26 -0
- package/dist/metrics/InvocationStatus.js +33 -0
- package/dist/metrics/Metric.d.ts +30 -0
- package/dist/metrics/Metric.js +44 -0
- package/dist/metrics/MetricLabels.d.ts +27 -0
- package/dist/metrics/MetricLabels.js +34 -0
- package/dist/metrics/index.d.ts +25 -0
- package/dist/metrics/index.js +42 -0
- package/dist/producer/Producer.js +33 -5
- package/dist/producer/PublishingLoadBalancer.js +9 -3
- package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
- package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
- package/dist/retry/RetryPolicy.d.ts +1 -1
- package/dist/retry/index.d.ts +1 -0
- package/dist/retry/index.js +2 -1
- package/dist/route/Endpoints.d.ts +6 -0
- package/dist/route/Endpoints.js +56 -4
- package/dist/route/IpNameResolver.d.ts +28 -0
- package/dist/route/IpNameResolver.js +104 -0
- package/dist/util/Semaphore.d.ts +47 -0
- package/dist/util/Semaphore.js +82 -0
- package/package.json +6 -2
- package/src/client/BaseClient.ts +202 -12
- package/src/client/RpcClient.ts +14 -1
- package/src/client/RpcClientManager.ts +27 -8
- package/src/client/TelemetrySession.ts +47 -9
- package/src/consumer/ConsumeService.ts +9 -2
- package/src/consumer/ConsumeTask.ts +43 -3
- package/src/consumer/Consumer.ts +103 -23
- package/src/consumer/FifoConsumeService.ts +4 -2
- package/src/consumer/LiteFifoConsumeService.ts +4 -2
- package/src/consumer/LitePushConsumerImpl.ts +21 -3
- package/src/consumer/LiteSimpleConsumer.ts +251 -0
- package/src/consumer/LiteSimpleConsumerImpl.ts +221 -0
- package/src/consumer/LiteStandardConsumeService.ts +4 -2
- package/src/consumer/LiteSubscriptionManager.ts +63 -36
- package/src/consumer/ProcessQueue.ts +44 -1
- package/src/consumer/PushConsumer.ts +54 -2
- package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
- package/src/consumer/PushSubscriptionSettings.ts +38 -7
- package/src/consumer/SimpleConsumer.ts +9 -0
- package/src/consumer/StandardConsumeService.ts +4 -2
- package/src/consumer/index.ts +1 -0
- package/src/exception/PayloadEmptyException.ts +25 -0
- package/src/exception/StatusChecker.ts +7 -0
- package/src/exception/index.ts +1 -0
- package/src/hook/Attribute.ts +36 -0
- package/src/hook/AttributeKey.ts +42 -0
- package/src/hook/CompositedMessageInterceptor.ts +92 -0
- package/src/hook/InflightRequestCountInterceptor.ts +46 -0
- package/src/hook/MessageHookPoints.ts +55 -0
- package/src/hook/MessageHookPointsStatus.ts +25 -0
- package/src/hook/MessageInterceptor.ts +35 -0
- package/src/hook/MessageInterceptorContext.ts +33 -0
- package/src/hook/MessageInterceptorContextImpl.ts +68 -0
- package/src/hook/MessageMeterInterceptor.ts +162 -0
- package/src/hook/index.ts +27 -0
- package/src/index.ts +1 -0
- package/src/metrics/ClientMeter.ts +81 -0
- package/src/metrics/ClientMeterManager.ts +145 -0
- package/src/metrics/EmptyGaugeObserver.ts +36 -0
- package/src/metrics/GaugeEnum.ts +33 -0
- package/src/metrics/GaugeObserver.ts +32 -0
- package/src/metrics/HistogramEnum.ts +69 -0
- package/src/metrics/InvocationStatus.ts +28 -0
- package/src/metrics/Metric.ts +42 -0
- package/src/metrics/MetricLabels.ts +31 -0
- package/src/metrics/index.ts +26 -0
- package/src/producer/Producer.ts +37 -6
- package/src/producer/PublishingLoadBalancer.ts +8 -2
- package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
- package/src/retry/RetryPolicy.ts +1 -1
- package/src/retry/index.ts +1 -0
- package/src/route/Endpoints.ts +52 -3
- package/src/route/IpNameResolver.ts +119 -0
- package/src/util/Semaphore.ts +82 -0
|
@@ -0,0 +1,119 @@
|
|
|
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 { experimental, Metadata, status as Status, ChannelOptions } from '@grpc/grpc-js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Custom gRPC name resolver registered under the `ip` scheme, mirroring the Java
|
|
22
|
+
* client's `IpNameResolverFactory`.
|
|
23
|
+
*
|
|
24
|
+
* The Java resolver converts each socket address into its own
|
|
25
|
+
* `EquivalentAddressGroup` and then {@code Collections.shuffle}s the list, so the
|
|
26
|
+
* gRPC load balancer starts from a random address instead of always the first
|
|
27
|
+
* one. RocketMQ proxies are typically deployed as multiple endpoints behind one
|
|
28
|
+
* `Endpoints`, and shuffling spreads clients across them.
|
|
29
|
+
*
|
|
30
|
+
* grpc-js models an `EquivalentAddressGroup` as a single `Endpoint` carrying one
|
|
31
|
+
* or more equivalent `SubchannelAddress`es, so we emit one `Endpoint` per address
|
|
32
|
+
* and shuffle the array — exactly the Java semantics.
|
|
33
|
+
*/
|
|
34
|
+
const DEFAULT_PORT = 80;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fisher–Yates shuffle (pure, does not mutate the input array).
|
|
38
|
+
*/
|
|
39
|
+
function shuffle<T>(input: readonly T[]): T[] {
|
|
40
|
+
const arr = input.slice();
|
|
41
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
42
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
43
|
+
const tmp = arr[i];
|
|
44
|
+
arr[i] = arr[j];
|
|
45
|
+
arr[j] = tmp;
|
|
46
|
+
}
|
|
47
|
+
return arr;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class IpResolver {
|
|
51
|
+
private readonly listener: experimental.ResolverListener;
|
|
52
|
+
private endpoints: experimental.Endpoint[] = [];
|
|
53
|
+
private error: { code: Status; details: string; metadata: Metadata } | null = null;
|
|
54
|
+
private hasReturnedResult = false;
|
|
55
|
+
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
+
constructor(target: experimental.GrpcUri, listener: experimental.ResolverListener, _channelOptions: ChannelOptions) {
|
|
58
|
+
this.listener = listener;
|
|
59
|
+
const pathList = (target.path || '').split(',').map(s => s.trim()).filter(Boolean);
|
|
60
|
+
const addresses: experimental.SubchannelAddress[] = [];
|
|
61
|
+
for (const path of pathList) {
|
|
62
|
+
const hostPort = experimental.splitHostPort(path);
|
|
63
|
+
if (!hostPort) {
|
|
64
|
+
this.error = {
|
|
65
|
+
code: Status.UNAVAILABLE,
|
|
66
|
+
details: `Failed to parse ip address: ${path}`,
|
|
67
|
+
metadata: new Metadata(),
|
|
68
|
+
};
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
addresses.push({
|
|
72
|
+
host: hostPort.host,
|
|
73
|
+
port: hostPort.port ?? DEFAULT_PORT,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
this.endpoints = shuffle(addresses).map(address => ({ addresses: [ address ] }));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
updateResolution(): void {
|
|
80
|
+
if (this.hasReturnedResult) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.hasReturnedResult = true;
|
|
84
|
+
// Defer the callback to the next tick, mirroring the built-in ip resolver so
|
|
85
|
+
// listeners are never invoked synchronously from the constructor path.
|
|
86
|
+
process.nextTick(() => {
|
|
87
|
+
if (this.error) {
|
|
88
|
+
this.listener(
|
|
89
|
+
experimental.statusOrFromError(this.error),
|
|
90
|
+
{},
|
|
91
|
+
null,
|
|
92
|
+
'',
|
|
93
|
+
);
|
|
94
|
+
} else {
|
|
95
|
+
this.listener(
|
|
96
|
+
experimental.statusOrFromValue(this.endpoints),
|
|
97
|
+
{},
|
|
98
|
+
null,
|
|
99
|
+
'',
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
destroy(): void {
|
|
106
|
+
this.hasReturnedResult = false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
110
|
+
static getDefaultAuthority(target: experimental.GrpcUri): string {
|
|
111
|
+
return (target.path || '').split(',')[0] || 'localhost';
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Register the resolver so that gRPC targets prefixed with `ip:` are handled by
|
|
116
|
+
// this class. Registration is idempotent at module-load time.
|
|
117
|
+
experimental.registerResolver('ip', IpResolver);
|
|
118
|
+
|
|
119
|
+
export { IpResolver };
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* A counting semaphore used to throttle the number of messages that can be
|
|
20
|
+
* in-flight (fetched but not yet settled) on a single {@link ProcessQueue}.
|
|
21
|
+
*
|
|
22
|
+
* This mirrors the permit model of the Java client's
|
|
23
|
+
* {@code ProcessQueueImpl#acquirePermit()}/{@code releasePermit()}, where the
|
|
24
|
+
* permit count is bounded by {@code consumeConcurrentlyMax} and exhaustion
|
|
25
|
+
* applies backpressure by stopping further message fetches until permits are
|
|
26
|
+
* released (i.e. messages are acked / nacked).
|
|
27
|
+
*/
|
|
28
|
+
export class Semaphore {
|
|
29
|
+
#permits: number;
|
|
30
|
+
readonly #waiters: Array<() => void> = [];
|
|
31
|
+
|
|
32
|
+
constructor(permits: number) {
|
|
33
|
+
if (!Number.isInteger(permits) || permits < 0) {
|
|
34
|
+
throw new RangeError(`permits must be a non-negative integer, got ${permits}`);
|
|
35
|
+
}
|
|
36
|
+
this.#permits = permits;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Current number of available permits. */
|
|
40
|
+
get availablePermits(): number {
|
|
41
|
+
return this.#permits;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Non-blocking acquisition: takes one permit if available and returns true,
|
|
46
|
+
* otherwise leaves the semaphore unchanged and returns false.
|
|
47
|
+
*/
|
|
48
|
+
tryAcquire(): boolean {
|
|
49
|
+
if (this.#permits > 0) {
|
|
50
|
+
this.#permits--;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Acquire a single permit. Resolves immediately when one is available;
|
|
58
|
+
* otherwise queues the caller until a permit is released.
|
|
59
|
+
*/
|
|
60
|
+
acquire(): Promise<void> {
|
|
61
|
+
if (this.#permits > 0) {
|
|
62
|
+
this.#permits--;
|
|
63
|
+
return Promise.resolve();
|
|
64
|
+
}
|
|
65
|
+
return new Promise<void>(resolve => {
|
|
66
|
+
this.#waiters.push(resolve);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Release a single permit. Hands it to a pending acquirer if any, otherwise
|
|
72
|
+
* increments the available count.
|
|
73
|
+
*/
|
|
74
|
+
release(): void {
|
|
75
|
+
const waiter = this.#waiters.shift();
|
|
76
|
+
if (waiter) {
|
|
77
|
+
waiter();
|
|
78
|
+
} else {
|
|
79
|
+
this.#permits++;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|