Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): keep the reconnect backoff at its cap on long streaks
Yudhi Armyndharisa9cf76a1a314
diff --git a/src/modules/session/reconnect-policy.spec.ts b/src/modules/session/reconnect-policy.spec.tsindex b96cb30e0..3c0333e23 100644--- a/src/modules/session/reconnect-policy.spec.ts+++ b/src/modules/session/reconnect-policy.spec.ts@@ -101,7 +101,20 @@ describe('decideReconnect', () => {it('never exhausts on the default unlimited budget', () => {const s = state({ attempts: 10_000 });- expect(decideReconnect(s, NO_JITTER).kind).toBe('schedule');+ const d = decideReconnect(s, NO_JITTER);++ expect(d.kind).toBe('schedule');+ expect((d as { delayMs: number }).delayMs).toBe(RECONNECT_DELAY_CAP_MS);+ });++ it('stays parked at the cap once baseDelay * 2^attempts would overflow to Infinity', () => {+ // 5000 * 2^1012 is Infinity; it must keep the cap rather than drop back to the base delay.+ for (const attempts of [1011, 1012, 1013, 10_000]) {+ expect(decideReconnect(state({ attempts }), NO_JITTER)).toMatchObject({+ kind: 'schedule',+ delayMs: RECONNECT_DELAY_CAP_MS,+ });+ }});});@@ -195,10 +208,10 @@ describe('clampReconnectDelay', () => {expect(clampReconnectDelay(Number.MAX_SAFE_INTEGER, 5000)).toBe(RECONNECT_DELAY_CAP_MS);});- it('falls back to baseDelay when the computed delay is not finite', () => {- // An operator-supplied non-numeric config would otherwise yield NaN → setTimeout fires at 0.- // Infinity is likewise not finite, so it takes the same fallback rather than the cap.+ it('falls back to baseDelay for NaN and caps +Infinity', () => {+ // An operator-supplied non-numeric config would otherwise yield NaN and setTimeout fires at 0.+ // Infinity is an oversized delay, not a missing one, so it takes the cap like any other.expect(clampReconnectDelay(NaN, 5000)).toBe(5000);- expect(clampReconnectDelay(Number.POSITIVE_INFINITY, 5000)).toBe(5000);+ expect(clampReconnectDelay(Number.POSITIVE_INFINITY, 5000)).toBe(RECONNECT_DELAY_CAP_MS);});});diff --git a/src/modules/session/reconnect-policy.ts b/src/modules/session/reconnect-policy.tsindex 5467c33ec..2c8b7c290 100644--- a/src/modules/session/reconnect-policy.ts+++ b/src/modules/session/reconnect-policy.ts@@ -56,10 +56,11 @@ export function clampNumber(value: number, min: number, max: number): number {/*** Clamp a computed backoff delay finite and within setTimeout's safe range (a huge value would- * overflow its 32-bit ms field and fire immediately).+ * overflow its 32-bit ms field and fire immediately). NaN falls back to baseDelay; +Infinity is+ * simply huge, so it takes the cap like any other oversized delay.*/export function clampReconnectDelay(rawDelay: number, baseDelay: number): number {- return clampNumber(Number.isFinite(rawDelay) ? rawDelay : baseDelay, 0, RECONNECT_DELAY_CAP_MS);+ return clampNumber(Number.isNaN(rawDelay) ? baseDelay : rawDelay, 0, RECONNECT_DELAY_CAP_MS);}/**@@ -91,8 +92,12 @@ export function decideReconnect(// Exponential backoff: baseDelay * 2^attempts (with jitter), clamped finite + within// setTimeout's safe range so the timer can't overflow and fire immediately. With the default- // unlimited budget the delay parks at RECONNECT_DELAY_CAP_MS once the exponent outgrows it.- const delayMs = clampReconnectDelay(state.baseDelay * Math.pow(2, state.attempts) + jitter, state.baseDelay);+ // unlimited budget the delay parks at RECONNECT_DELAY_CAP_MS once the exponent outgrows it; the+ // exponent is bounded so a very long streak never overflows the product to Infinity.+ const delayMs = clampReconnectDelay(+ state.baseDelay * Math.pow(2, Math.min(state.attempts, 30)) + jitter,+ state.baseDelay,+ );state.attempts++;return {