From 3328ddc2377f46a7b42c004e1f2bb8ba8edbec9c Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 15 Jan 2025 18:40:11 -0500 Subject: [PATCH 1/2] only-reset-after-5s --- src/abacus-ts/websocket/lib/reconnectingWebsocket.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts b/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts index 784e05b99..d69dfdb2c 100644 --- a/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts +++ b/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts @@ -6,6 +6,7 @@ interface ReconnectingWebSocketConfig { handleMessage: (data: any) => void; handleFreshConnect: () => void; initialReconnectInterval?: number; + connectionTimeToConsiderLive?: number; maxReconnectInterval?: number; backoffMultiplier?: number; } @@ -23,6 +24,8 @@ export class ReconnectingWebSocket { private readonly backoffMultiplier: number; + private readonly connectionTimeToConsiderLive: number; + private ws: WebSocketConnection | null = null; private currentId: number = 0; @@ -41,6 +44,7 @@ export class ReconnectingWebSocket { this.initialReconnectInterval = config.initialReconnectInterval ?? 1000; this.maxReconnectInterval = config.maxReconnectInterval ?? 120_000; this.backoffMultiplier = config.backoffMultiplier ?? 1.5; + this.connectionTimeToConsiderLive = config.connectionTimeToConsiderLive ?? 5000; this.connect(); } @@ -81,7 +85,13 @@ export class ReconnectingWebSocket { private handleWsConnected = (id: number) => { // can happen if we rapidly switch websockets maybe ?? if (id !== this.currentId || this.isDead) return; - this.numberOfFailedAttempts = 0; + + // after x seconds, reset our failure counter + setTimeout(() => { + if (id !== this.currentId || this.isDead || !this.isActive()) return; + this.numberOfFailedAttempts = 0; + }, this.connectionTimeToConsiderLive); + this.handleFreshConnect(); }; From 008f62869ef50ced2383a7700f0e13fd8df588ed Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 15 Jan 2025 18:41:56 -0500 Subject: [PATCH 2/2] fix --- src/abacus-ts/websocket/lib/reconnectingWebsocket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts b/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts index d69dfdb2c..ee4a009b5 100644 --- a/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts +++ b/src/abacus-ts/websocket/lib/reconnectingWebsocket.ts @@ -86,7 +86,7 @@ export class ReconnectingWebSocket { // can happen if we rapidly switch websockets maybe ?? if (id !== this.currentId || this.isDead) return; - // after x seconds, reset our failure counter + // after x seconds, reset our failure counter if we're still alive setTimeout(() => { if (id !== this.currentId || this.isDead || !this.isActive()) return; this.numberOfFailedAttempts = 0;