Documentation ¶
Overview ¶
Package retry is a small implementation of the `http.RoundTripper` interface that can be found in `http.Client`. It is responsible to make HTTP requests and can be used to cache or retry them.
By default a request will be tried again only if the response code is below 500 (Internal Server Error) and not 429 (Too Many Requests). This behaviour can be changed with the `WithVerifier` option.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(retries int, backoff time.Duration, options ...func(*roundTripper)) http.RoundTripper
New returns a new `http.RoundTripper` that can retry a request multiple times between which it sleeps a given duration. The options arguments can be used to replace the `http.DefaultTransport` or the function that determines if a request should be retried based on the corresponding `*http.Response`.
Example ¶
Create a new HTTP client that will retry requests five times and sleeps one second between each one. Since the requested URL will always return a status code of 500 (Internal Server Error), this function will run for five seconds and the time it takes to call the endpoint five times.
package main import ( "fmt" "io" "log" "net/http" "os" "time" "github.com/bake/retry" ) func main() { client := &http.Client{ Transport: retry.New(5, time.Second), } res, err := client.Get("https://httpbin.org/status/500") if err != nil { log.Fatal(err) } defer res.Body.Close() fmt.Printf("%s\n", res.Status) if _, err := io.Copy(os.Stdout, res.Body); err != nil { log.Fatal(err) } }
Output:
func WithTransport ¶
func WithTransport(t http.RoundTripper) func(*roundTripper)
WithTransport replaces the `http.DefaultTransport`.
func WithVerifier ¶
func WithVerifier(fn VerifierFunc) func(*roundTripper)
WithVerifier replaces the default verifier. If the given function returns true and there are retries left, another request will be sent.
Example ¶
WithVerifier can be used to modify the default behaviour that is used to determine if a request can be repeated. In this example, all responses that do not succeed (with a status code outside [200-300[) will be tried again.
package main import ( "fmt" "io" "log" "net/http" "os" "time" "github.com/bake/retry" ) func main() { verifier := func(res *http.Response) bool { return 200 > res.StatusCode || res.StatusCode >= 300 } client := &http.Client{ Transport: retry.New(5, time.Second, retry.WithVerifier(verifier)), } res, err := client.Get("https://httpbin.org/status/418") if err != nil { log.Fatal(err) } defer res.Body.Close() fmt.Printf("%s\n", res.Status) if _, err := io.Copy(os.Stdout, res.Body); err != nil { log.Fatal(err) } }
Output:
Types ¶
type VerifierFunc ¶
VerifierFunc can be passed to `WithVerifier` to specify if a request should be tried again based on its response. If it returns true, the request might be repeated.