-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwsConnector.ts
More file actions
800 lines (724 loc) · 26.1 KB
/
Copy pathwsConnector.ts
File metadata and controls
800 lines (724 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
import { ICloseEvent, w3cwebsocket } from "websocket";
import {
Address,
Dsn,
mergeAddresses,
parseDiscoveredEndpoints
} from "../common/dsn";
import { AddressConnectionTracker } from "../common/addressConnectionTracker";
import {
ErrorCode,
TDWebSocketClientError,
WebSocketQueryError,
} from "../common/wsError";
import { OnMessageType, WsEventCallback } from "./wsEventCallback";
import logger from "../common/log";
import {
maskSensitiveForLog,
maskUrlForLog
} from "../common/utils";
import { ClusterRegistry } from "./clusterRegistry";
interface InflightRequest {
reqId: bigint;
action: string;
id?: bigint;
message: string | ArrayBuffer;
resolve: (value: unknown) => void;
reject: (error: unknown) => void;
}
class InflightRequestStore {
private nextMsgId = 1n;
private readonly reqIdToMsgId: Map<bigint, bigint> = new Map();
private readonly msgIdToRequest: Map<bigint, InflightRequest> = new Map();
insert(req: InflightRequest): void {
const msgId = this.nextMsgId;
this.nextMsgId += 1n;
this.reqIdToMsgId.set(req.reqId, msgId);
this.msgIdToRequest.set(msgId, req);
}
remove(reqId: bigint): void {
const msgId = this.reqIdToMsgId.get(reqId);
if (msgId === undefined) {
return;
}
this.reqIdToMsgId.delete(reqId);
this.msgIdToRequest.delete(msgId);
}
getRequests(): InflightRequest[] {
return Array.from(this.msgIdToRequest.entries())
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
.map(([, req]) => req);
}
clear(): void {
this.nextMsgId = 1n;
this.reqIdToMsgId.clear();
this.msgIdToRequest.clear();
}
}
export type SessionRecoveryHook = () => Promise<void>;
const RETRIABLE_ACTIONS = new Set(["insert", "options_connection", "poll", "subscribe"]);
// TDengine websocket binary op codes that are safe to replay after reconnect.
const BINARY_RETRIABLE_ACTIONS = new Set<bigint>([4n, 5n, 6n, 10n]);
const DEFAULT_RETRIES = 5;
const DEFAULT_BACKOFF_MS = 200;
const DEFAULT_BACKOFF_MAX_MS = 2000;
const NETWORK_ERROR_CODES = new Set([
"econnreset",
"econnrefused",
"ehostunreach",
"enotfound",
"eai_again",
"epipe",
"etimedout",
"err_socket_closed",
"err_stream_write_after_end",
]);
const NETWORK_ERROR_MESSAGE_PATTERNS = [
"not connected",
"socket",
"network",
"econnreset",
"econnrefused",
"epipe",
"etimedout",
"write after end",
"broken pipe",
"connection reset",
"connection closed",
];
function parseNonNegativeInt(value: string | undefined | null, fallback: number): number {
return parseIntWithValidation(value, fallback, (parsed) => parsed >= 0);
}
function parsePositiveInt(value: string | undefined | null, fallback: number): number {
return parseIntWithValidation(value, fallback, (parsed) => parsed > 0);
}
function parseIntWithValidation(
value: string | undefined | null,
fallback: number,
isValid: (parsed: number) => boolean
): number {
if (value === undefined || value === null || value.length === 0) {
return fallback;
}
const parsed = Number.parseInt(value, 10);
if (Number.isNaN(parsed) || !isValid(parsed)) {
return fallback;
}
return parsed;
}
export class RetryConfig {
readonly retries: number;
readonly retryBackoffMs: number;
readonly retryBackoffMaxMs: number;
constructor(retries: number, retryBackoffMs: number, retryBackoffMaxMs: number) {
this.retries = retries;
this.retryBackoffMs = retryBackoffMs;
this.retryBackoffMaxMs = Math.max(retryBackoffMs, retryBackoffMaxMs);
}
getBackoffDelay(attempt: number): number {
const safeAttempt = Math.max(0, attempt);
const rawDelay = this.retryBackoffMs * Math.pow(2, safeAttempt);
const finiteDelay = Number.isFinite(rawDelay) ? rawDelay : this.retryBackoffMaxMs;
return Math.min(finiteDelay, this.retryBackoffMaxMs);
}
static fromDsn(dsn: Dsn): RetryConfig {
const retries = parseNonNegativeInt(
dsn.params.get("retries"),
DEFAULT_RETRIES
);
const retryBackoffMs = parsePositiveInt(
dsn.params.get("retry_backoff_ms"),
DEFAULT_BACKOFF_MS
);
const retryBackoffMaxMs = parsePositiveInt(
dsn.params.get("retry_backoff_max_ms"),
DEFAULT_BACKOFF_MAX_MS
);
return new RetryConfig(retries, retryBackoffMs, retryBackoffMaxMs);
}
}
export class WebSocketConnector {
private _conn!: w3cwebsocket;
private readonly _poolKey: string;
private readonly _dsn: Dsn;
private _failoverAddresses: Address[];
private _currentAddress: Address;
private _retryConfig: RetryConfig;
private _inflightStore: InflightRequestStore;
private readonly _suppressedSockets: WeakSet<w3cwebsocket> = new WeakSet();
private _reconnectLock: Promise<void> | null = null;
private _isReconnecting = false;
private _allowReconnect = true;
private _connectionReady: Promise<void> = Promise.resolve();
private _sessionRecoveryHook: SessionRecoveryHook | null = null;
private _sessionReady = false;
private _timeout = 60000;
constructor(
dsn: Dsn,
poolKey: string,
timeout: number | undefined | null
) {
if (!dsn || dsn.addresses.length === 0) {
throw new WebSocketQueryError(
ErrorCode.ERR_INVALID_URL,
"websocket URL must be defined"
);
}
this._poolKey = poolKey;
this._dsn = dsn;
this._failoverAddresses = dsn.addresses.map(
(address) => new Address(address.host, address.port)
);
if (dsn.isAdapterHA()) {
const expanded = ClusterRegistry.instance().expandEndpoints(this._failoverAddresses);
if (expanded.length > this._failoverAddresses.length) {
logger.info(
"Adapter HA: expanded seed endpoints from registry, " +
`${this._failoverAddresses.length} -> ${expanded.length}`
);
this._failoverAddresses = expanded;
}
}
this._currentAddress = this.selectLeastConnectedAddress();
this._retryConfig = RetryConfig.fromDsn(dsn);
this._inflightStore = new InflightRequestStore();
if (timeout) {
this._timeout = timeout;
}
logger.info(`Initial websocket address selected: ${this.getCurrentAddress()}`);
this.createConnection();
}
public refreshRetryConfig(dsn: Dsn): void {
this._retryConfig = RetryConfig.fromDsn(dsn);
}
private buildUrl(addr: Address): string {
const path = this._dsn.path();
const url = new URL(`${this._dsn.scheme}://${addr.host}:${addr.port}/${path}`);
const forwardedParams = ["token", "bearer_token"];
for (const key of forwardedParams) {
const value = this._dsn.params.get(key);
if (value !== undefined) {
url.searchParams.set(key, value);
}
}
return url.toString();
}
private createConnection(): void {
this._sessionReady = false;
const conn = new w3cwebsocket(
this.buildUrl(this._currentAddress),
undefined,
undefined,
undefined,
undefined,
{
maxReceivedFrameSize: 0x60000000,
maxReceivedMessageSize: 0x60000000,
}
);
conn._binaryType = "arraybuffer";
conn.onmessage = this._onmessage;
this._connectionReady = new Promise((resolve, reject) => {
let settled = false;
const settle = (handler: () => void) => {
if (settled) {
return;
}
settled = true;
clearTimeout(timeoutId);
handler();
};
const timeoutId = setTimeout(() => {
settle(() => {
reject(
new WebSocketQueryError(
ErrorCode.ERR_WEBSOCKET_QUERY_TIMEOUT,
`websocket connection timeout with ${this._timeout} milliseconds`
)
);
});
}, this._timeout);
conn.onopen = () => {
logger.debug("websocket connection opened");
AddressConnectionTracker.instance().increment(this.getCurrentAddress());
settle(resolve);
};
conn.onerror = (err: Error) => {
logger.error(
`webSocket connection failed, url: ${maskUrlForLog(new URL(conn.url))}, error: ${err.message}`
);
if (conn.readyState !== w3cwebsocket.OPEN) {
settle(() => reject(err));
}
void this.handleDisconnect(conn);
};
conn.onclose = (e: ICloseEvent) => {
logger.info(`websocket connection closed, code: ${e.code}, reason: ${e.reason}`);
if (conn.readyState !== w3cwebsocket.OPEN) {
settle(() => {
reject(
new WebSocketQueryError(
ErrorCode.ERR_WEBSOCKET_CONNECTION_FAIL,
`websocket connection closed: ${e.code} ${e.reason}`
)
);
});
}
void this.handleDisconnect(conn, e);
};
});
this._conn = conn;
}
private _onmessage(event: any) {
let data = event.data;
logger.debug("wsClient._onMessage()====" + Object.prototype.toString.call(data));
if (Object.prototype.toString.call(data) === "[object ArrayBuffer]") {
let id = new DataView(data, 26, 8).getBigUint64(0, true);
WsEventCallback.instance().handleEventCallback(
{ id: id, action: "", req_id: BigInt(0) },
OnMessageType.MESSAGE_TYPE_ARRAYBUFFER,
data
);
} else if (Object.prototype.toString.call(data) === "[object String]") {
let msg = JSON.parse(data);
logger.debug("[_onmessage.stringType]==>:" + data);
WsEventCallback.instance().handleEventCallback(
{ id: BigInt(0), action: msg.action, req_id: msg.req_id },
OnMessageType.MESSAGE_TYPE_STRING,
msg
);
} else {
throw new TDWebSocketClientError(
ErrorCode.ERR_INVALID_MESSAGE_TYPE,
`invalid message type ${Object.prototype.toString.call(data)}`
);
}
}
private shouldSkipReconnect(conn: w3cwebsocket): boolean {
if (!this.isReconnectAllowed()) {
return true;
}
return this._suppressedSockets.has(conn);
}
private async failNonRetriableCallbacksOnDisconnect(): Promise<void> {
const keepReqIds = new Set<bigint>(
this._inflightStore.getRequests().map((req) => req.reqId)
);
await WsEventCallback.instance().rejectCallbacksExceptReqIds(
keepReqIds,
new TDWebSocketClientError(
ErrorCode.ERR_CONNECTION_CLOSED,
"websocket connection closed before response was received"
),
this._poolKey
);
}
private async handleDisconnect(conn: w3cwebsocket, event?: ICloseEvent): Promise<void> {
if (this.shouldSkipReconnect(conn) || this._isReconnecting) {
return;
}
if (event && event.code === 1000) {
logger.info("Websocket closed normally, skipping reconnect.");
return;
}
await this.failNonRetriableCallbacksOnDisconnect();
try {
await this.triggerReconnect();
} catch (err: unknown) {
const type = event ? 'close' : 'error';
const message = err instanceof Error ? err.message : String(err);
logger.error(`Reconnect failed after websocket ${type}: ${message}`);
}
}
private extractReqId(reqId: unknown): bigint | null {
if (reqId === undefined || reqId === null) {
return null;
}
try {
return BigInt(reqId as bigint | number | string);
} catch (err) {
return null;
}
}
private isRetriableAction(action: string): boolean {
return RETRIABLE_ACTIONS.has(action);
}
private extractBinaryAction(message: ArrayBuffer): bigint {
if (message.byteLength < 24) {
return -1n;
}
return new DataView(message, 16, 8).getBigInt64(0, true);
}
private isRetriableBinaryAction(action: bigint): boolean {
return BINARY_RETRIABLE_ACTIONS.has(action);
}
public isNetworkError(err: unknown): boolean {
if (!this._conn || this._conn.readyState !== w3cwebsocket.OPEN) {
return true;
}
const errObj = err as { code?: unknown; message?: unknown };
const code = errObj?.code;
if (typeof code === "number" && code === ErrorCode.ERR_CONNECTION_CLOSED) {
return true;
}
if (typeof code === "string") {
const loweredCode = code.toLowerCase();
if (loweredCode.length > 0 && NETWORK_ERROR_CODES.has(loweredCode)) {
return true;
}
}
const message = err instanceof Error
? err.message
: typeof errObj?.message === "string"
? errObj.message
: String(err);
const lowered = message.toLowerCase();
return NETWORK_ERROR_MESSAGE_PATTERNS.some((pattern) =>
lowered.includes(pattern)
);
}
private send(message: string | ArrayBuffer, triggerReconnect: boolean = true): void {
try {
this._conn.send(message);
} catch (err) {
if (triggerReconnect && this.isNetworkError(err)) {
void this.triggerReconnect().catch((err: unknown) => {
const message = err instanceof Error ? err.message : String(err);
logger.error(`Reconnect trigger failed: ${message}`);
});
}
throw err;
}
}
async ready(): Promise<void> {
if (this._conn && this._conn.readyState === w3cwebsocket.OPEN) {
return;
}
if (this._reconnectLock) {
await this._reconnectLock;
return;
}
try {
await this._connectionReady;
} catch (err) {
if (this._reconnectLock) {
await this._reconnectLock;
return;
}
throw err;
}
}
private getCurrentAddress(): string {
return `${this._currentAddress.host}:${this._currentAddress.port}`;
}
private selectLeastConnectedAddress(excludedAddresses: Set<string> = new Set()): Address {
const candidates = this._failoverAddresses.filter((address) => {
const addressKey = `${address.host}:${address.port}`;
return !excludedAddresses.has(addressKey);
});
const selectableAddresses = candidates.length > 0 ? candidates : this._failoverAddresses;
const selectedIndex = AddressConnectionTracker.instance()
.selectLeastConnected(selectableAddresses);
return selectableAddresses[selectedIndex];
}
private async sleep(ms: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, ms));
}
private isReconnectAllowed(): boolean {
return this._allowReconnect;
}
private async triggerReconnect(): Promise<void> {
if (!this.isReconnectAllowed()) {
return;
}
if (!this._reconnectLock) {
this._reconnectLock = this._doReconnect();
}
const lock = this._reconnectLock;
try {
await lock;
} finally {
if (this._reconnectLock === lock) {
this._reconnectLock = null;
}
}
}
private async _doReconnect(): Promise<void> {
if (!this.isReconnectAllowed()) {
return;
}
this._isReconnecting = true;
try {
await this.attemptReconnect();
} catch (err: unknown) {
const reconnectError = err instanceof Error
? err
: new Error("unknown reconnect error");
this.failAllInflightRequests(reconnectError);
throw reconnectError;
} finally {
this._isReconnecting = false;
}
}
private async reconnect(): Promise<void> {
if (this._conn) {
AddressConnectionTracker.instance().decrement(this.getCurrentAddress());
this._suppressedSockets.add(this._conn);
this._conn.close();
}
this.createConnection();
await this._connectionReady;
}
private async attemptReconnect(): Promise<void> {
const totalAddresses = this._failoverAddresses.length;
const failedAddresses = new Set<string>();
for (let i = 0; i < totalAddresses; i++) {
for (let retry = 0; retry < this._retryConfig.retries; retry++) {
if (!this.isReconnectAllowed()) {
return;
}
try {
logger.info(`Reconnecting to ${this.getCurrentAddress()}, attempt ${retry + 1}`);
await this.reconnect();
await this.recoverSessionContext();
await this.replayRequests();
logger.info(`Reconnection successful to ${this.getCurrentAddress()}`);
return;
} catch (err: any) {
logger.warn(`Reconnect failed: ${err.message}`);
if (retry < this._retryConfig.retries - 1) {
const delay = this._retryConfig.getBackoffDelay(retry);
await this.sleep(delay);
}
}
}
if (i < totalAddresses - 1) {
failedAddresses.add(this.getCurrentAddress());
this._currentAddress = this.selectLeastConnectedAddress(failedAddresses);
logger.info(`Switching to least-connected address: ${this.getCurrentAddress()}`);
}
}
throw new TDWebSocketClientError(
ErrorCode.ERR_WEBSOCKET_CONNECTION_FAIL,
"Failed to reconnect to any available address"
);
}
private async replayRequests(): Promise<void> {
logger.info("Replaying requests after reconnection");
for (const req of this._inflightStore.getRequests()) {
try {
this.send(req.message, false);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
if (this.isNetworkError(err)) {
logger.warn(`Network error while replaying inflight request, stopping current replay round: ${message}`);
throw err;
}
logger.error(`Failed to replay inflight request: ${req}, error: ${message}`);
req.reject(err);
}
}
}
private failAllInflightRequests(error: Error): void {
for (const req of this._inflightStore.getRequests()) {
req.reject(error);
}
this._inflightStore.clear();
}
close() {
this._allowReconnect = false;
this.failAllInflightRequests(
new TDWebSocketClientError(
ErrorCode.ERR_CONNECTION_CLOSED,
"websocket connection closed"
)
);
if (this._conn) {
AddressConnectionTracker.instance().decrement(this.getCurrentAddress());
this._suppressedSockets.add(this._conn);
this._conn.close();
} else {
logger.warn("close() called but websocket connection is undefined");
}
}
readyState(): number {
return this._conn.readyState;
}
public setSessionRecoveryHook(
hook: SessionRecoveryHook | undefined | null
): void {
this._sessionRecoveryHook = hook || null;
}
public isSessionReady(): boolean {
return this._sessionReady;
}
public markSessionReady(): void {
this._sessionReady = true;
}
public mergeDiscoveredEndpoints(instances: string[]): void {
if (!this._dsn.isAdapterHA() || !instances || instances.length === 0) {
return;
}
const discovered = parseDiscoveredEndpoints(instances);
const merged = mergeAddresses(this._failoverAddresses, discovered);
if (merged.length <= this._failoverAddresses.length) {
return;
}
const newCount = merged.length - this._failoverAddresses.length;
this._failoverAddresses = merged;
ClusterRegistry.instance().registerCluster(merged);
logger.info(
`Adapter HA: discovered ${newCount} new endpoint(s), total ${merged.length}`
);
}
private async recoverSessionContext(): Promise<void> {
if (!this._sessionRecoveryHook) {
return;
}
await this._sessionRecoveryHook();
}
public async sendMsgDirect(message: string): Promise<any> {
if (logger.isDebugEnabled()) {
logger.debug("[wsClient.sendMsgDirect]===>" + maskSensitiveForLog(message));
}
const msg = JSON.parse(message);
const reqId = this.extractReqId(msg?.args?.req_id) ?? BigInt(0);
let resolveResp: (value: unknown) => void = () => { };
let rejectResp: (error: unknown) => void = () => { };
const responsePromise = new Promise((resolve, reject) => {
resolveResp = resolve;
rejectResp = reject;
});
await WsEventCallback.instance().registerCallback(
{
action: msg.action,
req_id: reqId,
timeout: this._timeout,
poolKey: this._poolKey,
},
resolveResp,
rejectResp
);
try {
this.send(message, false);
return await responsePromise;
} catch (err) {
await WsEventCallback.instance().unregisterCallback(reqId);
throw err;
}
}
async sendMsgNoResp(message: string): Promise<void> {
logger.debug("[wsClient.sendMsgNoResp]===>" + message);
if (this._reconnectLock) {
await this._reconnectLock;
}
this.send(message);
}
async sendMsg(message: string) {
if (logger.isDebugEnabled()) {
logger.debug("[wsConnector.sendMsg]===>" + maskSensitiveForLog(message));
}
const msg = JSON.parse(message);
const id = msg?.args?.id !== undefined ? BigInt(msg.args.id) : undefined;
const reqId = this.extractReqId(msg?.args?.req_id) ?? BigInt(0);
return this.sendAndTrackResponse(
reqId,
msg.action,
message,
this.isRetriableAction(msg.action),
id,
);
}
async sendBinaryMsg(
reqId: bigint,
action: string,
message: ArrayBuffer,
) {
return this.sendAndTrackResponse(
reqId,
action,
message,
this.isRetriableBinaryAction(this.extractBinaryAction(message)),
reqId,
);
}
private async sendAndTrackResponse(
reqId: bigint,
action: string,
message: string | ArrayBuffer,
retriable: boolean,
callbackId?: bigint,
) {
if (this._reconnectLock) {
await this._reconnectLock;
}
return new Promise((resolve, reject) => {
let settled = false;
const safeResolve = (result: unknown) => {
if (settled) {
return;
}
settled = true;
if (retriable) {
this._inflightStore.remove(reqId);
}
resolve(result);
};
const safeReject = (error: unknown) => {
if (settled) {
return;
}
settled = true;
if (retriable) {
this._inflightStore.remove(reqId);
}
void WsEventCallback.instance().unregisterCallback(reqId);
reject(error);
};
const registerAndSend = async () => {
if (retriable) {
this._inflightStore.insert({
reqId,
action,
id: callbackId,
message,
resolve: safeResolve,
reject: safeReject,
});
}
await WsEventCallback.instance().registerCallback(
{
action,
req_id: reqId,
timeout: this._timeout,
id: callbackId,
poolKey: this._poolKey,
},
safeResolve,
safeReject
);
if (settled) {
await WsEventCallback.instance().unregisterCallback(reqId);
return;
}
try {
this.send(message);
} catch (err) {
if (retriable && this.isNetworkError(err)) {
return;
}
safeReject(err);
}
};
void registerAndSend().catch((error) => {
safeReject(error);
});
});
}
public getPoolKey(): string {
return this._poolKey;
}
public getReconnectRetries(): number {
return this._retryConfig.retries;
}
}