Initial Commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user