Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidAttempts = errors.New("attempts must be >= 1")
ErrInvalidAttempts is returned when attempts < 1
var ErrOpen = errors.New("circuit breaker open")
ErrOpen is returned when the circuit breaker is open and calls are rejected.
Functions ¶
func Retry ¶
func Retry(ctx context.Context, attempts int, initial time.Duration, op func(context.Context) error) error
Retry runs the provided operation up to `attempts` times using exponential backoff starting from `initial`. The operation receives the context as its first argument and should respect it. If the context is canceled, Retry returns ctx.Err(). If attempts is exhausted, the last operation error is returned.
func WithTimeout ¶
WithTimeout runs op with a child context that has the provided timeout. If timeout <= 0 the operation is invoked directly with the original context. The function returns the operation error, or a context error if the parent context is canceled or the timeout elapses before op returns.
Note: if op ignores context cancellation it may continue running in the background; WithTimeout returns early when the timeout elapses.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker is a minimal, thread-safe circuit breaker. - CLOSED: calls are executed; failures are counted - OPEN: calls are rejected until openTimeout elapses - HALF-OPEN: a single trial call is allowed; success -> CLOSED, failure -> OPEN
func NewCircuitBreaker ¶
func NewCircuitBreaker(failureThreshold int, openTimeout time.Duration) *CircuitBreaker
NewCircuitBreaker creates a new CircuitBreaker. failureThreshold must be >= 1. openTimeout controls how long the breaker stays OPEN before transitioning to HALF-OPEN.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State returns the current breaker state.