rocketmq-client-remoting-nodejs 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +428 -0
- package/README.md +424 -2
- package/dist/client/admin.d.ts +229 -0
- package/dist/client/admin.js +2462 -0
- package/dist/client/allocate.d.ts +48 -0
- package/dist/client/allocate.js +365 -0
- package/dist/client/backpressure.d.ts +23 -0
- package/dist/client/backpressure.js +95 -0
- package/dist/client/consumer.d.ts +294 -0
- package/dist/client/consumer.js +3095 -0
- package/dist/client/consumer_result.d.ts +129 -0
- package/dist/client/consumer_result.js +138 -0
- package/dist/client/consumer_stats.d.ts +69 -0
- package/dist/client/consumer_stats.js +258 -0
- package/dist/client/exception.d.ts +39 -0
- package/dist/client/exception.js +36 -0
- package/dist/client/hook.d.ts +145 -0
- package/dist/client/hook.js +171 -0
- package/dist/client/latency.d.ts +61 -0
- package/dist/client/latency.js +239 -0
- package/dist/client/lite_pull_consumer.d.ts +151 -0
- package/dist/client/lite_pull_consumer.js +1096 -0
- package/dist/client/mq_client.d.ts +188 -0
- package/dist/client/mq_client.js +1678 -0
- package/dist/client/mq_client_manager.d.ts +21 -0
- package/dist/client/mq_client_manager.js +129 -0
- package/dist/client/offset_store.d.ts +45 -0
- package/dist/client/offset_store.js +389 -0
- package/dist/client/open_tracing_hook.d.ts +52 -0
- package/dist/client/open_tracing_hook.js +241 -0
- package/dist/client/pop_api.d.ts +28 -0
- package/dist/client/pop_api.js +224 -0
- package/dist/client/pop_consumer.d.ts +23 -0
- package/dist/client/pop_consumer.js +719 -0
- package/dist/client/pop_process_queue.d.ts +15 -0
- package/dist/client/pop_process_queue.js +51 -0
- package/dist/client/process_queue.d.ts +49 -0
- package/dist/client/process_queue.js +261 -0
- package/dist/client/produce_accumulator.d.ts +75 -0
- package/dist/client/produce_accumulator.js +288 -0
- package/dist/client/producer.d.ts +231 -0
- package/dist/client/producer.js +1774 -0
- package/dist/client/pull_api.d.ts +26 -0
- package/dist/client/pull_api.js +264 -0
- package/dist/client/pull_consumer.d.ts +119 -0
- package/dist/client/pull_consumer.js +876 -0
- package/dist/client/pull_schedule_service.d.ts +71 -0
- package/dist/client/pull_schedule_service.js +424 -0
- package/dist/client/request_reply.d.ts +61 -0
- package/dist/client/request_reply.js +232 -0
- package/dist/client/retry_message.d.ts +2 -0
- package/dist/client/retry_message.js +27 -0
- package/dist/client/send_result.d.ts +57 -0
- package/dist/client/send_result.js +70 -0
- package/dist/client/top_addressing.d.ts +20 -0
- package/dist/client/top_addressing.js +75 -0
- package/dist/client/trace_context.d.ts +61 -0
- package/dist/client/trace_context.js +373 -0
- package/dist/client/trace_dispatcher.d.ts +31 -0
- package/dist/client/trace_dispatcher.js +236 -0
- package/dist/client/trace_hook.d.ts +23 -0
- package/dist/client/trace_hook.js +228 -0
- package/dist/client/traceparent.d.ts +9 -0
- package/dist/client/traceparent.js +86 -0
- package/dist/common/boundary_type.d.ts +7 -0
- package/dist/common/boundary_type.js +16 -0
- package/dist/common/compress.d.ts +28 -0
- package/dist/common/compress.js +512 -0
- package/dist/common/message.d.ts +129 -0
- package/dist/common/message.js +214 -0
- package/dist/common/messageClientIdSetter.d.ts +7 -0
- package/dist/common/messageClientIdSetter.js +48 -0
- package/dist/common/messageConst.d.ts +68 -0
- package/dist/common/messageConst.js +68 -0
- package/dist/common/messageDecoder.d.ts +40 -0
- package/dist/common/messageDecoder.js +504 -0
- package/dist/common/messageType.d.ts +11 -0
- package/dist/common/messageType.js +11 -0
- package/dist/common/message_accessor.d.ts +39 -0
- package/dist/common/message_accessor.js +135 -0
- package/dist/common/mixAll.d.ts +106 -0
- package/dist/common/mixAll.js +247 -0
- package/dist/common/recall_message_handle.d.ts +25 -0
- package/dist/common/recall_message_handle.js +98 -0
- package/dist/common/subscriptionData.d.ts +36 -0
- package/dist/common/subscriptionData.js +64 -0
- package/dist/common/sysflag.d.ts +129 -0
- package/dist/common/sysflag.js +96 -0
- package/dist/common/topic_config.d.ts +66 -0
- package/dist/common/topic_config.js +106 -0
- package/dist/common/topic_validator.d.ts +49 -0
- package/dist/common/topic_validator.js +115 -0
- package/dist/common/utilAll.d.ts +39 -0
- package/dist/common/utilAll.js +154 -0
- package/dist/common/validators.d.ts +25 -0
- package/dist/common/validators.js +80 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +36 -0
- package/dist/logging.d.ts +24 -0
- package/dist/logging.js +246 -0
- package/dist/remoting/acl.d.ts +16 -0
- package/dist/remoting/acl.js +39 -0
- package/dist/remoting/admin_body.d.ts +257 -0
- package/dist/remoting/admin_body.js +669 -0
- package/dist/remoting/bodies.d.ts +475 -0
- package/dist/remoting/bodies.js +1131 -0
- package/dist/remoting/client.d.ts +73 -0
- package/dist/remoting/client.js +591 -0
- package/dist/remoting/codes.d.ts +296 -0
- package/dist/remoting/codes.js +301 -0
- package/dist/remoting/exception.d.ts +45 -0
- package/dist/remoting/exception.js +58 -0
- package/dist/remoting/extra_info.d.ts +61 -0
- package/dist/remoting/extra_info.js +220 -0
- package/dist/remoting/headers.d.ts +758 -0
- package/dist/remoting/headers.js +1307 -0
- package/dist/remoting/heartbeat.d.ts +91 -0
- package/dist/remoting/heartbeat.js +227 -0
- package/dist/remoting/namespace.d.ts +14 -0
- package/dist/remoting/namespace.js +85 -0
- package/dist/remoting/pop_bodies.d.ts +58 -0
- package/dist/remoting/pop_bodies.js +214 -0
- package/dist/remoting/remotingCommand.d.ts +39 -0
- package/dist/remoting/remotingCommand.js +225 -0
- package/dist/remoting/route.d.ts +61 -0
- package/dist/remoting/route.js +288 -0
- package/dist/remoting/rpc_hooks.d.ts +31 -0
- package/dist/remoting/rpc_hooks.js +73 -0
- package/dist/remoting/serialize.d.ts +33 -0
- package/dist/remoting/serialize.js +372 -0
- package/dist/remoting/socks_proxy.d.ts +29 -0
- package/dist/remoting/socks_proxy.js +379 -0
- package/dist/remoting/static_topic.d.ts +49 -0
- package/dist/remoting/static_topic.js +244 -0
- package/dist/remoting/subscription.d.ts +65 -0
- package/dist/remoting/subscription.js +172 -0
- package/package.json +38 -4
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DefaultMQPullConsumer } from './pull_consumer.js';
|
|
2
|
+
import type { PullResult } from './consumer_result.js';
|
|
3
|
+
import { MessageQueue } from '../common/message.js';
|
|
4
|
+
export declare const DEFAULT_PULL_NEXT_DELAY_MILLIS = 200;
|
|
5
|
+
export declare const PULL_TASK_EXCEPTION_DELAY_MILLIS = 1000;
|
|
6
|
+
export declare const DEFAULT_QUEUE_CHECK_INTERVAL_MILLIS = 20000;
|
|
7
|
+
export declare const DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_MILLIS = 5000;
|
|
8
|
+
export type PullTaskCallback = (mq: MessageQueue, pullResult: PullResult, context: PullTaskContext) => number | void | Promise<number | void>;
|
|
9
|
+
export interface PullTaskOptions {
|
|
10
|
+
subExpression?: string;
|
|
11
|
+
offset?: number | (() => number) | null;
|
|
12
|
+
nextDelayMillis?: number;
|
|
13
|
+
maxMsgNums?: number;
|
|
14
|
+
timeoutMillis?: number | null;
|
|
15
|
+
}
|
|
16
|
+
export declare class PullTaskContext {
|
|
17
|
+
pullConsumer: DefaultMQPullConsumer;
|
|
18
|
+
messageQueue: MessageQueue;
|
|
19
|
+
subscriptionExpression: string;
|
|
20
|
+
maxMsgNums: number;
|
|
21
|
+
timeoutMillis: number | null;
|
|
22
|
+
nextOffset: number | null;
|
|
23
|
+
roundCount: number;
|
|
24
|
+
lastNextDelayMillis: number | null;
|
|
25
|
+
private _pullNextDelayMillis;
|
|
26
|
+
private _offsetSource;
|
|
27
|
+
private _oneShotDelayMillis;
|
|
28
|
+
constructor(pullConsumer: DefaultMQPullConsumer, messageQueue: MessageQueue, subscriptionExpression?: string, offset?: number | (() => number) | null, nextDelayMillis?: number, maxMsgNums?: number, timeoutMillis?: number | null);
|
|
29
|
+
resolveOffset(): Promise<number>;
|
|
30
|
+
seek(offset: number): void;
|
|
31
|
+
recordPullResult(result: PullResult): void;
|
|
32
|
+
getPullNextDelayTimeMillis(): number;
|
|
33
|
+
setPullInterval(millis: number): void;
|
|
34
|
+
setPullNextDelayTimeMillis(millis: number): void;
|
|
35
|
+
pullMessageLater(millis: number): void;
|
|
36
|
+
consumeNextDelayMillis(returnedDelay: number | null): number;
|
|
37
|
+
}
|
|
38
|
+
export declare class PullConsumerScheduleService {
|
|
39
|
+
queueCheckIntervalMillis: number;
|
|
40
|
+
shutdownDrainTimeoutMillis: number;
|
|
41
|
+
private _pullConsumer;
|
|
42
|
+
private _ownConsumer;
|
|
43
|
+
private _callbackTable;
|
|
44
|
+
private _taskTable;
|
|
45
|
+
private _queueCheckTimer;
|
|
46
|
+
private _started;
|
|
47
|
+
constructor(consumerGroup?: string | null, pullConsumer?: DefaultMQPullConsumer | null);
|
|
48
|
+
getPullConsumer(): DefaultMQPullConsumer;
|
|
49
|
+
isStarted(): boolean;
|
|
50
|
+
taskCount(): number;
|
|
51
|
+
callbackTopics(): string[];
|
|
52
|
+
registerPullTask(topic: string, callback: PullTaskCallback, opts?: PullTaskOptions): void;
|
|
53
|
+
unregisterPullTask(topic: string): void;
|
|
54
|
+
start(): Promise<void>;
|
|
55
|
+
shutdown(): Promise<void>;
|
|
56
|
+
private _recheckQueues;
|
|
57
|
+
private _queuesOfTopic;
|
|
58
|
+
private _putTasksForTopic;
|
|
59
|
+
private _schedule;
|
|
60
|
+
private _runTask;
|
|
61
|
+
private _pullOnce;
|
|
62
|
+
}
|
|
63
|
+
declare const _default: {
|
|
64
|
+
PullConsumerScheduleService: typeof PullConsumerScheduleService;
|
|
65
|
+
PullTaskContext: typeof PullTaskContext;
|
|
66
|
+
DEFAULT_PULL_NEXT_DELAY_MILLIS: number;
|
|
67
|
+
PULL_TASK_EXCEPTION_DELAY_MILLIS: number;
|
|
68
|
+
DEFAULT_QUEUE_CHECK_INTERVAL_MILLIS: number;
|
|
69
|
+
DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_MILLIS: number;
|
|
70
|
+
};
|
|
71
|
+
export default _default;
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// -*- coding: utf-8 -*-
|
|
2
|
+
// PullConsumerScheduleService -- a scheduled pull facade pairing a
|
|
3
|
+
// DefaultMQPullConsumer with a per-topic callback table (PullTaskCallback /
|
|
4
|
+
// PullTaskContext), in the "service pulls, callback consumes" shape:
|
|
5
|
+
// the service owns the per-queue pull round and hands the PullResult to the
|
|
6
|
+
// topic's callback.
|
|
7
|
+
//
|
|
8
|
+
// Threading model: no thread pool. Each queue task is a self-rescheduling
|
|
9
|
+
// setTimeout chain driven by the single event loop, and that chain still
|
|
10
|
+
// gives the guarantees a dedicated pool with pullThreadNums workers would
|
|
11
|
+
// give:
|
|
12
|
+
// * queue-dimension SERIAL execution — a queue's next round is scheduled only
|
|
13
|
+
// after its previous round (pull + callback) fully settles;
|
|
14
|
+
// * cross-queue concurrency — independent chains interleave on the loop;
|
|
15
|
+
// * every round rides DefaultMQPullConsumer.pullAsync, whose exactly-once
|
|
16
|
+
// setImmediate delivery keeps one pull from ever double-settling a round.
|
|
17
|
+
//
|
|
18
|
+
// Rescheduling delay precedence (three channels, first hit wins):
|
|
19
|
+
// 1. the callback's numeric return value (this round only),
|
|
20
|
+
// 2. context.pullMessageLater(ms) (one-shot, consumed after use),
|
|
21
|
+
// 3. context.setPullInterval(ms) (the resident interval).
|
|
22
|
+
// A failed pull or a throwing callback falls back to
|
|
23
|
+
// PULL_TASK_EXCEPTION_DELAY_MILLIS, the fixed one-second retry delay.
|
|
24
|
+
//
|
|
25
|
+
// The consumer facade underneath goes through MQClientManager like every other
|
|
26
|
+
// facade (instance reuse + refcounted shutdown) — this service never builds an
|
|
27
|
+
// MQClient of its own.
|
|
28
|
+
import { DefaultMQPullConsumer } from "./pull_consumer.js";
|
|
29
|
+
import { MessageModel } from "../remoting/heartbeat.js";
|
|
30
|
+
import { mqKey } from "./offset_store.js";
|
|
31
|
+
import { getLogger } from "../logging.js";
|
|
32
|
+
const logger = getLogger('client.pull_schedule_service');
|
|
33
|
+
export const DEFAULT_PULL_NEXT_DELAY_MILLIS = 200;
|
|
34
|
+
export const PULL_TASK_EXCEPTION_DELAY_MILLIS = 1000;
|
|
35
|
+
export const DEFAULT_QUEUE_CHECK_INTERVAL_MILLIS = 20000;
|
|
36
|
+
// shutdown() waits at most this long for in-flight rounds to settle; a bounded
|
|
37
|
+
// drain keeps a stalled round from blocking shutdown forever.
|
|
38
|
+
export const DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_MILLIS = 5000;
|
|
39
|
+
export class PullTaskContext {
|
|
40
|
+
pullConsumer;
|
|
41
|
+
messageQueue;
|
|
42
|
+
subscriptionExpression;
|
|
43
|
+
maxMsgNums;
|
|
44
|
+
timeoutMillis;
|
|
45
|
+
// Cursor for the next round; null until the first round resolves it from the
|
|
46
|
+
// registered source (see resolveOffset).
|
|
47
|
+
nextOffset = null;
|
|
48
|
+
// Observation: completed pull rounds and the delay actually used last round —
|
|
49
|
+
// tests and troubleshooting read these instead of wall-clock timing.
|
|
50
|
+
roundCount = 0;
|
|
51
|
+
lastNextDelayMillis = null;
|
|
52
|
+
_pullNextDelayMillis;
|
|
53
|
+
_offsetSource;
|
|
54
|
+
_oneShotDelayMillis = null;
|
|
55
|
+
constructor(pullConsumer, messageQueue, subscriptionExpression = '*', offset = null, nextDelayMillis = DEFAULT_PULL_NEXT_DELAY_MILLIS, maxMsgNums = 32, timeoutMillis = null) {
|
|
56
|
+
this.pullConsumer = pullConsumer;
|
|
57
|
+
this.messageQueue = messageQueue;
|
|
58
|
+
this.subscriptionExpression = subscriptionExpression;
|
|
59
|
+
this._offsetSource = offset;
|
|
60
|
+
this._pullNextDelayMillis = Math.max(0, Math.floor(nextDelayMillis));
|
|
61
|
+
this.maxMsgNums = maxMsgNums;
|
|
62
|
+
this.timeoutMillis = timeoutMillis;
|
|
63
|
+
}
|
|
64
|
+
// ---------------- offset ----------------
|
|
65
|
+
// Starting offset of the next pull round: reuse the cursor when present; the
|
|
66
|
+
// first round evaluates the registered source — explicit value/provider, or
|
|
67
|
+
// the broker committed offset, or the queue tail when nothing is committed.
|
|
68
|
+
async resolveOffset() {
|
|
69
|
+
if (this.nextOffset != null)
|
|
70
|
+
return this.nextOffset;
|
|
71
|
+
const src = this._offsetSource;
|
|
72
|
+
if (src != null) {
|
|
73
|
+
const v = typeof src === 'function' ? Number(src()) : Number(src);
|
|
74
|
+
this.nextOffset = Number.isFinite(v) ? Math.max(0, Math.floor(v)) : 0;
|
|
75
|
+
return this.nextOffset;
|
|
76
|
+
}
|
|
77
|
+
let committed = -1;
|
|
78
|
+
try {
|
|
79
|
+
committed = await this.pullConsumer.fetchConsumeOffset(this.messageQueue, true);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
logger.debug('fetchConsumeOffset failed for %s: %s', mqKey(this.messageQueue), e.message);
|
|
83
|
+
}
|
|
84
|
+
if (committed >= 0) {
|
|
85
|
+
this.nextOffset = committed;
|
|
86
|
+
return this.nextOffset;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const max = await this.pullConsumer.maxOffset(this.messageQueue);
|
|
90
|
+
this.nextOffset = max >= 0 ? max : 0;
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
logger.debug('maxOffset failed for %s: %s', mqKey(this.messageQueue), e.message);
|
|
94
|
+
this.nextOffset = 0;
|
|
95
|
+
}
|
|
96
|
+
return this.nextOffset;
|
|
97
|
+
}
|
|
98
|
+
// seek manually overrides the next round's start (a callback deciding where
|
|
99
|
+
// to re-read from, given the result).
|
|
100
|
+
seek(offset) {
|
|
101
|
+
if (!Number.isFinite(offset) || offset < 0) {
|
|
102
|
+
throw new Error(`offset must be >= 0, got ${offset}`);
|
|
103
|
+
}
|
|
104
|
+
this.nextOffset = Math.floor(offset);
|
|
105
|
+
}
|
|
106
|
+
// Advance the cursor after a successful pull (on OFFSET_ILLEGAL the broker's
|
|
107
|
+
// suggested nextBeginOffset IS the correction).
|
|
108
|
+
recordPullResult(result) {
|
|
109
|
+
this.nextOffset = result.nextBeginOffset;
|
|
110
|
+
this.roundCount += 1;
|
|
111
|
+
}
|
|
112
|
+
// ---------------- next-round delay ----------------
|
|
113
|
+
getPullNextDelayTimeMillis() { return this._pullNextDelayMillis; }
|
|
114
|
+
// Channel #3: the resident interval for every following round.
|
|
115
|
+
setPullInterval(millis) {
|
|
116
|
+
this._pullNextDelayMillis = Math.max(0, Math.floor(millis));
|
|
117
|
+
}
|
|
118
|
+
// An alternate spelling of setPullInterval — both write the same
|
|
119
|
+
// resident interval field, so a callback may use either name.
|
|
120
|
+
setPullNextDelayTimeMillis(millis) {
|
|
121
|
+
this.setPullInterval(millis);
|
|
122
|
+
}
|
|
123
|
+
// Channel #2: defer the NEXT round by millis (one-shot; the round after that
|
|
124
|
+
// falls back to the resident interval).
|
|
125
|
+
pullMessageLater(millis) {
|
|
126
|
+
this._oneShotDelayMillis = Math.max(0, Math.floor(millis));
|
|
127
|
+
}
|
|
128
|
+
// Settle this round's delay: explicit callback return value > one-shot
|
|
129
|
+
// deferral (consumed) > resident interval.
|
|
130
|
+
consumeNextDelayMillis(returnedDelay) {
|
|
131
|
+
let delay;
|
|
132
|
+
if (returnedDelay != null && Number.isFinite(returnedDelay)) {
|
|
133
|
+
delay = Math.floor(returnedDelay);
|
|
134
|
+
}
|
|
135
|
+
else if (this._oneShotDelayMillis != null) {
|
|
136
|
+
delay = this._oneShotDelayMillis;
|
|
137
|
+
this._oneShotDelayMillis = null;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
delay = this._pullNextDelayMillis;
|
|
141
|
+
}
|
|
142
|
+
delay = Math.max(0, delay);
|
|
143
|
+
this.lastNextDelayMillis = delay;
|
|
144
|
+
return delay;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// One topic's registration entry: the callback plus the task template params.
|
|
148
|
+
class TaskSpec {
|
|
149
|
+
callback;
|
|
150
|
+
subscriptionExpression;
|
|
151
|
+
offset;
|
|
152
|
+
nextDelayMillis;
|
|
153
|
+
maxMsgNums;
|
|
154
|
+
timeoutMillis;
|
|
155
|
+
constructor(callback, opts) {
|
|
156
|
+
this.callback = callback;
|
|
157
|
+
this.subscriptionExpression = opts.subExpression != null ? opts.subExpression : '*';
|
|
158
|
+
this.offset = opts.offset != null ? opts.offset : null;
|
|
159
|
+
this.nextDelayMillis = opts.nextDelayMillis != null
|
|
160
|
+
? opts.nextDelayMillis : DEFAULT_PULL_NEXT_DELAY_MILLIS;
|
|
161
|
+
this.maxMsgNums = opts.maxMsgNums != null ? opts.maxMsgNums : 32;
|
|
162
|
+
this.timeoutMillis = opts.timeoutMillis != null ? opts.timeoutMillis : null;
|
|
163
|
+
}
|
|
164
|
+
newContext(pullConsumer, mq) {
|
|
165
|
+
return new PullTaskContext(pullConsumer, mq, this.subscriptionExpression, this.offset, this.nextDelayMillis, this.maxMsgNums, this.timeoutMillis);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// One queue's scheduling state: the context plus cancel/in-flight gates and the
|
|
169
|
+
// pending timer (queue-dimension serialization).
|
|
170
|
+
class PullTask {
|
|
171
|
+
context;
|
|
172
|
+
cancelled = false;
|
|
173
|
+
inFlight = false;
|
|
174
|
+
timer = null;
|
|
175
|
+
constructor(context) { this.context = context; }
|
|
176
|
+
}
|
|
177
|
+
export class PullConsumerScheduleService {
|
|
178
|
+
// Queue-view recheck period: this facade has no rebalance thread that is
|
|
179
|
+
// notified when a topic's queue set changes,
|
|
180
|
+
// so the view is reconciled on a timer instead.
|
|
181
|
+
queueCheckIntervalMillis = DEFAULT_QUEUE_CHECK_INTERVAL_MILLIS;
|
|
182
|
+
shutdownDrainTimeoutMillis = DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_MILLIS;
|
|
183
|
+
_pullConsumer;
|
|
184
|
+
_ownConsumer;
|
|
185
|
+
_callbackTable = new Map();
|
|
186
|
+
_taskTable = new Map(); // mqKey -> task
|
|
187
|
+
_queueCheckTimer = null;
|
|
188
|
+
_started = false;
|
|
189
|
+
constructor(consumerGroup = null, pullConsumer = null) {
|
|
190
|
+
if (pullConsumer == null) {
|
|
191
|
+
if (consumerGroup == null || !String(consumerGroup).trim()) {
|
|
192
|
+
throw new Error('consumerGroup is required when pullConsumer is not given');
|
|
193
|
+
}
|
|
194
|
+
this._pullConsumer = new DefaultMQPullConsumer(consumerGroup);
|
|
195
|
+
this._ownConsumer = true;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
this._pullConsumer = pullConsumer;
|
|
199
|
+
this._ownConsumer = false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// ---------------- public API ----------------
|
|
203
|
+
getPullConsumer() { return this._pullConsumer; }
|
|
204
|
+
isStarted() { return this._started; }
|
|
205
|
+
taskCount() { return this._taskTable.size; }
|
|
206
|
+
callbackTopics() { return [...this._callbackTable.keys()].sort(); }
|
|
207
|
+
// Register (or replace) a topic's pull callback. When already started the
|
|
208
|
+
// topic's queues are expanded into tasks immediately.
|
|
209
|
+
registerPullTask(topic, callback, opts = {}) {
|
|
210
|
+
if (topic == null || !String(topic).trim())
|
|
211
|
+
throw new Error('topic is empty');
|
|
212
|
+
if (typeof callback !== 'function')
|
|
213
|
+
throw new Error('callback is required');
|
|
214
|
+
this._callbackTable.set(topic, new TaskSpec(callback, opts));
|
|
215
|
+
if (this._started)
|
|
216
|
+
void this._putTasksForTopic(topic);
|
|
217
|
+
}
|
|
218
|
+
unregisterPullTask(topic) {
|
|
219
|
+
this._callbackTable.delete(topic);
|
|
220
|
+
if (!this._started)
|
|
221
|
+
return;
|
|
222
|
+
for (const [key, task] of [...this._taskTable]) {
|
|
223
|
+
if (task.context.messageQueue.getTopic() === topic) {
|
|
224
|
+
task.cancelled = true;
|
|
225
|
+
if (task.timer != null) {
|
|
226
|
+
clearTimeout(task.timer);
|
|
227
|
+
task.timer = null;
|
|
228
|
+
}
|
|
229
|
+
this._taskTable.delete(key);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async start() {
|
|
234
|
+
if (this._started)
|
|
235
|
+
return;
|
|
236
|
+
// The facade's own start() goes through MQClientManager (instance reuse +
|
|
237
|
+
// refcount); nothing here may bypass it.
|
|
238
|
+
await this._pullConsumer.start();
|
|
239
|
+
this._started = true;
|
|
240
|
+
for (const topic of [...this._callbackTable.keys()]) {
|
|
241
|
+
await this._putTasksForTopic(topic);
|
|
242
|
+
}
|
|
243
|
+
// Deliberately NOT unref'd: a started service must keep the process alive
|
|
244
|
+
// until shutdown() clears it, so this timer holds the loop open.
|
|
245
|
+
this._queueCheckTimer = setInterval(() => { void this._recheckQueues(); }, this.queueCheckIntervalMillis);
|
|
246
|
+
logger.info('PullConsumerScheduleService start OK, group=%s topics=%s', this._pullConsumer.consumerGroup, this.callbackTopics().join(','));
|
|
247
|
+
}
|
|
248
|
+
// Shutdown DRAINS: pending timers are cancelled, but in-flight rounds (pull +
|
|
249
|
+
// callback) are awaited up to shutdownDrainTimeoutMillis before the task
|
|
250
|
+
// table is dropped — a callback mid-flight always sees its round finished.
|
|
251
|
+
async shutdown() {
|
|
252
|
+
if (!this._started)
|
|
253
|
+
return;
|
|
254
|
+
this._started = false;
|
|
255
|
+
if (this._queueCheckTimer != null) {
|
|
256
|
+
clearInterval(this._queueCheckTimer);
|
|
257
|
+
this._queueCheckTimer = null;
|
|
258
|
+
}
|
|
259
|
+
for (const task of this._taskTable.values()) {
|
|
260
|
+
task.cancelled = true;
|
|
261
|
+
if (task.timer != null) {
|
|
262
|
+
clearTimeout(task.timer);
|
|
263
|
+
task.timer = null;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const deadline = Date.now() + this.shutdownDrainTimeoutMillis;
|
|
267
|
+
while (Date.now() < deadline) {
|
|
268
|
+
let inFlight = 0;
|
|
269
|
+
for (const task of this._taskTable.values())
|
|
270
|
+
if (task.inFlight)
|
|
271
|
+
inFlight++;
|
|
272
|
+
if (inFlight === 0)
|
|
273
|
+
break;
|
|
274
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
275
|
+
}
|
|
276
|
+
this._taskTable.clear();
|
|
277
|
+
if (this._ownConsumer) {
|
|
278
|
+
await this._pullConsumer.shutdown().catch((e) => logger.warning('pull consumer shutdown raised: %s', e.message));
|
|
279
|
+
}
|
|
280
|
+
logger.info('PullConsumerScheduleService shutdown OK, group=%s', this._pullConsumer.consumerGroup);
|
|
281
|
+
}
|
|
282
|
+
// ---------------- queue expansion ----------------
|
|
283
|
+
async _recheckQueues() {
|
|
284
|
+
if (!this._started)
|
|
285
|
+
return;
|
|
286
|
+
for (const topic of [...this._callbackTable.keys()]) {
|
|
287
|
+
await this._putTasksForTopic(topic);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async _queuesOfTopic(topic) {
|
|
291
|
+
if (this._pullConsumer.messageModel === MessageModel.BROADCASTING) {
|
|
292
|
+
return this._pullConsumer.fetchSubscribeMessageQueues(topic);
|
|
293
|
+
}
|
|
294
|
+
// CLUSTERING: this instance's share — fetchMessageQueuesInBalance keeps the
|
|
295
|
+
// existing assignment when the route/consumer-list view is unavailable, so
|
|
296
|
+
// a failed query never drops or duplicates queues.
|
|
297
|
+
return this._pullConsumer.fetchMessageQueuesInBalance(topic);
|
|
298
|
+
}
|
|
299
|
+
// Align one topic's queue set with the task table: cancel tasks whose queue
|
|
300
|
+
// left the view, enqueue newly appeared queues with zero delay.
|
|
301
|
+
async _putTasksForTopic(topic) {
|
|
302
|
+
const spec = this._callbackTable.get(topic);
|
|
303
|
+
if (spec == null || !this._started)
|
|
304
|
+
return;
|
|
305
|
+
let queues;
|
|
306
|
+
try {
|
|
307
|
+
queues = await this._queuesOfTopic(topic);
|
|
308
|
+
}
|
|
309
|
+
catch (e) {
|
|
310
|
+
// View unavailable: KEEP the existing tasks — never cancel everything on
|
|
311
|
+
// a transient route/consumer-list failure.
|
|
312
|
+
logger.debug('queue view unavailable for %s: %s', topic, e.message);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (!this._started)
|
|
316
|
+
return;
|
|
317
|
+
const newSet = new Map();
|
|
318
|
+
for (const mq of queues) {
|
|
319
|
+
if (mq.getTopic() === topic)
|
|
320
|
+
newSet.set(mqKey(mq), mq);
|
|
321
|
+
}
|
|
322
|
+
for (const [key, task] of [...this._taskTable]) {
|
|
323
|
+
if (task.context.messageQueue.getTopic() !== topic)
|
|
324
|
+
continue;
|
|
325
|
+
if (!newSet.has(key)) {
|
|
326
|
+
task.cancelled = true;
|
|
327
|
+
if (task.timer != null) {
|
|
328
|
+
clearTimeout(task.timer);
|
|
329
|
+
task.timer = null;
|
|
330
|
+
}
|
|
331
|
+
this._taskTable.delete(key);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
for (const [key, mq] of newSet) {
|
|
335
|
+
if (this._taskTable.has(key))
|
|
336
|
+
continue;
|
|
337
|
+
const task = new PullTask(spec.newContext(this._pullConsumer, mq));
|
|
338
|
+
this._taskTable.set(key, task);
|
|
339
|
+
this._schedule(task, 0);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
// ---------------- one queue's round ----------------
|
|
343
|
+
_schedule(task, delayMillis) {
|
|
344
|
+
if (task.cancelled || !this._started)
|
|
345
|
+
return;
|
|
346
|
+
const delay = Math.max(0, Math.floor(delayMillis));
|
|
347
|
+
// NOT unref'd — see start(): a live service keeps the process alive.
|
|
348
|
+
task.timer = setTimeout(() => { task.timer = null; void this._runTask(task); }, delay);
|
|
349
|
+
}
|
|
350
|
+
// One round: pull exactly once through pullAsync, hand the result to the
|
|
351
|
+
// callback, settle the next delay, reschedule. Serial per queue by
|
|
352
|
+
// construction — the next timer is only armed after this round settles.
|
|
353
|
+
async _runTask(task) {
|
|
354
|
+
const context = task.context;
|
|
355
|
+
const mq = context.messageQueue;
|
|
356
|
+
if (task.cancelled || !this._started)
|
|
357
|
+
return;
|
|
358
|
+
task.inFlight = true;
|
|
359
|
+
let result = null;
|
|
360
|
+
let returnedDelay = null;
|
|
361
|
+
let failed = false;
|
|
362
|
+
try {
|
|
363
|
+
const offset = await context.resolveOffset();
|
|
364
|
+
result = await this._pullOnce(context, offset);
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
failed = true;
|
|
368
|
+
logger.error('pull schedule: pull %s failed: %s', mqKey(mq), e.message);
|
|
369
|
+
}
|
|
370
|
+
if (task.cancelled) {
|
|
371
|
+
task.inFlight = false;
|
|
372
|
+
logger.warning('The pull task is cancelled after doPullTask, %s', mqKey(mq));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (!failed && result != null) {
|
|
376
|
+
context.recordPullResult(result);
|
|
377
|
+
const spec = this._callbackTable.get(mq.getTopic());
|
|
378
|
+
if (spec == null) {
|
|
379
|
+
logger.warning('Pull task callback not exist, topic=%s', mq.getTopic());
|
|
380
|
+
this._taskTable.delete(mqKey(mq));
|
|
381
|
+
task.inFlight = false;
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
const returned = await spec.callback(mq, result, context);
|
|
386
|
+
returnedDelay = (typeof returned === 'number' && Number.isFinite(returned))
|
|
387
|
+
? returned : null;
|
|
388
|
+
}
|
|
389
|
+
catch (e) {
|
|
390
|
+
logger.error('doPullTask exception, mq=%s: %s', mqKey(mq), e.message);
|
|
391
|
+
returnedDelay = PULL_TASK_EXCEPTION_DELAY_MILLIS;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
returnedDelay = PULL_TASK_EXCEPTION_DELAY_MILLIS;
|
|
396
|
+
}
|
|
397
|
+
const delay = context.consumeNextDelayMillis(returnedDelay);
|
|
398
|
+
task.inFlight = false;
|
|
399
|
+
if (task.cancelled || !this._started) {
|
|
400
|
+
logger.warning('The pull task is cancelled after doPullTask, %s', mqKey(mq));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (this._taskTable.get(mqKey(mq)) !== task)
|
|
404
|
+
return; // replaced by a re-expansion
|
|
405
|
+
this._schedule(task, delay);
|
|
406
|
+
}
|
|
407
|
+
// One wire round through pullAsync — the exactly-once setImmediate contract
|
|
408
|
+
// maps 1:1 onto a promise settle, so success XOR exception fires once.
|
|
409
|
+
_pullOnce(context, offset) {
|
|
410
|
+
const consumer = context.pullConsumer;
|
|
411
|
+
const timeout = context.timeoutMillis != null ? context.timeoutMillis : consumer.pullTimeoutMillis;
|
|
412
|
+
return new Promise((resolve, reject) => {
|
|
413
|
+
consumer.pullAsync(context.messageQueue, context.subscriptionExpression, offset, context.maxMsgNums, {
|
|
414
|
+
onSuccess: (r) => resolve(r),
|
|
415
|
+
onException: (e) => reject(e),
|
|
416
|
+
}, timeout);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
export default {
|
|
421
|
+
PullConsumerScheduleService, PullTaskContext,
|
|
422
|
+
DEFAULT_PULL_NEXT_DELAY_MILLIS, PULL_TASK_EXCEPTION_DELAY_MILLIS,
|
|
423
|
+
DEFAULT_QUEUE_CHECK_INTERVAL_MILLIS, DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_MILLIS,
|
|
424
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Message } from '../common/message.js';
|
|
2
|
+
export type RequestCallbackLike = RequestCallback | ((replyMsg: Message | null, err: Error | null) => void);
|
|
3
|
+
export declare function createCorrelationId(): string;
|
|
4
|
+
export declare class RequestResponseFuture {
|
|
5
|
+
correlationId: string;
|
|
6
|
+
requestMsg: Message | null;
|
|
7
|
+
timeoutMillis: number;
|
|
8
|
+
beginTimestamp: number;
|
|
9
|
+
done: boolean;
|
|
10
|
+
requestCallback: RequestCallbackLike | null;
|
|
11
|
+
responseMsg: Message | null;
|
|
12
|
+
sendRequestOk: boolean;
|
|
13
|
+
cause: Error | null;
|
|
14
|
+
private _callbackExecuted;
|
|
15
|
+
_resolve: ((msg: Message) => void) | null;
|
|
16
|
+
_reject: ((err: Error) => void) | null;
|
|
17
|
+
_promise: Promise<Message>;
|
|
18
|
+
constructor(correlationId: string, requestMsg: Message | null, timeoutMillis: number, requestCallback?: RequestCallbackLike | null);
|
|
19
|
+
promise(): Promise<Message>;
|
|
20
|
+
putResponseMessage(responseMsg: Message | null): void;
|
|
21
|
+
setSendRequestOk(sendRequestOk: boolean): void;
|
|
22
|
+
setCause(cause: Error | null): void;
|
|
23
|
+
executeRequestCallback(): void;
|
|
24
|
+
complete(replyMsg: Message): void;
|
|
25
|
+
completeExceptionally(err: Error): void;
|
|
26
|
+
isDone(): boolean;
|
|
27
|
+
isTimeout(): boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare class RequestFutureHolder {
|
|
30
|
+
private static _instance;
|
|
31
|
+
private table;
|
|
32
|
+
private producerSet;
|
|
33
|
+
private _sweepTimer;
|
|
34
|
+
static getInstance(): RequestFutureHolder;
|
|
35
|
+
putRequest(correlationId: string, future: RequestResponseFuture): void;
|
|
36
|
+
getRequest(correlationId: string): RequestResponseFuture | null;
|
|
37
|
+
removeRequest(correlationId: string): RequestResponseFuture | null;
|
|
38
|
+
scanExpiredRequest(): void;
|
|
39
|
+
startScheduledTask(producer: any): void;
|
|
40
|
+
shutdown(producer: any): void;
|
|
41
|
+
size(): number;
|
|
42
|
+
}
|
|
43
|
+
export declare const REQUEST_FUTURE_HOLDER: RequestFutureHolder;
|
|
44
|
+
export declare function createReplyMessage(requestMessage: Message, body: Buffer | string): Message;
|
|
45
|
+
export declare function isReplyMessage(msg: Message): boolean;
|
|
46
|
+
export declare abstract class RequestCallback {
|
|
47
|
+
onSuccess(replyMsg: Message): void;
|
|
48
|
+
onException(e: Error): void;
|
|
49
|
+
}
|
|
50
|
+
export declare function getReplyTopic(clusterName: string): string;
|
|
51
|
+
declare const _default: {
|
|
52
|
+
createCorrelationId: typeof createCorrelationId;
|
|
53
|
+
RequestResponseFuture: typeof RequestResponseFuture;
|
|
54
|
+
RequestFutureHolder: typeof RequestFutureHolder;
|
|
55
|
+
REQUEST_FUTURE_HOLDER: RequestFutureHolder;
|
|
56
|
+
createReplyMessage: typeof createReplyMessage;
|
|
57
|
+
isReplyMessage: typeof isReplyMessage;
|
|
58
|
+
RequestCallback: typeof RequestCallback;
|
|
59
|
+
getReplyTopic: typeof getReplyTopic;
|
|
60
|
+
};
|
|
61
|
+
export default _default;
|