Circuit breaker implemented as a GenServer.
Tracks consecutive failures and transitions through three states:
:closed-- calls pass through. Failures increment the counter.:open-- calls are rejected immediately with{:error, :circuit_open}. Afterreset_timeoutms, transitions to:half_open.:half_open-- allows trial calls. Aftersuccess_thresholdconsecutive successes the breaker resets to:closed; any failure reopens to:open.
Options
:name-- required. Registered name for this breaker instance.:failure_threshold-- consecutive failures before opening. Default5.:reset_timeout-- ms to wait in:openbefore trying:half_open. Default30_000.:half_open_max_calls-- concurrent calls allowed in:half_open. Default1.:success_threshold-- consecutive successes required in:half_openbefore transitioning back to:closed. Default1(immediate close on first success).:error_classifier-- controls which results count as failures. Accepts either a module implementingExResilience.ErrorClassifier(classifications:retriableand:failurecount as failures) or a 1-arity function returningtruefor failures (backward compatible). Default: matches{:error, _}and:error.
Examples
iex> {:ok, _} = ExResilience.CircuitBreaker.start_link(name: :test_cb, failure_threshold: 2, reset_timeout: 100)
iex> ExResilience.CircuitBreaker.call(:test_cb, fn -> :ok end)
{:ok, :ok}
Summary
Functions
Executes fun through the circuit breaker.
Returns a specification to start this module under a supervisor.
Returns detailed information about the circuit breaker state.
Returns the current state of the circuit breaker.
Manually resets the circuit breaker to :closed.
Starts a circuit breaker process.
Types
@type option() :: {:name, atom()} | {:failure_threshold, pos_integer()} | {:reset_timeout, pos_integer()} | {:half_open_max_calls, pos_integer()} | {:success_threshold, pos_integer()} | {:error_classifier, module() | (term() -> boolean())}
@type state_name() :: :closed | :open | :half_open
Functions
Executes fun through the circuit breaker.
Returns {:ok, result} on success, {:error, :circuit_open} if the
breaker is open, or {:error, reason} if the function returns an error.
Raises if the function raises (the raise counts as a failure).
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_info(atom()) :: %{ state: state_name(), failure_count: non_neg_integer(), success_count: non_neg_integer() }
Returns detailed information about the circuit breaker state.
@spec get_state(atom()) :: state_name()
Returns the current state of the circuit breaker.
@spec reset(atom()) :: :ok
Manually resets the circuit breaker to :closed.
@spec start_link([option()]) :: GenServer.on_start()
Starts a circuit breaker process.
See module docs for available options.