28 lines
916 B
TypeScript
28 lines
916 B
TypeScript
import type { RedisReconnectStrategy } from "../options/options.js";
|
|
|
|
/**
|
|
* Compute the next backoff delay in milliseconds for the configured
|
|
* exponential reconnect strategy. Capped at `maxDelayMs`. Pure function,
|
|
* trivially deterministic — the broker drives an external timer.
|
|
*/
|
|
export function nextReconnectDelayMs(
|
|
attempt: number,
|
|
strategy: RedisReconnectStrategy
|
|
): number {
|
|
if (attempt < 0) return strategy.initialDelayMs;
|
|
const raw = strategy.initialDelayMs * strategy.multiplier ** attempt;
|
|
return Math.min(raw, strategy.maxDelayMs);
|
|
}
|
|
|
|
/**
|
|
* Decide whether the reconnect loop should give up. Once `attempt` reaches
|
|
* the configured maximum, the broker raises `WS_BROKER_REDIS.RECONNECT_FAILED`
|
|
* and surfaces `down` health.
|
|
*/
|
|
export function shouldGiveUp(
|
|
attempt: number,
|
|
strategy: RedisReconnectStrategy
|
|
): boolean {
|
|
return attempt >= strategy.maxAttempts;
|
|
}
|