func TestRetryTimeout(t *testing.T) { d := time.Millisecond rt := &fixedRoundTripper{failUntil: time.Now().Add(d)} c := http.Client{} c.Transport = retry.Retry(retry.MaxAttempts(0), retry.Timeout(2*d), retry.Next(rt)) resp, err := c.Get("http://bar") if err != nil { t.Fatal(err) } if want, have := http.StatusTeapot, resp.StatusCode; want != have { t.Fatalf("want %v, have %v", want, have) } t.Logf("just FYI, it took %d attempt(s) to succeed", rt.count) }
func TestRetryMaxAttempts(t *testing.T) { rt := &fixedRoundTripper{failFor: 2} c := http.Client{} c.Transport = retry.Retry(retry.MaxAttempts(3), retry.Next(rt)) resp, err := c.Get("http://foo") if err != nil { t.Fatal(err) } if want, have := http.StatusTeapot, resp.StatusCode; want != have { t.Fatalf("want %v, have %v", want, have) } if want, have := 3, rt.count; want != have { t.Errorf("want %v, have %v", want, have) } }
// ExampleCustomUsage shows how to wire up a customized DNS SRV proxy into // http.DefaultClient. func ExampleCustomUsage() { var rt http.RoundTripper rt = http.DefaultTransport rt = proxy.Proxy( proxy.Next(rt), proxy.Scheme("https"), proxy.PoolReporter(os.Stderr), proxy.Factory(pool.RoundRobin), ) rt = retry.Retry( retry.Next(rt), retry.Pass(func(resp *http.Response, err error) error { if err != nil { return err } if resp.StatusCode >= 500 { return fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status) } return nil }), retry.MaxAttempts(10), retry.Timeout(750*time.Millisecond), ) t := &http.Transport{} t.RegisterProtocol("dnssrv", rt) http.DefaultClient.Transport = t resp, err := http.Get("dnssrv://foo.bar.srv.internal.name/normal/path?key=value") if err != nil { log.Fatal(err) } io.Copy(os.Stdout, resp.Body) resp.Body.Close() }