ExBreak v0.0.4 ExBreak.Breaker View Source

A server that serves as a circuit breaker for a single function

Link to this section Summary

Types

t()

A struct representing the state of a circuit breaker

Functions

Returns a specification to start this module under a supervisor.

Increment a circuit breaker.

Determine whether the given circuit breaker is tripped.

Create a new circuit breaker.

Reset a tripped circuit breaker by creating a new circuit breaker.

Start a new breaker.

Link to this section Types

Link to this type

t() View Source
t() :: %ExBreak.Breaker{
  break_count: non_neg_integer(),
  tripped: boolean(),
  tripped_at: DateTime.t() | nil
}

A struct representing the state of a circuit breaker

  • break_count The number of breaks that have occurred
  • tripped Whether the circuit breaker is tripped
  • tripped_at The time at which the circuit breaker was tripped (or nil, if un-tripped)

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

increment(pid, threshold) View Source
increment(pid(), pos_integer()) :: :ok

Increment a circuit breaker.

If the new break count exceeds the given threshold, the breaker is also marked as tripped.

iex> {:ok, pid} = ExBreak.Breaker.start_link([])
iex> ExBreak.Breaker.increment(pid, 10)
iex> ExBreak.Breaker.is_tripped(pid, 60)
false
Link to this function

is_tripped(pid, timeout_sec) View Source
is_tripped(pid(), pos_integer()) :: boolean()

Determine whether the given circuit breaker is tripped.

The second argument, timeout_sec, is the time that must pass for a tripped circuit breaker to re-open.

iex> {:ok, pid} = ExBreak.Breaker.start_link([])
iex> ExBreak.Breaker.increment(pid, 10)
iex> ExBreak.Breaker.is_tripped(pid, 10)
false

iex> {:ok, pid} = ExBreak.Breaker.start_link([])
iex> ExBreak.Breaker.increment(pid, 1)
iex> ExBreak.Breaker.is_tripped(pid, 10)
true

iex> {:ok, pid} = ExBreak.Breaker.start_link([])
iex> ExBreak.Breaker.increment(pid, 1)
iex> ExBreak.Breaker.is_tripped(pid, 0)
false

Create a new circuit breaker.

Link to this function

reset_tripped(pid) View Source
reset_tripped(pid()) :: :ok

Reset a tripped circuit breaker by creating a new circuit breaker.

If the circuit breaker is not tripped, it is simply returned.

iex> {:ok, pid} = ExBreak.Breaker.start_link([])
iex> ExBreak.Breaker.increment(pid, 1)
iex> ExBreak.Breaker.is_tripped(pid, 10)
true
iex> ExBreak.Breaker.reset_tripped(pid)
iex> ExBreak.Breaker.is_tripped(pid, 10)
false

Start a new breaker.