Documentation
¶
Overview ¶
Package circuitbreaker provides a transport-agnostic circuit breaker state machine. It can be used with any call pattern: HTTP, MCP, QUIC, or internal function calls.
The breaker transitions through three states:
- Closed: normal operation, calls pass through.
- Open: calls are rejected immediately with ErrOpen.
- HalfOpen: a limited number of probe calls are allowed to test recovery.
State is stored in-memory by default, with an optional SQLite backend for persistence across restarts.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker implements the circuit breaker pattern. Thread-safe: all state transitions use a mutex.
func New ¶
New creates a Breaker with sensible defaults: 5 failures to open, 30s reset timeout, 2 successes to close from half-open.
func (*Breaker) Allow ¶
Allow checks whether a call is allowed. Returns false if the breaker is open and the reset timeout has not elapsed.
func (*Breaker) Execute ¶
Execute runs fn if the breaker allows it, recording success or failure. Returns ErrOpen if the breaker is open.
func (*Breaker) RecordFailure ¶
func (b *Breaker) RecordFailure()
RecordFailure records a failed call.
func (*Breaker) RecordSuccess ¶
func (b *Breaker) RecordSuccess()
RecordSuccess records a successful call.
type ErrOpen ¶
type ErrOpen struct {
Name string
}
ErrOpen is returned when the circuit breaker is open and calls are rejected.
type Option ¶
type Option func(*Breaker)
Option configures a Breaker.
func Defaults ¶
func Defaults() Option
Defaults returns a set of options with the standard defaults.
func WithHalfOpenMax ¶
WithHalfOpenMax sets how many consecutive successes in half-open are needed to close the breaker (default 2).
func WithResetTimeout ¶
WithResetTimeout sets how long the breaker stays open before transitioning to half-open (default 30s).
func WithSQLite ¶
WithSQLite enables SQLite-backed persistence for the breaker state. The state is loaded on creation and persisted on every transition.
func WithThreshold ¶
WithThreshold sets the failure count that trips the breaker open (default 5).