backoff

package
v1.0.85 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRetryConfig = RetryConfig{
	MinWait:    1 * time.Second,
	MaxWait:    30 * time.Second,
	MaxRetries: 4,
	CheckRetry: DefaultRetryPolicy,
	Backoff:    DefaultBackoff,
}

DefaultRetryConfig is the default config for retrying http requests.

Functions

func DefaultBackoff

func DefaultBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration

DefaultBackoff provides a callback for retryablehttp.Client.Backoff which will perform exponential backoff based on the attempt number and limited by the provided minimum and maximum durations. On 429 responses it will try to parse the Retry-After header use that value as backoff. Will fallback to exponential backoff if the Retry-After header is not present or cannot be parsed.

func DefaultRetryPolicy

func DefaultRetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error)

DefaultRetryPolicy provides a callback for retryablehttp.Client.CheckRetry, which will retry on connection errors, server errors and request throttling.

Types

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is the interface of an http client that is compatible with *http.Client.

func WithRetries

func WithRetries(httpClient *http.Client, config *RetryConfig) HTTPClient

WithRetries wraps httpClient with backoff-retry logic.

type RetryConfig

type RetryConfig struct {
	// MinWait is the minimum time to wait before retrying.
	MinWait time.Duration

	// MaxWait is the maximum time to wait before retrying. When using
	// exponential backoff, this is the upper limit for the wait duration.
	MaxWait time.Duration

	// MaxRetries is the maximum number of retries to perform until giving up.
	MaxRetries int

	// CheckRetry is a function that decides whether a request should be
	// retried or not.
	CheckRetry retryablehttp.CheckRetry

	// Backoff calculates the sleep duration for the next retry.
	Backoff retryablehttp.Backoff
}

RetryConfig configures the backoff-retry behaviour.

Jump to

Keyboard shortcuts

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