SuperWorker.CircuitBreaker (SuperWorker v0.5.0)
View SourceA simple Circuit Breaker implementation for protecting external API calls.
The circuit breaker has three states:
:closed- Normal operation, requests go through:open- Requests are short-circuited (fail fast):half_open- Testing if the service has recovered
Usage
# Protect an API call
result =
SuperWorker.CircuitBreaker.call(:my_external_service, fn ->
HTTPoison.get("https://api.example.com/data")
end)
case result do
{:ok, response} -> # Handle success
{:error, :circuit_open} -> # Handle circuit open
{:error, reason} -> # Handle other errors
end
Summary
Functions
Call a function protected by the circuit breaker.
Returns a specification to start this module under a supervisor.
Get the current state of the circuit breaker.
Reset the circuit breaker to closed state.
Start a circuit breaker for a named service.
Types
@type t() :: %SuperWorker.CircuitBreaker{ failure_count: non_neg_integer(), failure_threshold: pos_integer(), half_open_max_calls: pos_integer(), last_failure_time: integer() | nil, name: atom(), reset_timeout: pos_integer(), state: :closed | :open | :half_open, success_count: non_neg_integer() }
Functions
Call a function protected by the circuit breaker.
Returns a specification to start this module under a supervisor.
See Supervisor.
Get the current state of the circuit breaker.
@spec reset(atom()) :: :ok | {:error, :not_found}
Reset the circuit breaker to closed state.
Start a circuit breaker for a named service.