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
@type state() :: %{ status: state_name(), failure_count: non_neg_integer(), last_failure_at: integer() | nil, last_success_at: integer() | nil }
@type state_name() :: :closed | :open | :half_open
Functions
@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
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec report_failure(term()) :: :ok
Reports a failed request.
@spec report_success() :: :ok
Reports a successful request.
@spec reset() :: :ok
Manually resets the circuit to closed state.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the circuit breaker GenServer.
@spec status() :: %{status: state_name(), failure_count: non_neg_integer()}
Returns the current circuit status.