Documentation
¶
Overview ¶
Package circuitbreaker implements the circuit breaker pattern for external service calls. This prevents cascading failures by temporarily blocking requests to failing services.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrCircuitOpen = errors.New("circuit breaker is open") ErrTooManyRequests = errors.New("too many requests in half-open state") )
Common errors
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern
func (*CircuitBreaker) Execute ¶
Execute runs the given function if the circuit allows
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset forces the circuit breaker back to closed state
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State returns the current state
func (*CircuitBreaker) Stats ¶
func (cb *CircuitBreaker) Stats() Stats
Stats returns the current statistics
type Config ¶
type Config struct {
// Name identifies this circuit breaker
Name string
// MaxFailures is the number of failures before opening the circuit
MaxFailures int
// SuccessThreshold is the number of successes needed to close from half-open
SuccessThreshold int
// Timeout is how long the circuit stays open before testing
Timeout time.Duration
// MaxConcurrentInHalfOpen is the max concurrent requests when half-open
MaxConcurrentInHalfOpen int
// OnStateChange is called when the state changes
OnStateChange func(name string, from, to State)
}
Config configures the circuit breaker behavior
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages multiple circuit breakers
func GetRegistry ¶
func GetRegistry() *Registry
GetRegistry returns the default circuit breaker registry
func (*Registry) Get ¶
func (r *Registry) Get(name string, config ...Config) *CircuitBreaker
Get returns a circuit breaker by name, creating it if necessary
type Stats ¶
type Stats struct {
Name string `json:"name"`
State string `json:"state"`
Failures int `json:"failures"`
Successes int `json:"successes"`
LastFailure time.Time `json:"last_failure,omitzero"`
LastStateChange time.Time `json:"last_state_change"`
Generation uint64 `json:"generation"`
}
Stats returns statistics about the circuit breaker
Source Files
¶
- circuitbreaker.go