CircuitBreaker

interface CircuitBreaker

A circuit breaker that protects a downstream service from overload and prevents repeated calls to a failing service.

It operates in three states:

  • CLOSED: All calls are allowed to pass through. Failed calls advance failure tracking until CircuitBreakerConfig.failureThreshold is reached (consecutive failures by default, or failures within CircuitBreakerConfig.slidingWindow when set), then the state changes to OPEN.

  • OPEN: All calls are immediately rejected with a CircuitBreakerOpenException without attempting to execute them. After a configured timeout, the state changes to HALF_OPEN.

  • HALF_OPEN: A limited number of calls (halfOpenMaxCalls) are allowed to pass through to test if the downstream service has recovered. If a call succeeds, the success count is incremented. If the success count reaches the successThreshold, the state changes back to CLOSED. If a call fails, the state immediately reverts to OPEN.

The state of the circuit can be observed via the state property.

See also

for configuration options.

for the exception thrown when the circuit is open.

Inheritors

Properties

Link copied to clipboard
abstract val state: StateFlow<CircuitState>

The current state of the circuit: CircuitState.CLOSED, CircuitState.OPEN, or CircuitState.HALF_OPEN.

Functions

Link copied to clipboard
abstract suspend fun <T> execute(block: suspend () -> T): T

Executes block if the circuit allows it; otherwise throws CircuitBreakerOpenException.

Link copied to clipboard

Returns a point-in-time snapshot of the circuit breaker state and counters. Use for health endpoints, dashboards, or metrics without subscribing to state or events.