Documentation ¶
Overview ¶
This package provides exponential backoff support for making HTTP requests.
It uses the github.com/cenkalti/backoff algorithm.
Network failures and HTTP 5xx status codes qualify for retries.
HTTP calls that return HTTP 4xx status codes do not get retried.
If the last HTTP request made does not result in a 2xx HTTP status code, an error is returned, together with the data.
There are several utility methods that wrap the standard net/http package calls.
Any function that takes no arguments and returns (*http.Response, error) can be retried using this library's Retry function.
The methods in this library should be able to run concurrently in multiple go routines.
Example Usage ¶
Consider this trivial HTTP GET request:
res, err := http.Get("http://www.google.com/robots.txt")
This can be rewritten as follows, enabling automatic retries:
res, attempts, err := httpbackoff.Get("http://www.google.com/robots.txt")
The variable attempts stores the number of http calls that were made (one plus the number of retries).
Index ¶
- func ClientDo(c *http.Client, req *http.Request) (resp *http.Response, attempts int, err error)
- func ClientGet(c *http.Client, url string) (resp *http.Response, attempts int, err error)
- func ClientHead(c *http.Client, url string) (resp *http.Response, attempts int, err error)
- func ClientPost(c *http.Client, url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
- func ClientPostForm(c *http.Client, url string, data url.Values) (resp *http.Response, attempts int, err error)
- func Get(url string) (resp *http.Response, attempts int, err error)
- func Head(url string) (resp *http.Response, attempts int, err error)
- func Post(url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
- func PostForm(url string, data url.Values) (resp *http.Response, attempts int, err error)
- func Retry(httpCall func() (resp *http.Response, tempError error, permError error)) (*http.Response, int, error)
- type BadHttpResponseCode
- type Client
- func (httpRetryClient *Client) ClientDo(c *http.Client, req *http.Request) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) ClientGet(c *http.Client, url string) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) ClientHead(c *http.Client, url string) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) ClientPost(c *http.Client, url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) ClientPostForm(c *http.Client, url string, data url.Values) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) Get(url string) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) Head(url string) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) Post(url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) PostForm(url string, data url.Values) (resp *http.Response, attempts int, err error)
- func (httpRetryClient *Client) Retry(httpCall func() (resp *http.Response, tempError error, permError error)) (*http.Response, int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientDo ¶
ClientDo works the same as HTTPRetryClient.ClientDo, but uses the default exponential back off settings
func ClientGet ¶
ClientGet works the same as HTTPRetryClient.ClientGet, but uses the default exponential back off settings
func ClientHead ¶
ClientHead works the same as HTTPRetryClient.ClientHead, but uses the default exponential back off settings
func ClientPost ¶
func ClientPost(c *http.Client, url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
ClientPost works the same as HTTPRetryClient.ClientPost, but uses the default exponential back off settings
func ClientPostForm ¶
func ClientPostForm(c *http.Client, url string, data url.Values) (resp *http.Response, attempts int, err error)
ClientPostForm works the same as HTTPRetryClient.ClientPostForm, but uses the default exponential back off settings
func Get ¶
Get works the same as HTTPRetryClient.Get, but uses the default exponential back off settings
func Head ¶
Head works the same as HTTPRetryClient.Head, but uses the default exponential back off settings
func Post ¶
Post works the same as HTTPRetryClient.Post, but uses the default exponential back off settings
Types ¶
type BadHttpResponseCode ¶
Any non 2xx HTTP status code is considered a bad response code, and will result in a BadHttpResponseCode.
func (BadHttpResponseCode) Error ¶
func (err BadHttpResponseCode) Error() string
Returns an error message for this bad HTTP response code
type Client ¶
type Client struct {
BackOffSettings *backoff.ExponentialBackOff
}
func (*Client) ClientDo ¶
func (httpRetryClient *Client) ClientDo(c *http.Client, req *http.Request) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Client.Do where attempts is the number of http calls made (one plus number of retries).
func (*Client) ClientGet ¶
func (httpRetryClient *Client) ClientGet(c *http.Client, url string) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Client.Get where attempts is the number of http calls made (one plus number of retries).
func (*Client) ClientHead ¶
func (httpRetryClient *Client) ClientHead(c *http.Client, url string) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Client.Head where attempts is the number of http calls made (one plus number of retries).
func (*Client) ClientPost ¶
func (httpRetryClient *Client) ClientPost(c *http.Client, url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Client.Post where attempts is the number of http calls made (one plus number of retries).
func (*Client) ClientPostForm ¶
func (httpRetryClient *Client) ClientPostForm(c *http.Client, url string, data url.Values) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Client.PostForm where attempts is the number of http calls made (one plus number of retries).
func (*Client) Get ¶
Retry wrapper for http://golang.org/pkg/net/http/#Get where attempts is the number of http calls made (one plus number of retries).
func (*Client) Head ¶
Retry wrapper for http://golang.org/pkg/net/http/#Head where attempts is the number of http calls made (one plus number of retries).
func (*Client) Post ¶
func (httpRetryClient *Client) Post(url string, bodyType string, body []byte) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#Post where attempts is the number of http calls made (one plus number of retries).
func (*Client) PostForm ¶
func (httpRetryClient *Client) PostForm(url string, data url.Values) (resp *http.Response, attempts int, err error)
Retry wrapper for http://golang.org/pkg/net/http/#PostForm where attempts is the number of http calls made (one plus number of retries).
func (*Client) Retry ¶
func (httpRetryClient *Client) Retry(httpCall func() (resp *http.Response, tempError error, permError error)) (*http.Response, int, error)
Retry is the core library method for retrying http calls.
httpCall should be a function that performs the http operation, and returns (resp *http.Response, tempError error, permError error). Errors that should cause retries should be returned as tempError. Permanent errors that should not result in retries should be returned as permError. Retries are performed using the exponential backoff algorithm from the github.com/cenkalti/backoff package. Retry automatically treats HTTP 5xx status codes as a temporary error, and any other non-2xx HTTP status codes as a permanent error. Thus httpCall function does not need to handle the HTTP status code of resp, since Retry will take care of it.
Concurrent use of this library method is supported.