httpretrier

package
v1.90.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package httpretrier provides the ability to automatically repeat HTTP requests based on user-defined conditions.

Any part of the returned http.Response (e.g., StatusCode) and error can be used to define the retry function (retry condition). The default behavior is to retry only in case of an error.

This package also offers ready-made retry functions that can be used for most cases:

- RetryIfForWriteRequests - RetryIfForReadRequests

Additionally, it allows you to set the maximum number of retries, the delay after the first failed attempt, the time multiplication factor to determine the successive delay value, and the jitter used to introduce randomness and avoid request collisions.

Index

Constants

View Source
const (
	// DefaultAttempts is the default maximum number of retry attempts.
	DefaultAttempts = 4

	// DefaultDelay is the delay to apply after the first failed attempt.
	DefaultDelay = 1 * time.Second

	// DefaultDelayFactor is the default multiplication factor to get the successive delay value.
	DefaultDelayFactor = 2

	// DefaultJitter is the maximum random Jitter time between retries.
	DefaultJitter = 100 * time.Millisecond
)

Variables

This section is empty.

Functions

func RetryIfForReadRequests

func RetryIfForReadRequests(r *http.Response, err error) bool

RetryIfForReadRequests is a retry check function used for read requests (e.g. GET requests that are guaranteed to not modify the remote state).

func RetryIfForWriteRequests

func RetryIfForWriteRequests(r *http.Response, err error) bool

RetryIfForWriteRequests is a retry check function used for write requests (e.g. PUT/PATCH/POST requests that can modify the remote state).

Types

type HTTPClient

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

HTTPClient contains the function to perform the actual HTTP request.

type HTTPRetrier

type HTTPRetrier struct {
	// contains filtered or unexported fields
}

HTTPRetrier represents an instance of the HTTP retrier.

func New

func New(httpClient HTTPClient, opts ...Option) (*HTTPRetrier, error)

New creates a new instance.

func (*HTTPRetrier) Do

func (c *HTTPRetrier) Do(r *http.Request) (*http.Response, error)

Do attempts to run the request according to the retry rules.

type Option

type Option func(c *HTTPRetrier) error

Option is the interface that allows to set the options.

func WithAttempts

func WithAttempts(attempts uint) Option

WithAttempts set the maximum number of retries.

func WithDelay

func WithDelay(delay time.Duration) Option

WithDelay set the delay after the first failed attempt.

func WithDelayFactor

func WithDelayFactor(delayFactor float64) Option

WithDelayFactor set the multiplication factor to get the successive delay value. A delay factor greater than 1 means an exponential delay increase. if the delay factor is 2 and the first delay is 1, then the delays will be: [1, 2, 4, 8, ...].

func WithJitter

func WithJitter(jitter time.Duration) Option

WithJitter sets the maximum random Jitter time between retries. This is useful to avoid the Thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem).

func WithRetryIfFn

func WithRetryIfFn(retryIfFn RetryIfFn) Option

WithRetryIfFn set the function used to decide when retry.

type RetryIfFn

type RetryIfFn func(r *http.Response, err error) bool

RetryIfFn is the signature of the function used to decide when retry.

Jump to

Keyboard shortcuts

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