Import
What it does
CircuitBreaker implements the circuit breaker pattern to prevent cascading failures. When failures exceed a threshold, the breaker opens and immediately rejects calls for a cooldown period instead of overloading an already-failing service.
ApiClient creates an internal CircuitBreaker from the circuitBreaker config option. Use this class directly when you need circuit breaking outside of HTTP requests.
Constructor
CircuitBreakerConfig
Methods
execute<T>(fn)
Runs fn if the circuit is closed or half-open. Throws immediately if the circuit is open and the timeout has not expired.
getState()
Returns the current state of the circuit.
reset()
Manually resets the circuit to the closed state, clearing all counters.
State machine
The circuit breaker cycles through three states:
Examples
Basic usage
Custom error message
Combined with RetryPolicy
When using ApiClient, both retry and circuit breaker are configured together in the constructor — you don’t need to compose them manually.
The circuit breaker does not persist state across process restarts. In serverless or multi-instance environments, each instance maintains its own circuit state.