circuitbreaker

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCircuitOpen is returned when the circuit breaker is open
	ErrCircuitOpen = errors.New("circuit breaker is open")
	// ErrTooManyRequests is returned when too many requests are made in half-open state
	ErrTooManyRequests = errors.New("too many requests in half-open state")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker implements the circuit breaker pattern

func New

func New(name string, config *Config) *CircuitBreaker

New creates a new circuit breaker

func (*CircuitBreaker) Counts

func (cb *CircuitBreaker) Counts() Counts

Counts returns a copy of the current counts

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(fn func() error) error

Execute runs the given function if the circuit breaker allows it

func (*CircuitBreaker) Name

func (cb *CircuitBreaker) Name() string

Name returns the circuit breaker name

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset manually resets the circuit breaker to closed state

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the current state

func (*CircuitBreaker) Trip

func (cb *CircuitBreaker) Trip()

Trip manually trips the circuit breaker to open state

type Config

type Config struct {
	// MaxRequests is the maximum number of requests allowed in half-open state
	MaxRequests uint32
	// Interval is the cyclic period in closed state to clear internal counts
	Interval time.Duration
	// Timeout is the period of open state before transitioning to half-open
	Timeout time.Duration
	// ReadyToTrip is called with counts to determine if the breaker should trip
	ReadyToTrip func(counts Counts) bool
	// OnStateChange is called whenever the state changes
	OnStateChange func(name string, from State, to State)
}

Config defines circuit breaker configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default circuit breaker configuration

type Counts

type Counts struct {
	Requests             uint32
	TotalSuccesses       uint32
	TotalFailures        uint32
	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

Counts holds the statistics for the circuit breaker

type Manager

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

Manager manages multiple circuit breakers for different hosts

func NewManager

func NewManager(config *Config) *Manager

NewManager creates a new circuit breaker manager

func (*Manager) Execute

func (m *Manager) Execute(host string, fn func() error) error

Execute runs the function through the circuit breaker for the given host

func (*Manager) GetAllStates

func (m *Manager) GetAllStates() map[string]State

GetAllStates returns the states of all circuit breakers

func (*Manager) GetBreaker

func (m *Manager) GetBreaker(host string) *CircuitBreaker

GetBreaker returns the circuit breaker for the given host Creates a new one if it doesn't exist

func (*Manager) GetCounts

func (m *Manager) GetCounts(host string) Counts

GetCounts returns the counts for the circuit breaker for the given host

func (*Manager) GetState

func (m *Manager) GetState(host string) State

GetState returns the state of the circuit breaker for the given host

func (*Manager) Reset

func (m *Manager) Reset(host string)

Reset resets the circuit breaker for the given host

func (*Manager) ResetAll

func (m *Manager) ResetAll()

ResetAll resets all circuit breakers

func (*Manager) Trip

func (m *Manager) Trip(host string)

Trip trips the circuit breaker for the given host

type State

type State int32

State represents the circuit breaker state

const (
	// StateClosed means requests are allowed
	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

Jump to

Keyboard shortcuts

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