Categorygithub.com/pmalekn/go-retryablehttp
modulepackage
0.6.5
Repository: https://github.com/pmalekn/go-retryablehttp.git
Documentation: pkg.go.dev

# README

go-retryablehttp

Build Status Go Documentation

The retryablehttp package provides a familiar HTTP client interface with automatic retries and exponential backoff. It is a thin wrapper over the standard net/http client library and exposes nearly the same public API. This makes retryablehttp very easy to drop into existing programs.

retryablehttp performs automatic retries under certain conditions. Mainly, if an error is returned by the client (connection errors, etc.), or if a 500-range response code is received (except 501), then a retry is invoked after a wait period. Otherwise, the response is returned and left to the caller to interpret.

The main difference from net/http is that requests which take a request body (POST/PUT et. al) can have the body provided in a number of ways (some more or less efficient) that allow "rewinding" the request body if the initial request fails so that the full request can be attempted again. See the godoc for more details.

Version 0.6.0 and before are compatible with Go prior to 1.12. From 0.6.1 onward, Go 1.12+ is required.

Example Use

Using this library should look almost identical to what you would do with net/http. The most simple example of a GET request is shown below:

resp, err := retryablehttp.Get("/foo")
if err != nil {
    panic(err)
}

The returned response object is an *http.Response, the same thing you would usually get from net/http. Had the request failed one or more times, the above call would block and retry with exponential backoff.

For more usage and examples see the godoc.

# Functions

DefaultBackoff provides a default callback for Client.Backoff which will perform exponential backoff based on the attempt number and limited by the provided minimum and maximum durations.
DefaultRetryPolicy provides a default callback for Client.CheckRetry, which will retry on connection errors and server errors.
FromRequest wraps an http.Request in a retryablehttp.Request.
Get is a shortcut for doing a GET request without making a new client.
Head is a shortcut for doing a HEAD request without making a new client.
LinearJitterBackoff provides a callback for Client.Backoff which will perform linear backoff based on the attempt number and with jitter to prevent a thundering herd.
NewClient creates a new Client with default settings.
NewRequest creates a new wrapped request.
PassthroughErrorHandler is an ErrorHandler that directly passes through the values from the net/http library for the final request.
Post
Post is a shortcut for doing a POST request without making a new client.
PostForm is a shortcut to perform a POST with form data without creating a new client.

# Structs

Client is used to make HTTP requests.
Request wraps the metadata needed to create HTTP requests.

# Interfaces

LenReader is an interface implemented by many in-memory io.Reader's.
LeveledLogger interface implements the basic methods that a logger library needs.
Logger interface allows to use other loggers than standard log.Logger.

# Type aliases

Backoff specifies a policy for how long to wait between retries.
CheckRetry specifies a policy for handling retries.
ErrorHandler is called if retries are expired, containing the last status from the http library.
ReaderFunc is the type of function that can be given natively to NewRequest.
RequestLogHook allows a function to run before each retry.
ResponseLogHook is like RequestLogHook, but allows running a function on each HTTP response.