Tripwire.Breaker (tripwire v0.1.0)

Copy Markdown

GenServer-based circuit breaker per target.

Each Tripwire.Breaker manages calls to a single target process. It tracks consecutive failures in the :closed state, trips to :open when the threshold is met, and allows probe calls in the :half_open state to test recovery.

State transitions are delegated to Tripwire.State.transition/3.

Registry

Breakers are registered in a Registry via {:via, Registry, ...} tuples. The caller's supervisor must start a Registry with the name returned by registry/0:

{Registry, keys: :unique, name: Tripwire.Breaker.registry()}

TaskSupervisor

Calls are forwarded to targets via Task.Supervisor.async_nolink/2. A TaskSupervisor must be running with the name Tripwire.TaskSupervisor:

{Task.Supervisor, name: Tripwire.TaskSupervisor}

Summary

Types

t()

Internal state of a breaker process.

Functions

Returns a child spec suitable for inclusion in a supervision tree.

Returns the atom name of the Registry used by all breakers.

Start a breaker for the given target.

Types

t()

@type t() :: %Tripwire.Breaker{
  circuit_state: Tripwire.State.state(),
  failure_count: non_neg_integer(),
  probe_from: GenServer.from() | nil,
  probe_in_flight: boolean(),
  probe_monitor_ref: reference() | nil,
  reset_timeout: non_neg_integer(),
  target_key: term(),
  target_pid: pid(),
  task_refs: %{required(reference()) => {reference(), GenServer.from()}},
  threshold: pos_integer(),
  timer_ref: reference() | nil
}

Internal state of a breaker process.

Functions

child_spec(init_arg)

@spec child_spec(keyword()) :: map()

Returns a child spec suitable for inclusion in a supervision tree.

Uses restart: :permanent so that crashes (corrupted state, etc.) are automatically restarted at the :closed state.

registry()

@spec registry() :: atom()

Returns the atom name of the Registry used by all breakers.

The Registry must be started with keys: :unique and this name:

{Registry, keys: :unique, name: Tripwire.Breaker.registry()}

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start a breaker for the given target.

Options

  • :target_key - (required) unique key identifying this target
  • :target_pid - (required) PID of the process to call
  • :threshold - consecutive failures before tripping (default 5)
  • :reset_timeout - milliseconds before :open -> :half_open (default 60_000)

The breaker is registered in Registry via a {:via, Registry, ...} tuple so it can be looked up by target_key.