Per-key circuit breaker implementing the CLOSED → OPEN → HALF_OPEN state machine with sliding-window or consecutive-count failure detection.
Thread-safe for async use. Rejects with CircuitOpenError when OPEN.
const cb = new CircuitBreaker("api.example.com", { failureThreshold: 3 });const result = await cb.execute(() => fetch("https://api.example.com/data")); Copy
const cb = new CircuitBreaker("api.example.com", { failureThreshold: 3 });const result = await cb.execute(() => fetch("https://api.example.com/data"));
The breaker key (typically origin/host like "https://api.example.com")
Circuit breaker configuration
The current circuit state.
Snapshot of all counters — suitable for logging or dashboards.
Execute a function through the circuit breaker. Throws CircuitOpenError if the circuit is open. Records success/failure and transitions state accordingly.
CircuitOpenError
Manually trip the circuit open — useful for maintenance windows.
Manually reset the circuit to CLOSED — useful after a fix deployment.
Per-key circuit breaker implementing the CLOSED → OPEN → HALF_OPEN state machine with sliding-window or consecutive-count failure detection.
Thread-safe for async use. Rejects with CircuitOpenError when OPEN.
Example