retry

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package retry provides a generic retry mechanism with configurable backoff strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

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

Do retries fn until it succeeds, max attempts are exhausted, or ctx is cancelled. Returns the last error if all attempts fail.

func DoWithResult

func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)

DoWithResult retries fn until it succeeds and returns both the result and error.

func Exponential

func Exponential(ctx context.Context, maxAttempts int, initDelay time.Duration, fn func(ctx context.Context) error) error

Exponential retries fn with exponential backoff.

func Simple

func Simple(ctx context.Context, maxAttempts int, delay time.Duration, fn func(ctx context.Context) error) error

Simple retries fn up to maxAttempts with constant delay.

Types

type Config

type Config struct {
	MaxAttempts int
	InitDelay   time.Duration
	MaxDelay    time.Duration
	Multiplier  float64          // backoff multiplier (1.0 = constant, 2.0 = exponential)
	Jitter      bool             // add random jitter to delay
	RetryIf     func(error) bool // only retry if this returns true; nil means always retry
}

Config configures the retry behavior.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a config with 3 attempts, exponential backoff starting at 100ms.

Jump to

Keyboard shortcuts

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