ADK.LLM.CircuitBreaker (ADK v0.0.1-alpha.1)

Copy Markdown View Source

Simple circuit breaker for LLM calls using GenServer.

States

  • :closed — Normal operation, calls pass through
  • :open — Too many failures, calls rejected with {:error, :circuit_open}
  • :half_open — Testing recovery, allows one call through

Options

  • :name - GenServer name (default: __MODULE__)
  • :failure_threshold - Failures before opening (default: 5)
  • :reset_timeout_ms - Time in open state before half-open (default: 60_000)

Examples

{:ok, _} = ADK.LLM.CircuitBreaker.start_link(name: :llm_breaker)
ADK.LLM.CircuitBreaker.call(:llm_breaker, fn -> some_llm_call() end)

Summary

Functions

Execute fun through the circuit breaker.

Returns a specification to start this module under a supervisor.

Get the current state of the circuit breaker.

Reset the circuit breaker to closed state.

Start the circuit breaker.

Types

t()

@type t() :: %ADK.LLM.CircuitBreaker{
  failure_count: non_neg_integer(),
  failure_threshold: pos_integer(),
  opened_at: integer() | nil,
  reset_timeout_ms: pos_integer(),
  state: :closed | :open | :half_open
}

Functions

call(server \\ __MODULE__, fun)

@spec call(GenServer.server(), (-> {:ok, term()} | {:error, term()})) ::
  {:ok, term()} | {:error, term()}

Execute fun through the circuit breaker.

Returns {:error, :circuit_open} if the circuit is open.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_state(server \\ __MODULE__)

@spec get_state(GenServer.server()) :: :closed | :open | :half_open

Get the current state of the circuit breaker.

reset(server \\ __MODULE__)

@spec reset(GenServer.server()) :: :ok

Reset the circuit breaker to closed state.

start_link(opts \\ [])

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

Start the circuit breaker.