circuit

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package circuit provides circuit breaker pattern implementation for high availability.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircuitOpen     = errors.New("circuit breaker is open")
	ErrTooManyRequests = errors.New("too many requests in half-open state")
)

Errors

Functions

This section is empty.

Types

type Breaker

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

Breaker implements the circuit breaker pattern.

func NewBreaker

func NewBreaker(config Config) *Breaker

NewBreaker creates a new circuit breaker with the given configuration.

func (*Breaker) Allow

func (b *Breaker) Allow() error

Allow checks if a request should be allowed through. Returns an error if the circuit is open.

func (*Breaker) Execute

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

Execute runs the given function with circuit breaker protection.

func (*Breaker) ForceOpen

func (b *Breaker) ForceOpen()

ForceOpen forces the circuit breaker to open state.

func (*Breaker) GetStats

func (b *Breaker) GetStats() Stats

GetStats returns current statistics.

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 resets the circuit breaker to closed state.

func (*Breaker) State

func (b *Breaker) State() State

State returns the current state of the circuit breaker.

type Config

type Config struct {
	// FailureThreshold is the number of failures before opening the circuit.
	// Default: 5
	FailureThreshold int

	// SuccessThreshold is the number of successes before closing the circuit.
	// Default: 3
	SuccessThreshold int

	// Timeout is how long to wait before attempting recovery.
	// Default: 30 seconds
	Timeout time.Duration

	// MaxRequests is the maximum requests allowed in half-open state.
	// Default: 1
	MaxRequests int

	// OnStateChange is called when the state changes.
	OnStateChange func(from, to State)
}

Config holds circuit breaker configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Manager

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

Manager manages multiple circuit breakers.

func NewManager

func NewManager(config Config) *Manager

NewManager creates a new circuit breaker manager.

func (*Manager) Get

func (m *Manager) Get(name string) *Breaker

Get returns the circuit breaker for the given name, creating it if necessary.

func (*Manager) Range

func (m *Manager) Range(fn func(name string, b *Breaker) bool)

Range iterates over all circuit breakers.

func (*Manager) Remove

func (m *Manager) Remove(name string)

Remove removes the circuit breaker for the given name.

type State

type State int32

State represents the circuit breaker state.

const (
	// StateClosed means requests are allowed through.
	StateClosed State = iota
	// StateOpen means requests are blocked.
	StateOpen
	// StateHalfOpen means limited requests are allowed to test recovery.
	StateHalfOpen
)

func (State) String

func (s State) String() string

String returns the string representation of the state.

type Stats

type Stats struct {
	State     State
	Failures  int64
	Successes int64
	Requests  int64
}

Stats returns current circuit breaker statistics.

Jump to

Keyboard shortcuts

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