Documentation
¶
Index ¶
- Variables
- func Do(ctx context.Context, fn func() error, log *slog.Logger, backoff time.Duration, ...) error
- type Breaker
- func (b *Breaker) Do(ctx context.Context, fn func() error) error
- func (b *Breaker) DoWithOutcome(ctx context.Context, fn func() error) Outcome
- func (b *Breaker) Run(ctx context.Context, fn func(attempt int) Result) error
- func (b *Breaker) RunWithOutcome(ctx context.Context, fn func(attempt int) Result) Outcome
- type Option
- type Outcome
- type Result
Constants ¶
This section is empty.
Variables ¶
var ( ErrFatal = errors.New("breaker: fatal error") ErrHitMaxRetries = errors.New("breaker: hit max retries") )
Functions ¶
Types ¶
type Breaker ¶ added in v0.1.0
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is an exponential-backoff-retry caller with optional jitter.
func Default ¶ added in v0.1.0
Default returns a Breaker with sensible defaults: 1s backoff, 2x factor, 15 retries, no jitter. An optional logger may be provided for retry events.
func New ¶ added in v0.1.0
func New(log *slog.Logger, backoff time.Duration, factor float64, maxTries int, opts ...Option) *Breaker
New creates a Breaker with the given backoff, exponential factor, and maximum number of retries. Functional options (e.g. WithJitter) can be appended to customise behaviour. The logger may be nil to suppress retry log output.
func (*Breaker) Do ¶ added in v0.1.0
Do is an exponential-backoff-retry caller which will wait `backoff*factor**retry` up to `maxTries`. `maxTries = 1` means retry only once when an error occurs. Backoff sleeps respect context cancellation.
func (*Breaker) DoWithOutcome ¶ added in v0.3.0
DoWithOutcome behaves like Do but returns an Outcome with attempt and timing metadata.
type Option ¶ added in v0.3.0
type Option func(*Breaker)
Option configures a Breaker.
func WithJitter ¶ added in v0.3.0
WithJitter sets the jitter fraction applied to each backoff delay. A value of 0.1 means ±10% randomisation around the computed delay. The value is clamped to the range [0, 1].
type Outcome ¶ added in v0.3.0
type Outcome struct {
Err error
Attempts int // total number of fn invocations
Retried bool // true if fn was called more than once
Latency time.Duration // wall-clock time from start to return
}
Outcome holds metadata about a Do or Run invocation.