-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection-manager.ts
More file actions
858 lines (742 loc) · 23 KB
/
connection-manager.ts
File metadata and controls
858 lines (742 loc) · 23 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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import { createRequire } from "node:module";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import process from "node:process";
import manifest from "../../deno.json" with { type: "json" };
import { isAbortError } from "../utils.ts";
import { redactEndpointUserInfo } from "./endpoint-redaction.ts";
import { logger } from "./logger.ts";
type McpClientInstance = {
connect(transport: unknown): Promise<void>;
close(): Promise<void>;
callTool(
request: GraphitiToolRequest,
metadata?: unknown,
options?: GraphitiRequestOptions,
): Promise<unknown>;
};
type McpClientConstructor = new (
clientInfo: { name: string; version: string },
) => McpClientInstance;
type McpTransportConstructor = new (url: URL) => unknown;
type McpRuntimeModules = {
Client: McpClientConstructor;
StreamableHTTPClientTransport: McpTransportConstructor;
};
const nodeRequire = createRequire(
pathToFileURL(join(process.cwd(), "graphiti.runtime.cjs")).href,
);
let mcpRuntimeModulesPromise: Promise<McpRuntimeModules> | null = null;
const importResolvedModule = async <T>(specifier: string): Promise<T> => {
const resolvedPath = nodeRequire.resolve(specifier);
return await import(pathToFileURL(resolvedPath).href) as T;
};
const loadMcpRuntimeModules = async (): Promise<McpRuntimeModules> => {
if (mcpRuntimeModulesPromise) {
return await mcpRuntimeModulesPromise;
}
mcpRuntimeModulesPromise = Promise.all([
importResolvedModule<{ Client: McpClientConstructor }>(
"@modelcontextprotocol/sdk/client/index.js",
),
importResolvedModule<{
StreamableHTTPClientTransport: McpTransportConstructor;
}>("@modelcontextprotocol/sdk/client/streamableHttp.js"),
]).then(([clientModule, transportModule]) => ({
Client: clientModule.Client,
StreamableHTTPClientTransport:
transportModule.StreamableHTTPClientTransport,
}));
return await mcpRuntimeModulesPromise;
};
export type GraphitiConnectionState =
| "connecting"
| "connected"
| "offline"
| "closing"
| "stopped";
type TimerHandle = ReturnType<typeof setTimeout> | number;
export class GraphitiOfflineError extends Error {
readonly kind = "offline";
constructor(
readonly state: "offline" | "closing" | "stopped",
message?: string,
) {
super(
message ??
(state === "closing"
? "Graphiti connection manager is closing"
: state === "stopped"
? "Graphiti connection manager is stopped"
: "Graphiti connection manager is offline"),
);
this.name = "GraphitiOfflineError";
}
}
export class GraphitiQueueTimeoutError extends Error {
readonly kind = "queue-timeout";
constructor(
message = "Graphiti request timed out while waiting for connection",
) {
super(message);
this.name = "GraphitiQueueTimeoutError";
}
}
export class GraphitiRequestTimeoutError extends Error {
readonly kind = "request-timeout";
constructor(message = "Graphiti request timed out") {
super(message);
this.name = "GraphitiRequestTimeoutError";
}
}
export class GraphitiTransportError extends Error {
readonly kind = "transport-failure";
constructor(message = "Graphiti transport failure") {
super(message);
this.name = "GraphitiTransportError";
}
}
export class GraphitiSessionExpiredError extends Error {
readonly kind = "session-expired";
constructor(message = "Graphiti session expired") {
super(message);
this.name = "GraphitiSessionExpiredError";
}
}
export function isGraphitiOfflineError(
err: unknown,
): err is GraphitiOfflineError {
return err instanceof GraphitiOfflineError;
}
export function isGraphitiTimeoutError(
err: unknown,
): err is GraphitiQueueTimeoutError | GraphitiRequestTimeoutError {
return err instanceof GraphitiQueueTimeoutError ||
err instanceof GraphitiRequestTimeoutError;
}
export type GraphitiToolRequest = {
name: string;
arguments?: Record<string, unknown>;
};
export type GraphitiRequestOptions = {
signal?: AbortSignal;
};
export interface GraphitiConnection {
connect(): Promise<void>;
close(): Promise<void>;
callTool(
request: GraphitiToolRequest,
options?: GraphitiRequestOptions,
): Promise<unknown>;
}
export interface GraphitiToolCaller {
start(): void;
stop(): Promise<void>;
ready(timeoutMs?: number): Promise<boolean>;
callTool(
name: string,
args: Record<string, unknown>,
deadlineMs?: number,
): Promise<unknown>;
}
type PendingRequest = {
name: string;
args: Record<string, unknown>;
deadlineAt: number;
resolve: (value: unknown) => void;
reject: (reason?: unknown) => void;
timer: TimerHandle | null;
};
type ConnectionFactory = (endpoint: string) => GraphitiConnection;
const validateEndpoint = (endpoint: string): string => {
const normalized = endpoint.trim();
if (!normalized) {
throw new Error("Graphiti endpoint must not be empty");
}
try {
new URL(normalized);
} catch (cause) {
const error = new Error(
`Invalid Graphiti endpoint: ${
JSON.stringify(redactEndpointUserInfo(normalized))
}`,
);
Object.defineProperty(error, "cause", {
value: cause,
writable: true,
configurable: true,
enumerable: false,
});
throw error;
}
return normalized;
};
type GraphitiConnectionManagerOptions = {
endpoint: string;
requestDeadlineMs?: number;
queueCapacity?: number;
startupTimeoutMs?: number;
reconnectInitialDelayMs?: number;
reconnectMaxDelayMs?: number;
reconnectMultiplier?: number;
reconnectJitter?: number;
connectionFactory?: ConnectionFactory;
random?: () => number;
now?: () => number;
setTimer?: (callback: () => void, delayMs: number) => TimerHandle;
clearTimer?: (timer: TimerHandle) => void;
};
function createMcpConnection(endpoint: string): GraphitiConnection {
let runtimePromise:
| Promise<{ client: McpClientInstance; transport: unknown }>
| null = null;
const getRuntime = async (): Promise<{
client: McpClientInstance;
transport: unknown;
}> => {
if (runtimePromise) {
return await runtimePromise;
}
runtimePromise = loadMcpRuntimeModules().then(
({ Client, StreamableHTTPClientTransport }) => ({
client: new Client({
name: manifest.name,
version: manifest.version,
}),
transport: new StreamableHTTPClientTransport(new URL(endpoint)),
}),
);
return await runtimePromise;
};
return {
connect: async () => {
const { client, transport } = await getRuntime();
await client.connect(transport);
},
close: async () => {
const { client } = await getRuntime();
await client.close();
},
callTool: async (request, options) => {
const { client } = await getRuntime();
return await client.callTool(request, undefined, options);
},
};
}
type RawErrorShape = {
code?: unknown;
message?: unknown;
cause?: unknown;
name?: unknown;
};
function getErrorMessage(err: unknown): string {
if (typeof err === "string") return err;
if (!err || typeof err !== "object") return "";
const { message, cause } = err as RawErrorShape;
if (typeof message === "string") return message;
if (cause) return getErrorMessage(cause);
return "";
}
function isRequestTimeout(err: unknown): boolean {
if (!err || typeof err !== "object") {
return typeof err === "string" && /request timed out/i.test(err);
}
const { code } = err as RawErrorShape;
return code === -32001 || /request timed out/i.test(getErrorMessage(err));
}
function isSessionExpired(err: unknown): boolean {
return !!(
err &&
typeof err === "object" &&
"code" in err &&
(err as RawErrorShape).code === 404
);
}
function isTransportFailure(err: unknown): boolean {
if (!err) return false;
if (isRequestTimeout(err) || isSessionExpired(err)) return false;
const message = getErrorMessage(err);
if (
/(socket hang up|fetch failed|network|connection reset|connection refused|econnrefused|terminated|broken pipe|stream closed|unexpected end|transport)/i
.test(message)
) {
return true;
}
if (typeof err === "object") {
const { name } = err as RawErrorShape;
return name === "TypeError" && message.length > 0;
}
return false;
}
export class GraphitiConnectionManager implements GraphitiToolCaller {
private readonly endpoint: string;
private readonly redactedEndpoint: string;
private readonly requestDeadlineMs: number;
private readonly queueCapacity: number;
private readonly startupTimeoutMs: number;
private readonly reconnectInitialDelayMs: number;
private readonly reconnectMaxDelayMs: number;
private readonly reconnectMultiplier: number;
private readonly reconnectJitter: number;
private readonly connectionFactory: ConnectionFactory;
private readonly random: () => number;
private readonly now: () => number;
private readonly setTimerImpl: (
callback: () => void,
delayMs: number,
) => TimerHandle;
private readonly clearTimerImpl: (timer: TimerHandle) => void;
private state: GraphitiConnectionState = "offline";
private connection: GraphitiConnection | null = null;
private connectPromise: Promise<boolean> | null = null;
private reconnectTimer: TimerHandle | null = null;
private pendingRequests: PendingRequest[] = [];
private readyWaiters = new Set<(value: boolean) => void>();
private reconnectDelayMs: number;
private started = false;
private flushingQueue = false;
private stopPromise: Promise<void> | null = null;
private activeRequestControllers = new Set<AbortController>();
constructor(options: GraphitiConnectionManagerOptions) {
this.endpoint = validateEndpoint(options.endpoint);
this.redactedEndpoint = redactEndpointUserInfo(this.endpoint);
this.requestDeadlineMs = options.requestDeadlineMs ?? 15_000;
this.queueCapacity = options.queueCapacity ?? 32;
this.startupTimeoutMs = options.startupTimeoutMs ?? this.requestDeadlineMs;
this.reconnectInitialDelayMs = options.reconnectInitialDelayMs ?? 1_000;
this.reconnectMaxDelayMs = options.reconnectMaxDelayMs ?? 60_000;
this.reconnectMultiplier = options.reconnectMultiplier ?? 2;
this.reconnectJitter = options.reconnectJitter ?? 0.25;
this.connectionFactory = options.connectionFactory ?? createMcpConnection;
this.random = options.random ?? Math.random;
this.now = options.now ?? Date.now;
this.setTimerImpl = options.setTimer ??
((callback, delayMs) => setTimeout(callback, delayMs));
this.clearTimerImpl = options.clearTimer ??
((timer) => clearTimeout(timer));
this.reconnectDelayMs = this.reconnectInitialDelayMs;
}
getState(): GraphitiConnectionState {
return this.state;
}
start(): void {
if (this.state === "closing" || this.state === "stopped") {
throw new GraphitiOfflineError(
this.state,
this.state === "closing"
? "Graphiti connection manager is closing"
: "Graphiti connection manager has been stopped and cannot be restarted",
);
}
if (this.started) return;
this.started = true;
void this.reconnect();
}
async stop(): Promise<void> {
if (this.state === "stopped") return;
if (this.stopPromise) {
await this.stopPromise;
return;
}
const stopPromise = (async () => {
this.started = false;
this.state = "closing";
this.cancelReconnectTimer();
this.rejectAllPending(
new GraphitiOfflineError(
"closing",
"Graphiti connection manager is closing",
),
);
this.abortActiveRequests(
new GraphitiOfflineError(
"closing",
"Graphiti connection manager is closing",
),
);
this.resolveReadyWaiters(false);
const connection = this.connection;
this.connection = null;
if (connection) {
try {
await connection.close();
} catch {
// Ignore close errors while shutting down.
}
}
this.state = "stopped";
})();
this.stopPromise = stopPromise;
try {
await stopPromise;
} finally {
if (this.stopPromise === stopPromise) {
this.stopPromise = null;
}
}
}
async ready(timeoutMs = this.startupTimeoutMs): Promise<boolean> {
if (this.state === "connected") return true;
if (this.state === "closing" || this.state === "stopped") return false;
return await new Promise<boolean>((resolve) => {
let settled = false;
let timer: TimerHandle | null = null;
const finish = (value: boolean) => {
if (settled) return;
settled = true;
if (timer !== null) this.clearTimerImpl(timer);
this.readyWaiters.delete(finish);
resolve(value);
};
this.readyWaiters.add(finish);
if (timeoutMs >= 0) {
timer = this.setTimerImpl(() => finish(false), timeoutMs);
}
});
}
async callTool(
name: string,
args: Record<string, unknown>,
deadlineMs = this.requestDeadlineMs,
): Promise<unknown> {
const sanitizedArgs = Object.fromEntries(
Object.entries(args).filter(([_, value]) =>
value !== null && value !== undefined
),
);
if (this.state === "closing" || this.state === "stopped") {
throw new GraphitiOfflineError(this.state);
}
if (this.state === "offline") {
throw new GraphitiOfflineError("offline");
}
if (this.state === "connecting") {
return await this.enqueueRequest(name, sanitizedArgs, deadlineMs);
}
return await this.executeConnectedCallWithinDeadline(
name,
sanitizedArgs,
this.now() + deadlineMs,
);
}
async reconnect(): Promise<boolean> {
if (this.state === "closing" || this.state === "stopped") return false;
if (this.connectPromise) return await this.connectPromise;
this.cancelReconnectTimer();
this.state = "connecting";
const attempt = this.performReconnect();
this.connectPromise = attempt.finally(() => {
this.connectPromise = null;
});
return await this.connectPromise;
}
private async performReconnect(): Promise<boolean> {
const previousConnection = this.connection;
this.connection = null;
let nextConnection: GraphitiConnection | null = null;
if (previousConnection) {
try {
await previousConnection.close();
} catch {
// Ignore stale close failures during reconnect.
}
}
try {
nextConnection = this.connectionFactory(this.endpoint);
await nextConnection.connect();
if (this.state === "closing" || this.state === "stopped") {
try {
await nextConnection.close();
} catch {
// Ignore close failures while shutting down.
}
return false;
}
this.connection = nextConnection;
this.state = "connected";
this.reconnectDelayMs = this.reconnectInitialDelayMs;
this.resolveReadyWaiters(true);
logger.info("Connected to Graphiti MCP server at", this.redactedEndpoint);
void this.flushPendingQueue();
return true;
} catch (err) {
if (nextConnection) {
try {
await nextConnection.close();
} catch {
// Ignore close failures for failed connects.
}
}
if (this.state !== "closing" && this.state !== "stopped") {
this.state = "offline";
this.scheduleReconnect();
logger.warn("Failed to connect to Graphiti MCP server", err);
}
return false;
}
}
private async executeConnectedCallWithinDeadline(
name: string,
args: Record<string, unknown>,
deadlineAt: number,
attempt = 0,
): Promise<unknown> {
if (this.state !== "connected" || !this.connection) {
throw new GraphitiOfflineError("offline");
}
const deadlineMs = this.getRemainingDeadlineMs(deadlineAt);
if (deadlineMs <= 0) {
throw new GraphitiRequestTimeoutError();
}
try {
const controller = new AbortController();
this.activeRequestControllers.add(controller);
try {
const task = this.connection.callTool(
{ name, arguments: args },
{ signal: controller.signal },
);
return await this.runWithRequestDeadline(
task,
deadlineMs,
controller,
);
} finally {
this.activeRequestControllers.delete(controller);
}
} catch (err) {
if (err instanceof GraphitiOfflineError) {
throw err;
}
if (this.stopPromise && isAbortError(err)) {
throw new GraphitiOfflineError("closing");
}
if (isRequestTimeout(err)) {
throw new GraphitiRequestTimeoutError(
getErrorMessage(err) || undefined,
);
}
if (isSessionExpired(err)) {
return await this.retryConnectedCallAfterRecoverableError(
new GraphitiSessionExpiredError(
getErrorMessage(err) || undefined,
),
name,
args,
deadlineAt,
attempt,
);
}
if (isTransportFailure(err)) {
return await this.retryConnectedCallAfterRecoverableError(
new GraphitiTransportError(
getErrorMessage(err) || undefined,
),
name,
args,
deadlineAt,
attempt,
);
}
throw err;
}
}
private async retryConnectedCallAfterRecoverableError(
typedError: GraphitiSessionExpiredError | GraphitiTransportError,
name: string,
args: Record<string, unknown>,
deadlineAt: number,
attempt: number,
): Promise<unknown> {
if (attempt >= 1) {
void this.reconnect();
throw typedError;
}
const connected = await this.reconnectWithinDeadline(deadlineAt);
if (!connected) {
throw typedError;
}
return await this.executeConnectedCallWithinDeadline(
name,
args,
deadlineAt,
attempt + 1,
);
}
private getRemainingDeadlineMs(deadlineAt: number): number {
return deadlineAt - this.now();
}
private async reconnectWithinDeadline(deadlineAt: number): Promise<boolean> {
const deadlineMs = this.getRemainingDeadlineMs(deadlineAt);
if (deadlineMs <= 0) {
throw new GraphitiRequestTimeoutError();
}
return await this.runWithRequestDeadline(this.reconnect(), deadlineMs);
}
private runWithRequestDeadline<T>(
task: Promise<T>,
deadlineMs: number,
controller?: AbortController,
): Promise<T> {
return new Promise<T>((resolve, reject) => {
let settled = false;
let timer: TimerHandle | null = null;
const clearDeadlineTimer = () => {
if (timer !== null) {
this.clearTimerImpl(timer);
timer = null;
}
};
timer = this.setTimerImpl(() => {
if (settled) return;
settled = true;
clearDeadlineTimer();
controller?.abort(new GraphitiRequestTimeoutError());
reject(new GraphitiRequestTimeoutError());
}, deadlineMs);
task.then(
(value) => {
if (settled) return;
settled = true;
clearDeadlineTimer();
resolve(value);
},
(error) => {
if (settled) return;
settled = true;
clearDeadlineTimer();
reject(error);
},
);
});
}
private abortActiveRequests(reason: unknown): void {
const controllers = [...this.activeRequestControllers];
this.activeRequestControllers.clear();
for (const controller of controllers) {
controller.abort(reason);
}
}
private enqueueRequest(
name: string,
args: Record<string, unknown>,
deadlineMs: number,
): Promise<unknown> {
if (deadlineMs <= 0) {
return Promise.reject(new GraphitiQueueTimeoutError());
}
return new Promise<unknown>((resolve, reject) => {
const deadlineAt = this.now() + deadlineMs;
const pending: PendingRequest = {
name,
args,
deadlineAt,
resolve,
reject,
timer: null,
};
pending.timer = this.setTimerImpl(() => {
this.removePendingRequest(pending);
reject(new GraphitiQueueTimeoutError());
}, deadlineMs);
if (this.pendingRequests.length >= this.queueCapacity) {
const dropped = this.pendingRequests.shift();
if (dropped) {
this.clearPendingTimer(dropped);
dropped.reject(
new GraphitiQueueTimeoutError(
"Graphiti request dropped because the connecting queue is full",
),
);
}
}
this.pendingRequests.push(pending);
});
}
private async flushPendingQueue(): Promise<void> {
if (this.flushingQueue || this.state !== "connected") return;
this.flushingQueue = true;
try {
while (this.state === "connected" && this.pendingRequests.length > 0) {
const next = this.pendingRequests.shift();
if (!next) continue;
this.clearPendingTimer(next);
if (this.getRemainingDeadlineMs(next.deadlineAt) <= 0) {
next.reject(new GraphitiQueueTimeoutError());
continue;
}
try {
const result = await this.executeConnectedCallWithinDeadline(
next.name,
next.args,
next.deadlineAt,
);
next.resolve(result);
} catch (err) {
next.reject(err);
}
}
} finally {
this.flushingQueue = false;
}
}
private removePendingRequest(target: PendingRequest): void {
const index = this.pendingRequests.indexOf(target);
if (index >= 0) {
this.pendingRequests.splice(index, 1);
}
this.clearPendingTimer(target);
}
private clearPendingTimer(request: PendingRequest): void {
if (request.timer !== null) {
this.clearTimerImpl(request.timer);
request.timer = null;
}
}
private rejectAllPending(error: Error): void {
const pending = [...this.pendingRequests];
this.pendingRequests = [];
for (const request of pending) {
this.clearPendingTimer(request);
request.reject(error);
}
}
private scheduleReconnect(): void {
if (
!this.started || this.state === "closing" || this.state === "stopped" ||
this.reconnectTimer !== null
) {
return;
}
const jitterFactor = 1 + ((this.random() * 2) - 1) * this.reconnectJitter;
const delayMs = Math.max(
0,
Math.round(this.reconnectDelayMs * jitterFactor),
);
this.reconnectTimer = this.setTimerImpl(() => {
this.reconnectTimer = null;
if (this.state === "closing" || this.state === "stopped") return;
void this.reconnect();
}, delayMs);
this.reconnectDelayMs = Math.min(
this.reconnectMaxDelayMs,
Math.max(
this.reconnectInitialDelayMs,
Math.round(this.reconnectDelayMs * this.reconnectMultiplier),
),
);
}
private cancelReconnectTimer(): void {
if (this.reconnectTimer !== null) {
this.clearTimerImpl(this.reconnectTimer);
this.reconnectTimer = null;
}
}
private resolveReadyWaiters(value: boolean): void {
const waiters = [...this.readyWaiters];
this.readyWaiters.clear();
for (const waiter of waiters) {
waiter(value);
}
}
}