-
Notifications
You must be signed in to change notification settings - Fork 8
/
retries.go
116 lines (102 loc) · 1.95 KB
/
retries.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gocardless
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
type temporary interface {
Temporary() bool
}
type waiter interface {
Wait()
}
func try(attempts int, fn func() error) error {
var err error
if attempts < 1 {
attempts = 1
}
for i := attempts; i > 0; i-- {
err = fn()
if err == nil {
return nil
}
if t, ok := err.(temporary); !ok || !t.Temporary() {
return err
}
if w, ok := err.(waiter); ok {
w.Wait()
}
}
return err
}
func responseErr(r *http.Response) error {
switch {
case 200 <= r.StatusCode && r.StatusCode < 400:
return nil
default:
var cause error
var result struct {
Err *APIError `json:"error"`
}
json.NewDecoder(r.Body).Decode(&result)
if result.Err != nil {
cause = result.Err
}
return &responseError{
res: r,
err: cause,
}
}
}
type responseError struct {
res *http.Response
err error
}
func (r *responseError) Cause() error {
return r.err
}
func (r *responseError) Unwrap() error {
return r.err
}
func (r *responseError) Error() string {
return r.res.Status
}
func (r *responseError) Temporary() bool {
switch {
case 500 <= r.res.StatusCode:
return true
case r.res.StatusCode == http.StatusTooManyRequests:
return true
default:
return false
}
}
func (r *responseError) Wait() {
rem, err := strconv.Atoi(r.res.Header.Get("RateLimit-Remaining"))
if err != nil || rem > 0 {
return
}
reset := r.res.Header.Get("RateLimit-Reset")
t, err := time.Parse(time.RFC1123, reset)
if err != nil {
t, err = time.Parse(time.RFC1123Z, reset)
}
if err != nil {
return
}
time.Sleep(time.Until(t))
}
// NewIdempotencyKey generates a random and unique idempotency key
func NewIdempotencyKey() string {
buf := make([]byte, 10)
_, err := rand.Read(buf)
if err != nil {
panic("failed do generate random key")
}
t := time.Now().UTC().UnixNano()
return fmt.Sprintf("%d-%s", t, base64.RawURLEncoding.EncodeToString(buf))
}