Debian Codegraph @

v5/round_trip_with_retry_backoff.go

raw
1 package v5
2
3 import (
4 "fmt"
5 "log"
6 "net/http"
7 "time"
8
9 "github.com/cenkalti/backoff"
10 )
11
12 // net/http RoundTripper interface, a.k.a. Transport
13 // https://godoc.org/net/http#RoundTripper
14 type RoundTripWithRetryBackoff struct {
15 // Configuration fields for backoff.ExponentialBackOff
16 InitialIntervalSeconds int64
17 RandomizationFactor float64
18 Multiplier float64
19 MaxIntervalSeconds int64
20 // After MaxElapsedTime the ExponentialBackOff stops.
21 // It never stops if MaxElapsedTime == 0.
22 MaxElapsedTimeSeconds int64
23 }
24
25 func (r RoundTripWithRetryBackoff) RoundTrip(req *http.Request) (*http.Response, error) {
26 var lastResponse *http.Response
27 var lastError error
28
29 retryableRoundTrip := func() error {
30 lastResponse = nil
31 lastError = nil
32
33 lastResponse, lastError = http.DefaultTransport.RoundTrip(req)
34 // Detect Heroku API rate limiting
35 // https://devcenter.heroku.com/articles/platform-api-reference#client-error-responses
36 if lastResponse != nil && lastResponse.StatusCode == 429 {
37 return fmt.Errorf("Heroku API rate limited: 429 Too Many Requests")
38 }
39 return nil
40 }
41
42 rateLimitRetryConfig := &backoff.ExponentialBackOff{
43 Clock: backoff.SystemClock,
44 InitialInterval: time.Duration(int64WithDefault(r.InitialIntervalSeconds, int64(30))) * time.Second,
45 RandomizationFactor: float64WithDefault(r.RandomizationFactor, float64(0.25)),
46 Multiplier: float64WithDefault(r.Multiplier, float64(2)),
47 MaxInterval: time.Duration(int64WithDefault(r.MaxIntervalSeconds, int64(900))) * time.Second,
48 MaxElapsedTime: time.Duration(int64WithDefault(r.MaxElapsedTimeSeconds, int64(0))) * time.Second,
49 }
50 rateLimitRetryConfig.Reset()
51
52 err := backoff.RetryNotify(retryableRoundTrip, rateLimitRetryConfig, notifyLog)
53 // Propagate the rate limit error when retries eventually fail.
54 if err != nil {
55 if lastResponse != nil {
56 lastResponse.Body.Close()
57 }
58 return nil, err
59 }
60 // Propagate all other response errors.
61 if lastError != nil {
62 if lastResponse != nil {
63 lastResponse.Body.Close()
64 }
65 return nil, lastError
66 }
67
68 return lastResponse, nil
69 }
70
71 func int64WithDefault(v int64, defaultV int64) int64 {
72 if v == int64(0) {
73 return defaultV
74 } else {
75 return v
76 }
77 }
78
79 func float64WithDefault(v float64, defaultV float64) float64 {
80 if v == float64(0) {
81 return defaultV
82 } else {
83 return v
84 }
85 }
86
87 func notifyLog(err error, waitDuration time.Duration) {
88 log.Printf("Will retry Heroku API request in %s, because %s", waitDuration, err)
89 }