LlmCore.Memory.Hindsight.CircuitBreaker (llm_core v0.3.0)

Copy Markdown View Source

Circuit breaker pattern for Hindsight MCP connections.

States:

  • Closed: Normal operation, track failures
  • Open: Reject requests, return cached/error
  • Half-open: Allow 1 probe request

State Transitions

  • Closed → Open: After threshold consecutive failures
  • Open → Half-open: After reset time elapsed
  • Half-open → Closed: On successful probe
  • Half-open → Open: On failed probe

Summary

Functions

Checks if the circuit allows a request.

Returns a specification to start this module under a supervisor.

Reports a failed request.

Reports a successful request.

Manually resets the circuit to closed state.

Starts the circuit breaker GenServer.

Returns the current circuit status.

Types

state()

@type state() :: %{
  status: state_name(),
  failure_count: non_neg_integer(),
  last_failure_at: integer() | nil,
  last_success_at: integer() | nil
}

state_name()

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

Functions

allow?()

@spec allow?() :: :ok | {:error, :circuit_open}

Checks if the circuit allows a request.

Returns:

  • :ok - proceed with request
  • {:error, :circuit_open} - circuit is open, use fallback

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

report_failure(reason)

@spec report_failure(term()) :: :ok

Reports a failed request.

report_success()

@spec report_success() :: :ok

Reports a successful request.

reset()

@spec reset() :: :ok

Manually resets the circuit to closed state.

start_link(opts \\ [])

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

Starts the circuit breaker GenServer.

status()

@spec status() :: %{status: state_name(), failure_count: non_neg_integer()}

Returns the current circuit status.