SuperWorker.CircuitBreaker (SuperWorker v0.5.0)

View Source

A 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

t()

@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(name, fun)

@spec call(atom(), (-> {:ok, any()} | {:error, any()})) ::
  {:ok, any()} | {:error, term()}

Call a function protected by the circuit breaker.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_state(name)

@spec get_state(atom()) :: {:ok, t()} | {:error, :not_found}

Get the current state of the circuit breaker.

reset(name)

@spec reset(atom()) :: :ok | {:error, :not_found}

Reset the circuit breaker to closed state.

start(name, opts \\ [])

@spec start(
  atom(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Start a circuit breaker for a named service.