circuitbreaker

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package circuitbreaker implements the circuit breaker pattern to prevent cascading failures when external services (LLM, gRPC, SQLite) degrade.

States: Closed (normal) → Open (failing) → HalfOpen (testing) → Closed

Index

Constants

This section is empty.

Variables

View Source
var ErrOpen = errors.New("circuit breaker is open")

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

Functions

This section is empty.

Types

type Breaker

type Breaker struct {
	// contains filtered or unexported fields
}

Breaker implements the circuit breaker pattern with thread-safe state transitions.

func New

func New(cfg Config) *Breaker

New creates a circuit breaker with the given configuration.

func (*Breaker) Allow

func (b *Breaker) Allow() error

Allow checks if a request should be allowed through. Returns ErrOpen if the circuit is open and recovery timeout hasn't elapsed.

func (*Breaker) RecordFailure

func (b *Breaker) RecordFailure()

RecordFailure records a failed operation.

func (*Breaker) RecordSuccess

func (b *Breaker) RecordSuccess()

RecordSuccess records a successful operation.

func (*Breaker) Reset

func (b *Breaker) Reset()

Reset manually resets the circuit breaker to closed state.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current circuit breaker state.

type Config

type Config struct {
	// FailureThreshold is the number of consecutive failures before opening the circuit.
	FailureThreshold int
	// RecoveryTimeout is how long the circuit stays open before transitioning to HalfOpen.
	RecoveryTimeout time.Duration
	// SuccessThreshold is the number of consecutive successes in HalfOpen before closing.
	SuccessThreshold int
}

Config holds circuit breaker parameters.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a production-ready configuration.

type State

type State int

State represents the current state of a circuit breaker.

const (
	StateClosed   State = iota // Normal operation, requests pass through
	StateOpen                  // Service failing, requests rejected immediately
	StateHalfOpen              // Testing if service has recovered
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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