-
Notifications
You must be signed in to change notification settings - Fork 9
/
retry.go
86 lines (72 loc) · 2.32 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"net"
"net/http"
"time"
"github.com/cenkalti/backoff/v4"
)
// RetryableTransport wraps an http.RoundTripper and retries on failure.
type RetryableTransport struct {
transport http.RoundTripper
backoffConfig *backoff.ExponentialBackOff
}
// RetryableTransportCfg is the configuration for a RetryableTransport.
type RetryableTransportCfg struct {
Transport http.RoundTripper
InitialInterval time.Duration
MaxInterval time.Duration
MaxElapsedTime time.Duration
}
// NewRetryableTransport creates a new RetryableTransport.
func NewRetryableTransport(cfg *RetryableTransportCfg) *RetryableTransport {
setIfNotZero := func(dst *time.Duration, src time.Duration) {
if src != 0 {
*dst = src
}
}
backoffConfig := backoff.NewExponentialBackOff()
setIfNotZero(&backoffConfig.InitialInterval, cfg.InitialInterval)
setIfNotZero(&backoffConfig.MaxInterval, cfg.MaxInterval)
setIfNotZero(&backoffConfig.MaxElapsedTime, cfg.MaxElapsedTime)
return &RetryableTransport{
transport: cfg.Transport,
backoffConfig: backoffConfig,
}
}
func (r *RetryableTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
startTime := time.Now()
operation := func() error {
resp, err = r.transport.RoundTrip(req)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
return err
}
return backoff.Permanent(err)
}
switch {
case resp.StatusCode >= 500:
resp.Body.Close()
return fmt.Errorf("server error: %d %v", resp.StatusCode, resp.Status)
case resp.StatusCode == http.StatusTooManyRequests:
resp.Body.Close()
if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
if delay, err := time.ParseDuration(retryAfter); err == nil {
if delay > r.backoffConfig.MaxElapsedTime || time.Since(startTime)+delay > r.backoffConfig.MaxElapsedTime {
return backoff.Permanent(fmt.Errorf("retry-after delay is greater than max elapsed time: %v", delay))
}
time.Sleep(delay)
}
}
return fmt.Errorf("rate limit reached: %d %v", resp.StatusCode, resp.Status)
case resp.StatusCode >= 400:
resp.Body.Close()
return backoff.Permanent(fmt.Errorf("client error: %d %v", resp.StatusCode, resp.Status))
}
return nil
}
backoff.Retry(operation, r.backoffConfig)
return resp, err
}