Heuristic:23blocks OS Ai maestro Exponential Backoff Reconnect
| Knowledge Sources | |
|---|---|
| Domains | Network, Client |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
On WebSocket disconnect, reconnect after a delay that grows exponentially with each failure (capped), so a down server isn't hammered and a brief flap doesn't lock you out.
Description
The `useWebSocket` and `useCompanionWebSocket` hooks (`hooks/useWebSocket.ts`, `hooks/useCompanionWebSocket.ts`) reconnect on close with an exponential backoff: the retry delay doubles after each consecutive failure, up to a cap (e.g. 30s). This avoids hammering a server that's restarting or a network that's flapping, and lets the connection recover quickly once the server is back. The backoff resets to the base on a successful connect. This is the client-side complement to the server's circuit breaker (Principle:23blocks_OS_Ai_maestro_Circuit_Breaker_Resilience).
Usage
Use this heuristic for any client-managed persistent connection (WebSocket, SSE, long-poll) that must self-heal after server or network failure.
The Insight (Rule of Thumb)
- Action: `delay = min(base * 2**attempts, cap)`; reset `attempts` on success.
- Value: base ~1s, cap ~30s.
- Trade-off: A fast transient failure still waits one base interval; acceptable to avoid a storm.
Reasoning
A fixed retry delay either floods a recovering server (too short) or recovers too slowly after a brief outage (too long). Exponential backoff with a cap adapts: quick first retry catches transient drops, growing delay protects a struggling server, and the cap bounds the worst-case recovery time.