Documentation
¶
Index ¶
- Variables
- type CircuitBreaker
- type Config
- type Counts
- type Manager
- func (m *Manager) Execute(host string, fn func() error) error
- func (m *Manager) GetAllStates() map[string]State
- func (m *Manager) GetBreaker(host string) *CircuitBreaker
- func (m *Manager) GetCounts(host string) Counts
- func (m *Manager) GetState(host string) State
- func (m *Manager) Reset(host string)
- func (m *Manager) ResetAll()
- func (m *Manager) Trip(host string)
- type State
Constants ¶
This section is empty.
Variables ¶
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 (*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 ¶
NewManager creates a new circuit breaker manager
func (*Manager) GetAllStates ¶
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