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
@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
@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.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_state(GenServer.server()) :: :closed | :open | :half_open
Get the current state of the circuit breaker.
@spec reset(GenServer.server()) :: :ok
Reset the circuit breaker to closed state.
@spec start_link(keyword()) :: GenServer.on_start()
Start the circuit breaker.