retry

package
v0.39.8 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package retry provides a generic retry utility with exponential backoff and jitter for transient network failures.

Index

Constants

View Source
const ComponentRetry = "retry"

ComponentRetry is the log component name for retry operations.

Variables

This section is empty.

Functions

func Do

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

Do executes fn, retrying on transient errors with exponential backoff and jitter. It respects context cancellation and logs each retry attempt. Returns the last error if all attempts are exhausted.

func IsTransient

func IsTransient(err error) bool

IsTransient returns true for errors that are typically transient network failures worth retrying: timeouts, DNS errors, connection refused, and connection reset.

Types

type Config

type Config struct {
	// MaxAttempts is the total number of attempts (initial + retries).
	// A value of 1 means no retries. Zero defaults to 3.
	MaxAttempts int

	// InitialDelay is the base delay before the first retry.
	// Zero defaults to 1 second.
	InitialDelay time.Duration

	// MaxDelay caps the backoff delay. Zero defaults to 30 seconds.
	MaxDelay time.Duration

	// Multiplier scales the delay between retries. Zero defaults to 2.0.
	Multiplier float64

	// IsRetryable determines whether an error should be retried.
	// If nil, the default transient error classifier is used.
	IsRetryable func(error) bool
}

Config controls retry behavior.

Jump to

Keyboard shortcuts

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