retry

package
v1.9.6 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package retry provides opt-in HTTP retries for providers, paced by a Policy that callers retrying at another layer, such as a whole stream, can pace their retries with too.

Providers never retry. Pass a client from NewHTTPClient as a provider Config.HTTPClient to opt in. Enabling retries authorizes repeated requests and possible duplicate charges: a transient HTTP status does not establish that an operation was not processed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPClient

func NewHTTPClient(base *http.Client, policy *Policy) *http.Client

NewHTTPClient returns a shallow copy of base, http.DefaultClient when nil, whose Transport retries, according to policy, complete responses that providers report as temporary errors: 408, 429 and 5xx statuses but 501, 505 and Cloudflare's 525 and 526, except those the body shows to be exhausted quota, authentication, content filter or context overflow failures. A nil policy or MaxAttempts <= 1 disables retries. Only replayable request bodies are resent; replayable means the bytes can be resent, not that the operation is idempotent. A wait that would reach the request's deadline returns the response instead. Transport failures and response-body errors, including interrupted successful streams, are never retried.

Types

type Policy

type Policy struct {
	// MaxAttempts counts the first attempt; 1 or less disables retries.
	MaxAttempts int
	// Delays grow from InitialDelay by Multiplier up to MaxDelay, each
	// varied by up to ±25%.
	InitialDelay time.Duration
	MaxDelay     time.Duration
	Multiplier   float64
	// MaxRetryAfter bounds the wait a server suggests, in a Retry-After
	// header or a Google API error body, which is taken instead even beyond
	// MaxDelay. One beyond it ends retrying with the response, whose error
	// reports the wait in RetryAfter.
	MaxRetryAfter time.Duration
}

Policy controls retries. Zero durations and multiplier take the DefaultPolicy values.

func DefaultPolicy

func DefaultPolicy() *Policy

DefaultPolicy returns a conservative retry policy for complete retryable HTTP responses. It does not retry network write/read errors because a POST may already have been processed by the provider. Its MaxRetryAfter of a minute gives up on Cloudflare's origin timeouts (504, 521–524), which ask for two.

func (Policy) Delay added in v1.9.4

func (p Policy) Delay(attempt int, retryAfter time.Duration) (time.Duration, bool)

Delay returns the wait before the attempt after attempt, the first being 1: retryAfter, when the server suggests a wait, or else the backoff. It returns false when retryAfter is longer than MaxRetryAfter, which ends retrying.

Jump to

Keyboard shortcuts

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