Skip to content

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-After header 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.

CircuitBreakerConfig

Failure threshold, reset timeout, and storage backend.

Policy

A Policy at priority 30.

// Open after 5 failures, retry after 30s
circuitBreaker();
// Tighter threshold with custom store
circuitBreaker({
failureThreshold: 3,
resetTimeoutMs: 10_000,
failureOn: [500, 502, 503],
store: new InMemoryCircuitBreakerStore(),
});
// With state change notifications
circuitBreaker({
failureThreshold: 5,
onStateChange: (key, from, to) => {
console.log(`Circuit ${key}: ${from} -> ${to}`);
},
});