Documentation
¶
Index ¶
Constants ¶
const ( // DefaultThreshold is the default number of consecutive failures before // the circuit breaker opens. DefaultThreshold = 5 // DefaultTimeout is the default duration the circuit breaker stays open // before attempting to close again. DefaultTimeout = 1 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
func SetTimeNow ¶
SetTimeNow sets the time function for the package and returns a function to restore it. This is intended for testing purposes only.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements a simple circuit breaker pattern for service health. It tracks consecutive failures and opens the circuit after a threshold is reached.
func New ¶
func New(threshold int, timeout time.Duration) *CircuitBreaker
New creates a new circuit breaker.
func (*CircuitBreaker) AllowRequest ¶
func (cb *CircuitBreaker) AllowRequest() bool
AllowRequest checks if the circuit breaker allows a request to go through. It handles the state transition from Open to Half-Open.
func (*CircuitBreaker) ForceOpen ¶
func (cb *CircuitBreaker) ForceOpen()
ForceOpen forces the circuit breaker into an open state. This is useful for testing or degraded mode initialization.
func (*CircuitBreaker) IsOpen ¶
func (cb *CircuitBreaker) IsOpen() bool
IsOpen returns true if the circuit breaker is currently open.
func (*CircuitBreaker) RecordFailure ¶
func (cb *CircuitBreaker) RecordFailure()
RecordFailure increments the failure count.
func (*CircuitBreaker) RecordSuccess ¶
func (cb *CircuitBreaker) RecordSuccess()
RecordSuccess records a success, resetting the failure count and closing the circuit if it was open or half-open.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State returns the current state of the circuit breaker.