View Source BreakerBox (BreakerBox v0.5.0)

Server for circuit breakers.

Maintains state of registered breakers and their configurations, and allows for querying the status of breakers, as well as enabling and disabling.

Modules can be automatically registered if they implement the BreakerBox.BreakerConfiguration behaviour and are passed in to start_link/2.

Link to this section Summary

Functions

Retrieve the current status of all registered breakers.

Returns a specification to start this module under a supervisor.

Retrieve the configuration for a breaker.

Increment the error counter for a circuit breaker.

Initializes the breaker box state, attempting to call registration/0 on every argument passed in, assuming they're a module implementing the BreakerBox.BreakerConfiguration behaviour. If they are not a module, or don't implement the behaviour, a warning will be logged indicating how to fix the issue, and that item will be skipped.

Register a circuit breaker given its name and options.

Retrieve a map with breaker names as keys and BreakerBox.BreakerConfiguration structs as values.

Reset a breaker that has been tripped. This will only reset breakers that have been blown via exceeding the error limit in a given time window, and will not re-enable a breaker that has been disabled via disable/1.

Wrapper around GenServer.start_link/3. Passes the list of modules received to init/1, which will attempt to register the circuit breakers inside those modules.

Retrieve the status of a single breaker.

Link to this section Functions

Link to this function

all_statuses(process_name \\ __MODULE__)

View Source

Specs

all_statuses(process_name :: term()) :: %{optional(term()) => fuse_status()}

Retrieve the current status of all registered breakers.

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

disable(breaker_name, process_name \\ __MODULE__)

View Source

Specs

disable(breaker_name :: term(), process_name :: term()) :: :ok

Disable a circuit breaker.

Sets the breaker's status to :breaker_tripped until enable/1 is called for the same breaker, or the application is restarted.

Will not be reset by calling reset/1.

Link to this function

enable(breaker_name, process_name \\ __MODULE__)

View Source

Specs

enable(breaker_name :: term(), process_name :: term()) :: :ok

Enable a circuit breaker.

Sets the breaker's status to :ok.

Link to this function

get_config(breaker_name, process_name \\ __MODULE__)

View Source

Specs

get_config(breaker_name :: term(), process_name :: term()) ::
  {:ok, BreakerBox.BreakerConfiguration.t()} | {:error, :not_found}

Retrieve the configuration for a breaker.

Link to this function

increment_error(breaker_name, process_name \\ __MODULE__)

View Source

Specs

increment_error(breaker_name :: term(), process_name :: term()) :: :ok

Increment the error counter for a circuit breaker.

If this causes the breaker to go over its error limit for its time window, the breaker will trip, and subsequent calls to status/1 will show it as {:error, {:breaker_tripped, breaker_name}}.

Link to this function

init(circuit_breaker_modules)

View Source

Specs

init(circuit_breaker_modules :: [module()]) ::
  {:ok, %{optional(term()) => BreakerBox.BreakerConfiguration.t()}}

Initializes the breaker box state, attempting to call registration/0 on every argument passed in, assuming they're a module implementing the BreakerBox.BreakerConfiguration behaviour. If they are not a module, or don't implement the behaviour, a warning will be logged indicating how to fix the issue, and that item will be skipped.

Link to this function

register(breaker_name, breaker_options, process_name \\ __MODULE__)

View Source

Specs

register(
  breaker_name :: term(),
  breaker_options :: BreakerBox.BreakerConfiguration.t(),
  process_name :: term()
) :: :ok | :reset | {:error, reason :: term()}

Register a circuit breaker given its name and options.

_MODULE_ is a good default breaker name, but can be a string, atom, or anything you want. Re-using a breaker name in multiple places will overwrite with the last configuration.

Link to this function

registered(process_name \\ __MODULE__)

View Source

Specs

registered(process_name :: term()) :: %{
  optional(term()) => BreakerBox.BreakerConfiguration.t()
}

Retrieve a map with breaker names as keys and BreakerBox.BreakerConfiguration structs as values.

Link to this function

remove(breaker_name, process_name \\ __MODULE__)

View Source

Specs

remove(breaker_name :: term(), process_name :: term()) :: ok_or_not_found()

Remove a circuit breaker.

Link to this function

reset(breaker_name, process_name \\ __MODULE__)

View Source

Specs

reset(breaker_name :: term(), process_name :: term()) :: ok_or_not_found()

Reset a breaker that has been tripped. This will only reset breakers that have been blown via exceeding the error limit in a given time window, and will not re-enable a breaker that has been disabled via disable/1.

Link to this function

start_link(circuit_breaker_modules, process_name \\ __MODULE__)

View Source

Specs

start_link(circuit_breaker_modules :: [module()], process_name :: term()) ::
  {:ok, pid()}
  | {:error, {:already_started, pid()}}
  | {:error, reason :: term()}
  | {:stop, reason :: term()}
  | :ignore

Wrapper around GenServer.start_link/3. Passes the list of modules received to init/1, which will attempt to register the circuit breakers inside those modules.

Modules passed in are expected to implement the BreakerBox.BreakerConfiguration behaviour via @behaviour BreakerBox.BreakerConfiguration, which will require them to have a method named registration/0 that returns a 2-tuple containing the breaker's name and configuration options.

Link to this function

status(breaker_name, process_name \\ __MODULE__)

View Source

Specs

status(breaker_name :: term(), process_name :: term()) :: fuse_status()

Retrieve the status of a single breaker.