circuitBreaker
circuitBreaker(
config?):Policy
Defined in: src/policies/resilience/circuit-breaker.ts:205
Protect upstream services by breaking the circuit on repeated failures.
Implements the three-state circuit breaker pattern:
- Closed — requests flow normally; failures are counted.
- Open — requests are immediately rejected with 503; a
Retry-Afterheader is set. - Half-open — a limited number of probe requests are allowed through to test recovery.
State transitions: closed → open when failures reach the threshold,
open → half-open after the reset timeout, half-open → closed on
probe success or half-open → open on probe failure.
Parameters
Section titled “Parameters”config?
Section titled “config?”Failure threshold, reset timeout, and storage backend.
Returns
Section titled “Returns”A Policy at priority 30.
Example
Section titled “Example”// Open after 5 failures, retry after 30scircuitBreaker();
// Tighter threshold with custom storecircuitBreaker({ failureThreshold: 3, resetTimeoutMs: 10_000, failureOn: [500, 502, 503], store: new InMemoryCircuitBreakerStore(),});
// With state change notificationscircuitBreaker({ failureThreshold: 5, onStateChange: (key, from, to) => { console.log(`Circuit ${key}: ${from} -> ${to}`); },});