resilience

package
v1.3.27 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidAttempts = errors.New("attempts must be >= 1")

ErrInvalidAttempts is returned when attempts < 1

View Source
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

func WithTimeout(ctx context.Context, timeout time.Duration, op func(context.Context) error) error

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) Execute

func (cb *CircuitBreaker) Execute(ctx context.Context, op func(context.Context) error) error

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the current breaker state.

type State

type State int

State represents the circuit breaker state.

const (
	StateClosed State = iota
	StateOpen
	StateHalfOpen
)

Jump to

Keyboard shortcuts

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