-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
145 lines (115 loc) · 3.08 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package goworker
import (
"context"
"math"
"time"
"github.com/lufred/goworker/common"
)
const RetryDone = time.Duration(-1)
const (
defaultInitialInterval = time.Second
defaultBackoffCoefficient = 2.0
defaultMaximumIntervalCoefficient = 10.0
defaultMaximumAttempts = 0
)
type RetryPolicy interface {
ComputeNextDelay(numAttempts int) time.Duration
}
type DefaultRetryPolicy struct {
// initialInterval 首次重启间隔,后续重启时间基于 backoffCoefficient
initialInterval time.Duration
// backoffCoefficient 重试补偿系数,值必须>=1, 默认为1
backoffCoefficient float64
// maximumInterval 最大间隔时间,默认为 initialInterval 的10倍
maximumInterval time.Duration
// maximumAttempts 最大尝试次数,0表示无限制
maximumAttempts int
}
func GetDefaultRetryPolicy() *DefaultRetryPolicy {
return &DefaultRetryPolicy{
initialInterval: defaultInitialInterval,
backoffCoefficient: defaultBackoffCoefficient,
maximumInterval: defaultMaximumIntervalCoefficient * defaultInitialInterval,
maximumAttempts: defaultMaximumAttempts,
}
}
func (d *DefaultRetryPolicy) SetInitialInterval(t time.Duration) *DefaultRetryPolicy {
d.initialInterval = t
d.maximumInterval = d.initialInterval * defaultMaximumIntervalCoefficient
return d
}
func (d *DefaultRetryPolicy) SetBackoffCoefficient(b float64) *DefaultRetryPolicy {
d.backoffCoefficient = b
return d
}
func (d *DefaultRetryPolicy) SetMaximumInterval(t time.Duration) *DefaultRetryPolicy {
d.maximumInterval = t
return d
}
func (d *DefaultRetryPolicy) SetMaximumAttempts(ma int) *DefaultRetryPolicy {
d.maximumAttempts = ma
return d
}
func (d *DefaultRetryPolicy) ComputeNextDelay(numAttempts int) time.Duration {
if d.maximumAttempts != 0 && d.maximumAttempts < numAttempts {
return RetryDone
}
nextInterval := float64(d.initialInterval) * math.Pow(d.backoffCoefficient, float64(numAttempts-1))
if nextInterval <= 0 {
return RetryDone
}
nextDuration := time.Duration(nextInterval)
if nextDuration < d.initialInterval {
return RetryDone
}
if d.maximumInterval < nextDuration {
nextDuration = d.maximumInterval
}
return nextDuration
}
type (
retrierImpl struct {
policy RetryPolicy
numAttempt int
}
Retrier interface {
NextBackOff() time.Duration
TryAgain() bool
Reset()
}
)
func newRetrier(policy RetryPolicy) Retrier {
return &retrierImpl{
policy: policy,
numAttempt: 1,
}
}
func (r *retrierImpl) Reset() {
r.numAttempt = 1
}
func (r *retrierImpl) TryAgain() bool {
return r.policy.ComputeNextDelay(r.numAttempt) != RetryDone
}
func (r *retrierImpl) NextBackOff() time.Duration {
nextInterval := r.policy.ComputeNextDelay(r.numAttempt)
r.numAttempt++
return nextInterval
}
func waitRetryBackoff(ctx context.Context, r Retrier) error {
waitTime := time.Duration(0)
if r != nil {
waitTime = r.NextBackOff()
}
if waitTime > 0 {
timer := time.NewTimer(waitTime)
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
} else {
return common.ErrAttemptExhausted
}
return nil
}