retry

package
v0.0.0-...-242e66e Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package retry provides retry logic with exponential backoff and jitter.

The retry strategy uses exponential backoff where each successive retry waits longer, with random jitter to prevent thundering herd problems when many clients retry simultaneously.

Index

Constants

View Source
const (
	// DefaultMaxRetries is the default maximum number of retry attempts.
	DefaultMaxRetries = 3

	// DefaultBaseDelay is the default initial delay between retries.
	DefaultBaseDelay = 100 * time.Millisecond

	// DefaultMaxDelay is the default maximum delay between retries.
	DefaultMaxDelay = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// MaxRetries is the maximum number of retry attempts after the initial
	// request fails. Set to 0 to disable retries.
	MaxRetries int

	// BaseDelay is the initial delay before the first retry.
	// Subsequent retries double this delay up to MaxDelay.
	BaseDelay time.Duration

	// MaxDelay caps the maximum delay between retries.
	MaxDelay time.Duration
}

Config controls retry behavior.

type Retryer

type Retryer struct {
	// contains filtered or unexported fields
}

Retryer calculates retry delays using exponential backoff with jitter.

func New

func New(config Config) *Retryer

New creates a Retryer with the given configuration. Invalid or zero values are replaced with defaults.

func (*Retryer) Backoff

func (r *Retryer) Backoff(attempt int) time.Duration

Backoff calculates the delay before the given attempt. Attempt 0 (initial request) returns 0. Subsequent attempts return exponentially increasing delays with ±25% random jitter.

func (*Retryer) MaxAttempts

func (r *Retryer) MaxAttempts() int

MaxAttempts returns the total number of attempts including the initial request.

func (*Retryer) Wait

func (r *Retryer) Wait(ctx context.Context, attempt int) bool

Wait blocks for the appropriate backoff duration before the given attempt. It returns false if the context is cancelled before the delay elapses.

Jump to

Keyboard shortcuts

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