circuitbreaker

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

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

func New(name string, opts ...Option) *Breaker

New creates a Breaker with sensible defaults: 5 failures to open, 30s reset timeout, 2 successes to close from half-open.

func (*Breaker) Allow

func (b *Breaker) Allow() bool

Allow checks whether a call is allowed. Returns false if the breaker is open and the reset timeout has not elapsed.

func (*Breaker) Execute

func (b *Breaker) Execute(ctx context.Context, fn func() error) error

Execute runs fn if the breaker allows it, recording success or failure. Returns ErrOpen if the breaker is open.

func (*Breaker) Name

func (b *Breaker) Name() string

Name returns the breaker name.

func (*Breaker) RecordFailure

func (b *Breaker) RecordFailure()

RecordFailure records a failed call.

func (*Breaker) RecordSuccess

func (b *Breaker) RecordSuccess()

RecordSuccess records a successful call.

func (*Breaker) Reset

func (b *Breaker) Reset()

Reset forces the breaker back to closed state.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current breaker state, applying time-based transitions.

type ErrOpen

type ErrOpen struct {
	Name string
}

ErrOpen is returned when the circuit breaker is open and calls are rejected.

func (*ErrOpen) Error

func (e *ErrOpen) Error() string

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 WithClock

func WithClock(fn func() time.Time) Option

WithClock sets a custom clock function (for testing).

func WithHalfOpenMax

func WithHalfOpenMax(n int) Option

WithHalfOpenMax sets how many consecutive successes in half-open are needed to close the breaker (default 2).

func WithResetTimeout

func WithResetTimeout(d time.Duration) Option

WithResetTimeout sets how long the breaker stays open before transitioning to half-open (default 30s).

func WithSQLite

func WithSQLite(db *sql.DB) Option

WithSQLite enables SQLite-backed persistence for the breaker state. The state is loaded on creation and persisted on every transition.

func WithThreshold

func WithThreshold(n int) Option

WithThreshold sets the failure count that trips the breaker open (default 5).

type State

type State int

State represents the circuit breaker state.

const (
	Closed   State = iota // Normal operation, calls pass through.
	Open                  // Calls rejected immediately.
	HalfOpen              // Probe calls allowed to test recovery.
)

func (State) String

func (s State) String() string

String returns the state name.

type Store

type Store interface {
	Init() error
	Load(name string) (state State, failures int, lastFailure time.Time, err error)
	Save(name string, state State, failures int, lastFailure time.Time) error
}

Store is the interface for circuit breaker state persistence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL