-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
84 lines (68 loc) · 1.53 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
package pixela
import (
"context"
"math"
"net/http"
"time"
"github.com/pkg/errors"
)
const maxRetryCount = 20
// RetryCount number of retries when an API call is rejected (max: 20)
var RetryCount = 0
// ErrAPICallRejected API call rejected.
// See: https://help.pixe.la/en/blog/release-request-rejecting
var ErrAPICallRejected = errors.New("api call rejected")
type retryer struct {
processFunc func(r *retryer)
maxRetry int
statusCode int
body []byte
err error
}
func (m *retryer) do(ctx context.Context) error {
for i := 0; i <= m.maxRetry; i++ {
m.process()
if !m.shouldRetry() {
return m.err
}
waitTime := m.getWaitTimeExp(i, 100)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Millisecond * time.Duration(waitTime)):
}
}
return ErrAPICallRejected
}
func (m *retryer) process() {
m.processFunc(m)
}
func (m *retryer) shouldRetry() bool {
if m.err != nil {
return false
}
if m.statusCode != http.StatusServiceUnavailable {
return false
}
r, err := parseNormalResponse(m.body)
if err != nil {
m.err = errors.Wrapf(err, "failed to parse normal response")
return false
}
return r.IsRejected
}
func (m *retryer) getWaitTimeExp(retryCount int, baseDelayMilliSeconds int) (delayMilliSeconds int) {
if retryCount == 0 {
return 0
}
return int(math.Pow(2, float64(retryCount)) * float64(baseDelayMilliSeconds))
}
func getRetryCount() int {
if RetryCount < 0 {
return 0
}
if RetryCount > maxRetryCount {
return maxRetryCount
}
return RetryCount
}