...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package metadata
16
17 import (
18 "context"
19 "io"
20 "math/rand"
21 "net/http"
22 "time"
23 )
24
25 const (
26 maxRetryAttempts = 5
27 )
28
29 var (
30 syscallRetryable = func(error) bool { return false }
31 )
32
33
34
35 type defaultBackoff struct {
36 max time.Duration
37 mul float64
38 cur time.Duration
39 }
40
41 func (b *defaultBackoff) Pause() time.Duration {
42 d := time.Duration(1 + rand.Int63n(int64(b.cur)))
43 b.cur = time.Duration(float64(b.cur) * b.mul)
44 if b.cur > b.max {
45 b.cur = b.max
46 }
47 return d
48 }
49
50
51 func sleep(ctx context.Context, d time.Duration) error {
52 t := time.NewTimer(d)
53 select {
54 case <-ctx.Done():
55 t.Stop()
56 return ctx.Err()
57 case <-t.C:
58 return nil
59 }
60 }
61
62 func newRetryer() *metadataRetryer {
63 return &metadataRetryer{bo: &defaultBackoff{
64 cur: 100 * time.Millisecond,
65 max: 30 * time.Second,
66 mul: 2,
67 }}
68 }
69
70 type backoff interface {
71 Pause() time.Duration
72 }
73
74 type metadataRetryer struct {
75 bo backoff
76 attempts int
77 }
78
79 func (r *metadataRetryer) Retry(status int, err error) (time.Duration, bool) {
80 if status == http.StatusOK {
81 return 0, false
82 }
83 retryOk := shouldRetry(status, err)
84 if !retryOk {
85 return 0, false
86 }
87 if r.attempts == maxRetryAttempts {
88 return 0, false
89 }
90 r.attempts++
91 return r.bo.Pause(), true
92 }
93
94 func shouldRetry(status int, err error) bool {
95 if 500 <= status && status <= 599 {
96 return true
97 }
98 if err == io.ErrUnexpectedEOF {
99 return true
100 }
101
102 if syscallRetryable(err) {
103 return true
104 }
105 if err, ok := err.(interface{ Temporary() bool }); ok {
106 if err.Temporary() {
107 return true
108 }
109 }
110 if err, ok := err.(interface{ Unwrap() error }); ok {
111 return shouldRetry(status, err.Unwrap())
112 }
113 return false
114 }
115
View as plain text