Documentation
¶
Overview ¶
Package retryhttp provides a simple client that retries HTTP transactions according to a policy.
Typical usage:
client, _ := retryhttp.NewClient(
retryhttp.NewClient( // can omit to use http.DefaultClient
new(http.Client),
),
retryhttp.WithRunner(
retry.WithPolicyFactory(
retry.Config{
Interval: 5 * time.Second,
MaxRetries: 5,
},
),
),
retryhttp.WithShouldRetry(
http.StatusTooManyRequests,
),
)
request, _ := http.NewRequest("GET", "https://foobar.com/", nil)
response, err := client.Do(request)
// normal response and error handling ...
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanupResponse ¶
CleanupResponse is an OnAttempt that drains and closes the response body between each attempt. If the given attempt is the last one, including if it represents an error, the response is left as is for the Client's caller to deal with.
func NewShouldRetry ¶
func NewShouldRetry(statusCodes ...int) retry.ShouldRetry[*http.Response]
NewShouldRetry creates a retry predicate that retries any of the given status codes from a valid response.
The returned predicate will retry any errors that provide a 'Temporary() bool' method that returns true. Any other kind of error is considered fatal and will halt retries.
func WithShouldRetry ¶
func WithShouldRetry(statusCodes ...int) retry.RunnerOption[*http.Response]
WithShouldRetry creates a Client runner option that retries the given status codes. NewShouldRetry is used to create the retry predicate.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTPClient that retries HTTP requests according to a retry policy established with WithRunner.
func NewClient ¶
func NewClient(opts ...ClientOption) (c *Client, err error)
NewClient creates a Client from a set of options. If no options are passed, the returned Client will never retry any request and will use http.DefaultClient to execute transactions.
func (*Client) Do ¶
Do executes the HTTP transaction specified by the request. The runner specified via WithRunner is used to retry requests.
The context of the original request is used as the absolute bounds for all retries. If the request was not created with a context, then only the retry policy will govern any limits.
If a body should be transmitted with each attempt, a caller must either specify a Request.GetBody or use a Requester to set the body. A Requester will cause the use of the same body for all requests sent through this client, which may not be desirable. Set the request's GetBody, just as one would for redirects, to allow per-request bodies. Note that http.NewRequest and http.NewRequestWithContext both set GetBody for common standard library types.
type ClientOption ¶
type ClientOption interface {
// contains filtered or unexported methods
}
ClientOption is a configurable option for a Client.
func WithHTTPClient ¶
func WithHTTPClient(hc HTTPClient) ClientOption
WithHTTPClient associates the given HTTP client with the Client being created. If this option is not supplied, http.DefaultClient is used.
func WithRequesters ¶
func WithRequesters(r ...Requester) ClientOption
WithRequesters appends Requester strategies to the client. Multiple uses of this option are cumulative.
func WithRunner ¶
func WithRunner(r retry.Runner[*http.Response]) ClientOption
WithRunner sets the retry.Runner for the Client. In order to prevent leaking response bodies between retry attempts, the given runner should have the CleanupResponse retry.OnAttempt appended to its options.
type HTTPClient ¶
HTTPClient is the required behavior of anything that can execute HTTP transactions.
type Requester ¶
Requester is a strategy for modifying an HTTP request before it is attempted. The supplied request can be modified and returned, or a new request may be created. If a new request is created, it should incorporate the context from the supplied request.
Callers can use one or more Requesters to tailor the request prior to each attempt. Authorization is an example of something that might need to be supplied before each attempt instead of in the original request.