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
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
t()
View Source
t() :: %ExBreak.Breaker{
break_count: non_neg_integer(),
tripped: boolean(),
tripped_at: DateTime.t() | nil
}
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 occurredtripped
Whether the circuit breaker is trippedtripped_at
The time at which the circuit breaker was tripped (ornil
, if un-tripped)
Link to this section Functions
child_spec(arg) View Source
Returns a specification to start this module under a supervisor.
See Supervisor
.
increment(pid, threshold)
View Source
increment(pid(), pos_integer()) :: :ok
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
is_tripped(pid, timeout_sec)
View Source
is_tripped(pid(), pos_integer()) :: boolean()
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
new()
View Source
new() :: t()
new() :: t()
Create a new circuit breaker.
reset_tripped(pid)
View Source
reset_tripped(pid()) :: :ok
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_link(opts)
View Source
start_link(Keyword.t()) :: Agent.on_start()
start_link(Keyword.t()) :: Agent.on_start()
Start a new breaker.