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,73 @@
|
|
|
1
|
+
import { RemotingCommand } from './remotingCommand.js';
|
|
2
|
+
import type { SocksProxyConfig } from './socks_proxy.js';
|
|
3
|
+
export type ChannelListener = (addr: string) => void;
|
|
4
|
+
export interface TlsOptions {
|
|
5
|
+
caCert?: string;
|
|
6
|
+
clientCert?: string;
|
|
7
|
+
clientKey?: string;
|
|
8
|
+
serverName?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class RemotingClient {
|
|
11
|
+
connectTimeoutMillis: number;
|
|
12
|
+
invokeTimeoutMillis: number;
|
|
13
|
+
enableReconnectForGoAway: boolean;
|
|
14
|
+
maxIdleTimeMillis: number;
|
|
15
|
+
closeSocketOnSyncTimeout: boolean;
|
|
16
|
+
tlsEnable: boolean;
|
|
17
|
+
tlsOptions: TlsOptions | null;
|
|
18
|
+
socksProxyConfig: SocksProxyConfig | null;
|
|
19
|
+
private conns;
|
|
20
|
+
private buffers;
|
|
21
|
+
private responseTable;
|
|
22
|
+
private rpcHooks;
|
|
23
|
+
private channelListeners;
|
|
24
|
+
private processors;
|
|
25
|
+
private running;
|
|
26
|
+
private connectPromises;
|
|
27
|
+
private lastActivity;
|
|
28
|
+
private idleSweepTimer;
|
|
29
|
+
constructor(opts?: {
|
|
30
|
+
connectTimeoutMillis?: number;
|
|
31
|
+
invokeTimeoutMillis?: number;
|
|
32
|
+
maxIdleTimeMillis?: number;
|
|
33
|
+
closeSocketOnSyncTimeout?: boolean;
|
|
34
|
+
tlsEnable?: boolean;
|
|
35
|
+
tlsOptions?: TlsOptions | null;
|
|
36
|
+
socksProxyConfig?: SocksProxyConfig | null;
|
|
37
|
+
});
|
|
38
|
+
private _parseAddr;
|
|
39
|
+
private _tlsOptions;
|
|
40
|
+
private _connect;
|
|
41
|
+
isChannelWritable(addr: string): boolean;
|
|
42
|
+
closeChannel(addr: string): void;
|
|
43
|
+
private _closeSocketForFrameError;
|
|
44
|
+
private _onData;
|
|
45
|
+
private _onClose;
|
|
46
|
+
private _dispatch;
|
|
47
|
+
registerProcessor(requestCode: number, handler: (cmd: RemotingCommand, addr: string) => RemotingCommand | null | Promise<RemotingCommand | null>): void;
|
|
48
|
+
unregisterProcessor(requestCode: number): void;
|
|
49
|
+
registerRpcHook(hook: any): void;
|
|
50
|
+
unregisterRpcHook(hook: any): void;
|
|
51
|
+
private _applyBeforeRequestHooks;
|
|
52
|
+
private _applyAfterResponseHooks;
|
|
53
|
+
registerChannelListener(listener: ChannelListener): void;
|
|
54
|
+
private _fireChannelActive;
|
|
55
|
+
private _send;
|
|
56
|
+
private _write;
|
|
57
|
+
private _writeResponse;
|
|
58
|
+
invokeSync(addr: string, request: RemotingCommand, timeoutMillis?: number): Promise<RemotingCommand>;
|
|
59
|
+
private _handleGoAway;
|
|
60
|
+
private _retryRequest;
|
|
61
|
+
private _shouldCloseOnSyncTimeout;
|
|
62
|
+
private _invokeOnce;
|
|
63
|
+
invokeAsync(addr: string, request: RemotingCommand, callback: (cmd: RemotingCommand | null, err: Error | null) => void, timeoutMillis?: number): void;
|
|
64
|
+
private _invokeAsyncOnce;
|
|
65
|
+
invokeOneway(addr: string, request: RemotingCommand): Promise<void>;
|
|
66
|
+
private _complete;
|
|
67
|
+
private _failFast;
|
|
68
|
+
private _idleLimitMillis;
|
|
69
|
+
private _ensureIdleSweep;
|
|
70
|
+
private _sweepIdleConnections;
|
|
71
|
+
shutdown(): void;
|
|
72
|
+
}
|
|
73
|
+
export default RemotingClient;
|
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import tls from 'node:tls';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import { once } from 'node:events';
|
|
5
|
+
import { Buffer } from 'node:buffer';
|
|
6
|
+
import { ResponseCode } from "./codes.js";
|
|
7
|
+
import { RemotingCommand } from "./remotingCommand.js";
|
|
8
|
+
import { RemotingSendRequestException, RemotingTimeoutException } from "./exception.js";
|
|
9
|
+
import { parseProxyAddr, parseSocksProxyConfig, socksHandshake } from "./socks_proxy.js";
|
|
10
|
+
import { getLogger } from "../logging.js";
|
|
11
|
+
const logger = getLogger('remoting.client');
|
|
12
|
+
const MAX_FRAME_LENGTH = 16 * 1024 * 1024;
|
|
13
|
+
// Idle-connection close default: a connection that has neither read nor written
|
|
14
|
+
// for this long is swept away. A configured value that is not positive falls back
|
|
15
|
+
// to this default (it does not disable the sweep).
|
|
16
|
+
const DEFAULT_MAX_IDLE_TIME_MILLIS = 120 * 1000;
|
|
17
|
+
export class RemotingClient {
|
|
18
|
+
connectTimeoutMillis = 3000;
|
|
19
|
+
invokeTimeoutMillis = 15000;
|
|
20
|
+
enableReconnectForGoAway = true;
|
|
21
|
+
// Idle-connection close limit, in milliseconds (120 s by default). A value <= 0
|
|
22
|
+
// falls back to the default rather than disabling the sweep.
|
|
23
|
+
maxIdleTimeMillis = DEFAULT_MAX_IDLE_TIME_MILLIS;
|
|
24
|
+
// When a synchronous invoke's own wait times out, close the connection first.
|
|
25
|
+
// A port that does not subtract the connect cost effectively always closes for
|
|
26
|
+
// any positive timeout; the flag is the master switch for that behaviour.
|
|
27
|
+
closeSocketOnSyncTimeout = true;
|
|
28
|
+
tlsEnable = false;
|
|
29
|
+
tlsOptions = null;
|
|
30
|
+
socksProxyConfig = null;
|
|
31
|
+
conns = new Map();
|
|
32
|
+
buffers = new Map();
|
|
33
|
+
responseTable = new Map();
|
|
34
|
+
rpcHooks = [];
|
|
35
|
+
channelListeners = [];
|
|
36
|
+
processors = new Map();
|
|
37
|
+
running = true;
|
|
38
|
+
connectPromises = new Map();
|
|
39
|
+
// Per-connection last-activity stamp (address -> epoch millis), refreshed on
|
|
40
|
+
// every successful read and every completed write; consumed by the idle sweep.
|
|
41
|
+
lastActivity = new Map();
|
|
42
|
+
// The idle sweep timer. It is unref()'d so it never keeps a test process alive,
|
|
43
|
+
// and cleared on shutdown.
|
|
44
|
+
idleSweepTimer = null;
|
|
45
|
+
constructor(opts = {}) {
|
|
46
|
+
if (opts.connectTimeoutMillis != null)
|
|
47
|
+
this.connectTimeoutMillis = opts.connectTimeoutMillis;
|
|
48
|
+
if (opts.invokeTimeoutMillis != null)
|
|
49
|
+
this.invokeTimeoutMillis = opts.invokeTimeoutMillis;
|
|
50
|
+
if (opts.maxIdleTimeMillis != null)
|
|
51
|
+
this.maxIdleTimeMillis = opts.maxIdleTimeMillis;
|
|
52
|
+
if (opts.closeSocketOnSyncTimeout != null)
|
|
53
|
+
this.closeSocketOnSyncTimeout = opts.closeSocketOnSyncTimeout;
|
|
54
|
+
if (opts.tlsEnable != null)
|
|
55
|
+
this.tlsEnable = opts.tlsEnable;
|
|
56
|
+
else {
|
|
57
|
+
const e = (process.env['ROCKETMQ_TLS_ENABLE'] || '').trim().toLowerCase();
|
|
58
|
+
this.tlsEnable = e === '1' || e === 'true' || e === 'yes';
|
|
59
|
+
}
|
|
60
|
+
if (opts.tlsOptions !== undefined)
|
|
61
|
+
this.tlsOptions = opts.tlsOptions;
|
|
62
|
+
if (opts.socksProxyConfig !== undefined)
|
|
63
|
+
this.socksProxyConfig = opts.socksProxyConfig;
|
|
64
|
+
else {
|
|
65
|
+
const env = (process.env['ROCKETMQ_SOCKS_PROXY_CONFIG'] || '').trim();
|
|
66
|
+
if (env)
|
|
67
|
+
this.socksProxyConfig = parseSocksProxyConfig(env);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// ---------------- connection management ----------------
|
|
71
|
+
_parseAddr(addr) {
|
|
72
|
+
if (addr.startsWith('[')) {
|
|
73
|
+
const idx = addr.indexOf(']');
|
|
74
|
+
const host = addr.slice(1, idx);
|
|
75
|
+
const port = parseInt(addr.slice(idx + 2), 10);
|
|
76
|
+
return [host, port];
|
|
77
|
+
}
|
|
78
|
+
const idx = addr.lastIndexOf(':');
|
|
79
|
+
return [addr.slice(0, idx), parseInt(addr.slice(idx + 1), 10)];
|
|
80
|
+
}
|
|
81
|
+
// TLS ConnectionOptions WITHOUT host/port/socket — shared by the direct dial
|
|
82
|
+
// and the SOCKS-tunnel upgrade path (identical cert handling in both).
|
|
83
|
+
async _tlsOptions(host) {
|
|
84
|
+
const t = this.tlsOptions;
|
|
85
|
+
const strict = t?.caCert != null;
|
|
86
|
+
const opts = { rejectUnauthorized: strict };
|
|
87
|
+
if (strict) {
|
|
88
|
+
opts.ca = [await fs.promises.readFile(t.caCert)];
|
|
89
|
+
// servername defaults to host; set it only when explicitly overridden and different (node emits
|
|
90
|
+
// DEP0123 deprecation for IP SNI, but the certificate's IP SAN check still follows host).
|
|
91
|
+
if (t.serverName != null && t.serverName !== '' && t.serverName !== host) {
|
|
92
|
+
opts.servername = t.serverName;
|
|
93
|
+
}
|
|
94
|
+
if (t.clientCert != null)
|
|
95
|
+
opts.cert = [await fs.promises.readFile(t.clientCert)];
|
|
96
|
+
if (t.clientKey != null)
|
|
97
|
+
opts.key = [await fs.promises.readFile(t.clientKey)];
|
|
98
|
+
}
|
|
99
|
+
return opts;
|
|
100
|
+
}
|
|
101
|
+
async _connect(addr) {
|
|
102
|
+
const existing = this.conns.get(addr);
|
|
103
|
+
if (existing && !existing.destroyed)
|
|
104
|
+
return existing;
|
|
105
|
+
let p = this.connectPromises.get(addr);
|
|
106
|
+
if (p)
|
|
107
|
+
return p;
|
|
108
|
+
p = (async () => {
|
|
109
|
+
const [host, port] = this._parseAddr(addr);
|
|
110
|
+
const proxy = this.socksProxyConfig;
|
|
111
|
+
let sock;
|
|
112
|
+
if (proxy != null) {
|
|
113
|
+
const [phost, pport] = parseProxyAddr(proxy.addr);
|
|
114
|
+
sock = net.connect({ host: phost, port: pport });
|
|
115
|
+
}
|
|
116
|
+
else if (this.tlsEnable) {
|
|
117
|
+
// caCert given → strict verification: 'secureConnect' is not emitted until the handshake
|
|
118
|
+
// (certificate chain + hostname check) completes — a wrong CA / mismatched hostname errors at
|
|
119
|
+
// connect time rather than on first write. test-mode (no caCert) keeps rejectUnauthorized:false.
|
|
120
|
+
sock = tls.connect({ ...(await this._tlsOptions(host)), host, port });
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
sock = net.connect({ host, port });
|
|
124
|
+
}
|
|
125
|
+
sock.setNoDelay(true);
|
|
126
|
+
const timer = setTimeout(() => sock.destroy(new Error('connect timeout')), this.connectTimeoutMillis);
|
|
127
|
+
try {
|
|
128
|
+
if (proxy != null) {
|
|
129
|
+
// TCP to the proxy is connected → SOCKS CONNECT handshake (before the TLS handshake).
|
|
130
|
+
const rawSock = sock;
|
|
131
|
+
try {
|
|
132
|
+
await once(rawSock, 'connect');
|
|
133
|
+
await socksHandshake(rawSock, proxy, host, port, this.connectTimeoutMillis);
|
|
134
|
+
if (this.tlsEnable) {
|
|
135
|
+
// Upgrade to TLS inside the tunnel: SNI uses the target host (serverName override was set in _tlsOptions).
|
|
136
|
+
const opts = await this._tlsOptions(host);
|
|
137
|
+
if (opts.servername == null)
|
|
138
|
+
opts.servername = host;
|
|
139
|
+
opts.socket = rawSock;
|
|
140
|
+
sock = tls.connect(opts);
|
|
141
|
+
sock.setNoDelay(true);
|
|
142
|
+
await once(sock, 'secureConnect');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
// Handshake failed: the tunnel must not enter the connection pool and must be torn down to avoid a dangling half-open connection.
|
|
147
|
+
try {
|
|
148
|
+
if (!rawSock.destroyed)
|
|
149
|
+
rawSock.destroy();
|
|
150
|
+
}
|
|
151
|
+
catch { /* ignore */ }
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
await once(sock, this.tlsEnable && this.tlsOptions?.caCert != null ? 'secureConnect' : 'connect');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
}
|
|
162
|
+
this.conns.set(addr, sock);
|
|
163
|
+
this.buffers.set(addr, Buffer.alloc(0));
|
|
164
|
+
this.lastActivity.set(addr, Date.now());
|
|
165
|
+
this._ensureIdleSweep();
|
|
166
|
+
sock.on('data', (chunk) => this._onData(addr, sock, chunk));
|
|
167
|
+
sock.on('close', () => this._onClose(addr, sock));
|
|
168
|
+
sock.on('error', (e) => { logger.debug('socket error %s: %s', addr, e.message); });
|
|
169
|
+
// Fired here, inside the single dial, so concurrent callers waiting on
|
|
170
|
+
// the same promise still see exactly one event per connection.
|
|
171
|
+
this._fireChannelActive(addr);
|
|
172
|
+
return sock;
|
|
173
|
+
})();
|
|
174
|
+
this.connectPromises.set(addr, p);
|
|
175
|
+
try {
|
|
176
|
+
return await p;
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
this.connectPromises.delete(addr);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
isChannelWritable(addr) {
|
|
183
|
+
// "No connection yet" is not "cannot write": an absent or already-closed
|
|
184
|
+
// socket reports writable (there is nothing to block on). Only a live socket
|
|
185
|
+
// whose outbound buffer has hit the backpressure watermark is unwritable.
|
|
186
|
+
const c = this.conns.get(addr);
|
|
187
|
+
if (c == null || c.destroyed)
|
|
188
|
+
return true;
|
|
189
|
+
return !(c.writableNeedDrain === true || c.writableLength > c.writableHighWaterMark);
|
|
190
|
+
}
|
|
191
|
+
closeChannel(addr) {
|
|
192
|
+
const c = this.conns.get(addr);
|
|
193
|
+
this.conns.delete(addr);
|
|
194
|
+
this.buffers.delete(addr);
|
|
195
|
+
this.lastActivity.delete(addr);
|
|
196
|
+
if (c && !c.destroyed)
|
|
197
|
+
c.destroy();
|
|
198
|
+
}
|
|
199
|
+
// Take down exactly this socket the way a peer close does: destroying it fires
|
|
200
|
+
// 'close' -> _onClose -> _failFast, so its pending entries fail fast while other
|
|
201
|
+
// connections stay put.
|
|
202
|
+
_closeSocketForFrameError(sock) {
|
|
203
|
+
try {
|
|
204
|
+
if (sock && !sock.destroyed)
|
|
205
|
+
sock.destroy();
|
|
206
|
+
}
|
|
207
|
+
catch { /* ignore */ }
|
|
208
|
+
}
|
|
209
|
+
// ---------------- read loop ----------------
|
|
210
|
+
_onData(addr, sock, chunk) {
|
|
211
|
+
this.lastActivity.set(addr, Date.now());
|
|
212
|
+
let buf = this.buffers.get(addr) || Buffer.alloc(0);
|
|
213
|
+
buf = Buffer.concat([buf, chunk]);
|
|
214
|
+
for (;;) {
|
|
215
|
+
if (buf.length < 4)
|
|
216
|
+
break;
|
|
217
|
+
const totalLen = buf.readInt32BE(0);
|
|
218
|
+
if (totalLen <= 0 || totalLen > MAX_FRAME_LENGTH) {
|
|
219
|
+
// A bad length prefix desynchronises the whole stream, so it is not
|
|
220
|
+
// trustworthy: log and take the connection down like a peer close.
|
|
221
|
+
logger.error('illegal frame length %d from %s (must be 1..%d), closing connection', totalLen, addr, MAX_FRAME_LENGTH);
|
|
222
|
+
this.buffers.set(addr, Buffer.alloc(0));
|
|
223
|
+
this._closeSocketForFrameError(sock);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (buf.length < 4 + totalLen)
|
|
227
|
+
break;
|
|
228
|
+
const frame = buf.subarray(0, 4 + totalLen);
|
|
229
|
+
buf = buf.subarray(4 + totalLen);
|
|
230
|
+
if (!this._dispatch(Buffer.from(frame), addr, sock))
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.buffers.set(addr, buf);
|
|
234
|
+
}
|
|
235
|
+
_onClose(addr, sock) {
|
|
236
|
+
// Only drop the pooled entry if it still points at the socket that closed;
|
|
237
|
+
// a reconnect to the same address must not be clobbered by the old socket's
|
|
238
|
+
// late 'close'. The socket-bound fail-fast runs regardless.
|
|
239
|
+
if (this.conns.get(addr) === sock) {
|
|
240
|
+
this.conns.delete(addr);
|
|
241
|
+
this.buffers.delete(addr);
|
|
242
|
+
this.lastActivity.delete(addr);
|
|
243
|
+
}
|
|
244
|
+
this._failFast(addr, sock);
|
|
245
|
+
}
|
|
246
|
+
// Returns true when the connection is still usable, false when this frame tore
|
|
247
|
+
// the connection down and the read loop must stop.
|
|
248
|
+
_dispatch(frame, addr, sock) {
|
|
249
|
+
let cmd;
|
|
250
|
+
try {
|
|
251
|
+
cmd = RemotingCommand.decode(frame);
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
// The bytes no longer line up with the frame boundaries, so nothing that arrives
|
|
255
|
+
// after this frame on this stream can be trusted: report it and take the
|
|
256
|
+
// connection down the way a peer close does, leaving the other connections alone.
|
|
257
|
+
logger.error('decode response failed from %s (%s bytes): %s, closing connection', addr, frame.length, (e instanceof Error ? e.message : String(e)));
|
|
258
|
+
this.buffers.set(addr, Buffer.alloc(0));
|
|
259
|
+
this._closeSocketForFrameError(sock);
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
if (cmd.isResponseType()) {
|
|
263
|
+
const fut = this.responseTable.get(cmd.opaque);
|
|
264
|
+
if (fut) {
|
|
265
|
+
this.responseTable.delete(cmd.opaque);
|
|
266
|
+
fut.conn = sock;
|
|
267
|
+
this._complete(fut, cmd);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
// No pending request owns this opaque: drop it, keep the connection, but
|
|
271
|
+
// say so instead of swallowing it in silence.
|
|
272
|
+
logger.warning('receive response, but not matched any request: opaque=%s, addr=%s', cmd.opaque, addr);
|
|
273
|
+
}
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
const fut = this.responseTable.get(cmd.opaque);
|
|
277
|
+
if (fut) {
|
|
278
|
+
this.responseTable.delete(cmd.opaque);
|
|
279
|
+
fut.conn = sock;
|
|
280
|
+
this._complete(fut, cmd);
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
const handler = this.processors.get(cmd.code);
|
|
284
|
+
if (handler) {
|
|
285
|
+
Promise.resolve(handler(cmd, addr)).then((response) => {
|
|
286
|
+
if (response && !cmd.isOnewayRpc()) {
|
|
287
|
+
response.opaque = cmd.opaque;
|
|
288
|
+
this._write(addr, response).catch((e) => logger.warning('failed to write response: %s', e.message));
|
|
289
|
+
}
|
|
290
|
+
}).catch((e) => {
|
|
291
|
+
logger.warning('processor for code %s raised: %s', cmd.code, e.message);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
logger.debug('no processor for request code %s (opaque=%s) from %s', cmd.code, cmd.opaque, addr);
|
|
296
|
+
}
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
registerProcessor(requestCode, handler) {
|
|
300
|
+
this.processors.set(requestCode, handler);
|
|
301
|
+
}
|
|
302
|
+
unregisterProcessor(requestCode) { this.processors.delete(requestCode); }
|
|
303
|
+
// ---------------- rpc hooks ----------------
|
|
304
|
+
registerRpcHook(hook) { this.rpcHooks.push(hook); }
|
|
305
|
+
unregisterRpcHook(hook) {
|
|
306
|
+
const i = this.rpcHooks.indexOf(hook);
|
|
307
|
+
if (i >= 0)
|
|
308
|
+
this.rpcHooks.splice(i, 1);
|
|
309
|
+
}
|
|
310
|
+
_applyBeforeRequestHooks(addr, cmd) {
|
|
311
|
+
for (const hook of this.rpcHooks.slice())
|
|
312
|
+
hook.doBeforeRequest(addr, cmd);
|
|
313
|
+
}
|
|
314
|
+
_applyAfterResponseHooks(addr, request, response) {
|
|
315
|
+
if (!this.rpcHooks.length || request == null)
|
|
316
|
+
return;
|
|
317
|
+
for (const hook of this.rpcHooks.slice()) {
|
|
318
|
+
try {
|
|
319
|
+
hook.doAfterResponse(addr, request, response);
|
|
320
|
+
}
|
|
321
|
+
catch (e) { /* swallow */ }
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// ---------------- connection events ----------------
|
|
325
|
+
registerChannelListener(listener) { this.channelListeners.push(listener); }
|
|
326
|
+
_fireChannelActive(addr) {
|
|
327
|
+
for (const listener of this.channelListeners.slice()) {
|
|
328
|
+
try {
|
|
329
|
+
const r = listener(addr);
|
|
330
|
+
if (r != null && typeof r.then === 'function') {
|
|
331
|
+
r.catch((e) => {
|
|
332
|
+
logger.warn('channel listener for %s failed: %s', addr, e.message);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
catch (e) {
|
|
337
|
+
logger.warn('channel listener for %s raised: %s', addr, e.message);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// ---------------- write ----------------
|
|
342
|
+
async _send(addr, request) {
|
|
343
|
+
this._applyBeforeRequestHooks(addr, request);
|
|
344
|
+
return this._write(addr, request);
|
|
345
|
+
}
|
|
346
|
+
async _write(addr, request) {
|
|
347
|
+
const sock = await this._connect(addr);
|
|
348
|
+
const data = request.encode();
|
|
349
|
+
await new Promise((resolve, reject) => {
|
|
350
|
+
sock.write(data, (err) => {
|
|
351
|
+
if (err)
|
|
352
|
+
reject(err);
|
|
353
|
+
else {
|
|
354
|
+
this.lastActivity.set(addr, Date.now());
|
|
355
|
+
resolve();
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
return sock;
|
|
360
|
+
}
|
|
361
|
+
async _writeResponse(addr, request, response) {
|
|
362
|
+
try {
|
|
363
|
+
await this._write(addr, response);
|
|
364
|
+
}
|
|
365
|
+
catch (e) {
|
|
366
|
+
logger.warning('failed to write response (code=%s) to %s: %s', response.code, addr, e.message);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// ---------------- invoke sync ----------------
|
|
370
|
+
async invokeSync(addr, request, timeoutMillis) {
|
|
371
|
+
const timeout = timeoutMillis ?? this.invokeTimeoutMillis;
|
|
372
|
+
const started = Date.now();
|
|
373
|
+
const response = await this._invokeOnce(addr, request, timeout);
|
|
374
|
+
if (response.code !== ResponseCode.GO_AWAY)
|
|
375
|
+
return response;
|
|
376
|
+
return this._handleGoAway(addr, request, timeout, started, response);
|
|
377
|
+
}
|
|
378
|
+
async _handleGoAway(addr, request, timeout, started, _response) {
|
|
379
|
+
if (!this.enableReconnectForGoAway)
|
|
380
|
+
throw new RemotingSendRequestException(addr, `Receive GO_AWAY from channel ${addr}`);
|
|
381
|
+
logger.info('receive GO_AWAY from %s, reconnect and retry once', addr);
|
|
382
|
+
this.closeChannel(addr);
|
|
383
|
+
const spent = Date.now() - started;
|
|
384
|
+
const retryTimeout = Math.max(1, timeout - spent);
|
|
385
|
+
const retry = this._retryRequest(request);
|
|
386
|
+
const response = await this._invokeOnce(addr, retry, retryTimeout);
|
|
387
|
+
if (response.code === ResponseCode.GO_AWAY)
|
|
388
|
+
throw new RemotingSendRequestException(addr, `Receive GO_AWAY twice from channel ${addr}`);
|
|
389
|
+
return response;
|
|
390
|
+
}
|
|
391
|
+
_retryRequest(request) {
|
|
392
|
+
const retry = new RemotingCommand(request.code, request.customHeader, request.remark, RemotingCommand.createNewRequestId(), request.flag, request.body);
|
|
393
|
+
retry.language = request.language;
|
|
394
|
+
retry.version = request.version;
|
|
395
|
+
retry.extFields = { ...request.extFields };
|
|
396
|
+
retry.serializeTypeCurrentRpc = request.serializeTypeCurrentRpc;
|
|
397
|
+
return retry;
|
|
398
|
+
}
|
|
399
|
+
// Whether a sync invoke that hits its own deadline should close the connection.
|
|
400
|
+
// Gated by the master flag and by the time still available when the wait began:
|
|
401
|
+
// close unless that budget is both <= 100 ms and <= timeout / 4. This client
|
|
402
|
+
// schedules the deadline as a plain setTimeout(timeout) and does not subtract the
|
|
403
|
+
// connect cost, so the budget when the wait began is the whole timeout.
|
|
404
|
+
_shouldCloseOnSyncTimeout(timeoutMillis) {
|
|
405
|
+
if (!this.closeSocketOnSyncTimeout)
|
|
406
|
+
return false;
|
|
407
|
+
const left = timeoutMillis;
|
|
408
|
+
return !(left <= 100 && left <= timeoutMillis / 4);
|
|
409
|
+
}
|
|
410
|
+
_invokeOnce(addr, request, timeout) {
|
|
411
|
+
return new Promise((resolve, reject) => {
|
|
412
|
+
const fut = {
|
|
413
|
+
opaque: request.opaque, timeoutMillis: timeout, resolve, reject,
|
|
414
|
+
startTime: Date.now(), addr, request, sendRequestOK: false, fired: false,
|
|
415
|
+
};
|
|
416
|
+
this.responseTable.set(request.opaque, fut);
|
|
417
|
+
fut.timer = setTimeout(() => {
|
|
418
|
+
if (!this.responseTable.delete(request.opaque))
|
|
419
|
+
return;
|
|
420
|
+
if (this._shouldCloseOnSyncTimeout(timeout)) {
|
|
421
|
+
logger.warning('invokeSync timed out, closing connection to %s (opaque=%s)', addr, request.opaque);
|
|
422
|
+
this.closeChannel(addr);
|
|
423
|
+
}
|
|
424
|
+
logger.warning('invokeSync request expired: addr=%s, opaque=%s, timeoutMillis=%s', addr, request.opaque, timeout);
|
|
425
|
+
reject(new RemotingTimeoutException(addr, timeout));
|
|
426
|
+
}, timeout);
|
|
427
|
+
this._send(addr, request).then((sock) => {
|
|
428
|
+
fut.conn = sock;
|
|
429
|
+
fut.sendRequestOK = true;
|
|
430
|
+
}).catch((e) => {
|
|
431
|
+
this.responseTable.delete(request.opaque);
|
|
432
|
+
if (fut.timer)
|
|
433
|
+
clearTimeout(fut.timer);
|
|
434
|
+
logger.warning('invokeSync send to %s failed, tearing down the connection: %s', addr, (e instanceof Error ? e.message : String(e)));
|
|
435
|
+
this.closeChannel(addr);
|
|
436
|
+
reject(e instanceof Error ? e : new RemotingSendRequestException(addr, String(e)));
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
// ---------------- invoke async ----------------
|
|
441
|
+
invokeAsync(addr, request, callback, timeoutMillis) {
|
|
442
|
+
const timeout = timeoutMillis ?? this.invokeTimeoutMillis;
|
|
443
|
+
this._invokeAsyncOnce(addr, request, callback, timeout, Date.now());
|
|
444
|
+
}
|
|
445
|
+
_invokeAsyncOnce(addr, request, callback, timeout, started) {
|
|
446
|
+
const onResponse = (cmd, err) => {
|
|
447
|
+
if (cmd == null || err != null || cmd.code !== ResponseCode.GO_AWAY) {
|
|
448
|
+
callback(cmd, err);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
// GO_AWAY: retry once on a fresh thread to avoid blocking this path
|
|
452
|
+
setTimeout(() => {
|
|
453
|
+
try {
|
|
454
|
+
this._handleGoAway(addr, request, timeout, started, cmd).then((r) => callback(r, null)).catch((e) => callback(null, e));
|
|
455
|
+
}
|
|
456
|
+
catch (e) {
|
|
457
|
+
callback(null, e);
|
|
458
|
+
}
|
|
459
|
+
}, 0);
|
|
460
|
+
};
|
|
461
|
+
const fut = {
|
|
462
|
+
opaque: request.opaque, timeoutMillis: timeout, resolve: () => { }, reject: () => { },
|
|
463
|
+
onResponse, startTime: Date.now(), addr, request, sendRequestOK: false, fired: false,
|
|
464
|
+
};
|
|
465
|
+
this.responseTable.set(request.opaque, fut);
|
|
466
|
+
fut.timer = setTimeout(() => {
|
|
467
|
+
if (!this.responseTable.delete(request.opaque))
|
|
468
|
+
return;
|
|
469
|
+
logger.warning('invokeAsync request expired: addr=%s, opaque=%s, timeoutMillis=%s', addr, request.opaque, timeout);
|
|
470
|
+
// This body runs on Node's timer stack: a throw from the caller's callback
|
|
471
|
+
// would surface as an uncaughtException and kill the process, so it must not
|
|
472
|
+
// escape here.
|
|
473
|
+
try {
|
|
474
|
+
callback(null, new RemotingTimeoutException(addr, timeout));
|
|
475
|
+
}
|
|
476
|
+
catch (e) {
|
|
477
|
+
logger.error('invokeAsync timeout callback threw for %s: %s', addr, e.message);
|
|
478
|
+
}
|
|
479
|
+
}, timeout);
|
|
480
|
+
this._send(addr, request).then((sock) => {
|
|
481
|
+
fut.conn = sock;
|
|
482
|
+
fut.sendRequestOK = true;
|
|
483
|
+
}).catch((e) => {
|
|
484
|
+
this.responseTable.delete(request.opaque);
|
|
485
|
+
if (fut.timer)
|
|
486
|
+
clearTimeout(fut.timer);
|
|
487
|
+
callback(null, e instanceof Error ? e : new RemotingSendRequestException(addr, String(e)));
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
// ---------------- invoke oneway ----------------
|
|
491
|
+
async invokeOneway(addr, request) {
|
|
492
|
+
request.markOnewayRpc();
|
|
493
|
+
await this._send(addr, request);
|
|
494
|
+
}
|
|
495
|
+
// ---------------- completion ----------------
|
|
496
|
+
_complete(fut, cmd) {
|
|
497
|
+
if (fut.timer)
|
|
498
|
+
clearTimeout(fut.timer);
|
|
499
|
+
this._applyAfterResponseHooks(fut.addr, fut.request, cmd);
|
|
500
|
+
if (fut.onResponse) {
|
|
501
|
+
fut.onResponse(cmd, null);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
fut.resolve(cmd);
|
|
505
|
+
}
|
|
506
|
+
_failFast(addr, sock) {
|
|
507
|
+
const doomed = [];
|
|
508
|
+
for (const [opaque, fut] of this.responseTable) {
|
|
509
|
+
if (fut.conn === sock) {
|
|
510
|
+
if (this.responseTable.delete(opaque))
|
|
511
|
+
doomed.push(fut);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (const fut of doomed) {
|
|
515
|
+
fut.sendRequestOK = false;
|
|
516
|
+
fut.conn = undefined;
|
|
517
|
+
if (fut.onResponse) {
|
|
518
|
+
fut.onResponse(null, new RemotingSendRequestException(fut.addr, 'connection closed'));
|
|
519
|
+
}
|
|
520
|
+
else
|
|
521
|
+
fut.reject(new RemotingSendRequestException(fut.addr, 'connection closed'));
|
|
522
|
+
}
|
|
523
|
+
if (doomed.length)
|
|
524
|
+
logger.warning('connection to %s closed, %d in-flight request(s) failed fast', addr, doomed.length);
|
|
525
|
+
return doomed.length;
|
|
526
|
+
}
|
|
527
|
+
// ---------------- idle connection sweep ----------------
|
|
528
|
+
// The effective idle limit: a non-positive configured value falls back to the
|
|
529
|
+
// default (it does not disable the sweep — the sweep is always installed).
|
|
530
|
+
_idleLimitMillis() {
|
|
531
|
+
return this.maxIdleTimeMillis > 0 ? this.maxIdleTimeMillis : DEFAULT_MAX_IDLE_TIME_MILLIS;
|
|
532
|
+
}
|
|
533
|
+
// Install the sweep once, lazily, on the first connection. The interval is a
|
|
534
|
+
// fraction of the idle limit so detection is prompt for small limits yet cheap for
|
|
535
|
+
// the 120 s default. unref() keeps a test process from hanging on this timer.
|
|
536
|
+
_ensureIdleSweep() {
|
|
537
|
+
if (this.idleSweepTimer != null || !this.running)
|
|
538
|
+
return;
|
|
539
|
+
const tick = Math.max(200, Math.floor(this._idleLimitMillis() / 4));
|
|
540
|
+
const timer = setInterval(() => this._sweepIdleConnections(), tick);
|
|
541
|
+
if (typeof timer.unref === 'function')
|
|
542
|
+
timer.unref();
|
|
543
|
+
this.idleSweepTimer = timer;
|
|
544
|
+
}
|
|
545
|
+
// Close any connection idle strictly past the configured limit, WARNing the
|
|
546
|
+
// address; the close goes through the peer-close route (destroy -> 'close' ->
|
|
547
|
+
// _onClose -> _failFast), so its pending entries fail fast and other connections
|
|
548
|
+
// are untouched.
|
|
549
|
+
_sweepIdleConnections() {
|
|
550
|
+
const limit = this._idleLimitMillis();
|
|
551
|
+
const now = Date.now();
|
|
552
|
+
for (const [addr, sock] of this.conns) {
|
|
553
|
+
const last = this.lastActivity.get(addr);
|
|
554
|
+
if (last == null) {
|
|
555
|
+
this.lastActivity.set(addr, now);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
if (now - last > limit) {
|
|
559
|
+
logger.warning('closing idle connection to %s: no traffic for %s ms (limit %s ms)', addr, now - last, limit);
|
|
560
|
+
try {
|
|
561
|
+
if (!sock.destroyed)
|
|
562
|
+
sock.destroy();
|
|
563
|
+
}
|
|
564
|
+
catch { /* ignore */ }
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
shutdown() {
|
|
569
|
+
this.running = false;
|
|
570
|
+
if (this.idleSweepTimer != null) {
|
|
571
|
+
clearInterval(this.idleSweepTimer);
|
|
572
|
+
this.idleSweepTimer = null;
|
|
573
|
+
}
|
|
574
|
+
for (const fut of this.responseTable.values()) {
|
|
575
|
+
if (fut.timer)
|
|
576
|
+
clearTimeout(fut.timer);
|
|
577
|
+
}
|
|
578
|
+
this.responseTable.clear();
|
|
579
|
+
for (const [, sock] of this.conns) {
|
|
580
|
+
try {
|
|
581
|
+
if (!sock.destroyed)
|
|
582
|
+
sock.destroy();
|
|
583
|
+
}
|
|
584
|
+
catch (e) { /* ignore */ }
|
|
585
|
+
}
|
|
586
|
+
this.conns.clear();
|
|
587
|
+
this.buffers.clear();
|
|
588
|
+
this.lastActivity.clear();
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
export default RemotingClient;
|