retry

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// DefaultMaxAttempts is the default number of total attempts for network operations.
	DefaultMaxAttempts = 3

	// DefaultInitialDelay is the initial delay before the first retry.
	DefaultInitialDelay = 2 * time.Second

	// DefaultMaxDelay is the maximum delay between retry attempts.
	DefaultMaxDelay = 30 * time.Second

	// DefaultBackoffMultiplier is the multiplier applied to the delay on each subsequent retry.
	DefaultBackoffMultiplier = 2.0
)

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, cfg Config, operation func() error) error

Do executes the given operation, retrying on error according to cfg. It uses exponential backoff with random jitter between attempts and respects context cancellation during waits.

If cfg is zero-valued, DefaultConfig is used.

Types

type Config

type Config struct {
	// MaxAttempts is the total number of attempts (including the initial one).
	// Values <= 0 are treated as 1 (no retries).
	MaxAttempts int

	// InitialDelay is the base delay before the first retry.
	InitialDelay time.Duration

	// MaxDelay caps the delay between any two consecutive attempts.
	MaxDelay time.Duration

	// BackoffMultiplier scales the delay on each successive retry.
	BackoffMultiplier float64
}

Config controls the behavior of Do. A zero-value Config will be treated as DefaultConfig.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults for network operations.

func Disabled

func Disabled() Config

Disabled returns a Config that performs exactly one attempt (no retries).

Jump to

Keyboard shortcuts

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