Foundation.CircuitBreaker (foundation v0.2.1)

Copy Markdown View Source

Circuit breaker state machine for resilient calls.

Summary

Functions

Check if a request should be allowed.

Execute a function through the circuit breaker.

Create a new circuit breaker.

Record a failed call.

Ignore a call result without changing breaker health.

Record a successful call.

Reset the circuit breaker to a closed state.

Get the current circuit breaker state.

Types

state()

@type state() :: :closed | :open | :half_open

t()

@type t() :: %Foundation.CircuitBreaker{
  failure_count: non_neg_integer(),
  failure_threshold: pos_integer(),
  half_open_calls: non_neg_integer(),
  half_open_max_calls: pos_integer(),
  name: String.t(),
  opened_at: integer() | nil,
  reset_timeout_ms: pos_integer(),
  state: state()
}

Functions

allow_request?(cb)

@spec allow_request?(t()) :: boolean()

Check if a request should be allowed.

call(cb, fun, opts \\ [])

@spec call(t(), (-> result), keyword()) :: {result | {:error, :circuit_open}, t()}
when result: term()

Execute a function through the circuit breaker.

new(name, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Create a new circuit breaker.

record_failure(cb)

@spec record_failure(t()) :: t()

Record a failed call.

record_ignored(cb)

@spec record_ignored(t()) :: t()

Ignore a call result without changing breaker health.

This is useful for outcomes such as rate-limits where callers want shared coordination, but do not want the circuit breaker to count the result as a success or a failure.

record_success(cb)

@spec record_success(t()) :: t()

Record a successful call.

reset(cb)

@spec reset(t()) :: t()

Reset the circuit breaker to a closed state.

state(circuit_breaker)

@spec state(t()) :: state()

Get the current circuit breaker state.