retry

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package retry provides retry logic with exponential backoff.

Index

Constants

View Source
const MaxRetryAfter = 10 * time.Minute

MaxRetryAfter is the maximum server-supplied Retry-After delay that will be honored. A hostile or broken server could otherwise park the retry loop indefinitely; above this cap the calculated backoff is used instead.

Variables

This section is empty.

Functions

func CalculateBackoff

func CalculateBackoff(cfg Config, attempt int) time.Duration

CalculateBackoff returns the backoff duration for the given attempt. Attempt is 0-indexed (first retry is attempt 0). Formula: initial * 2^attempt, uncapped per SDK requirements §9.3. The result is clamped to MaxInt64 nanoseconds so extreme user-configured retry counts cannot overflow time.Duration.

func ParseRetryAfter

func ParseRetryAfter(resp *http.Response) time.Duration

ParseRetryAfter parses the Retry-After header value. Returns the duration to wait, or 0 if not present/parseable.

func ShouldRetryError

func ShouldRetryError(err error) bool

ShouldRetryError returns true if the error is transient and the request can be retried. It unwraps errors to check the underlying cause.

func ShouldRetryStatus

func ShouldRetryStatus(statusCode int) bool

ShouldRetryStatus returns true if the HTTP status code indicates the request can be retried. Only 501-599 are retryable. 429 (rate limit) and 500 (internal server error) are NOT retried.

func Wait

func Wait(ctx context.Context, d time.Duration) error

Wait waits for the specified duration or until the context is canceled. Returns ctx.Err() if the context is canceled.

Types

type Config

type Config struct {
	MaxRetries     int
	InitialBackoff time.Duration
	Multiplier     float64
}

Config holds retry configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default retry configuration. MaxRetries can be overridden by the user via WithMaxRetries.

Jump to

Keyboard shortcuts

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