-
Notifications
You must be signed in to change notification settings - Fork 1
/
retry.go
185 lines (152 loc) · 4.73 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package retry
import (
"fmt"
"log"
"reflect"
"sync/atomic"
"time"
"github.com/cenkalti/backoff"
)
type BackOffOpts struct {
InitialInterval time.Duration
MaxInterval time.Duration
MaxElapsedTime time.Duration
}
var DefaultBackOffOpts *BackOffOpts = &BackOffOpts{
InitialInterval: 500 * time.Millisecond,
MaxInterval: 3 * time.Second,
MaxElapsedTime: 10 * time.Second}
var RetryOnAnyError = func(error) bool { return true }
type RetryNotifier func(*RetryEvent)
type Retrier struct {
Name string
backOffOpts *BackOffOpts
shouldRetryFunc func(error) bool
notifyRetryFuncs []RetryNotifier
notifyGaveUpFuncs []RetryNotifier
notifyShouldNotRetryFuncs []RetryNotifier
}
var retrierNum uint32 = 0
func newName(name string) string {
return fmt.Sprintf("%s%d", name, atomic.AddUint32(&retrierNum, 1))
}
func NewRetrier(name string,
backOffOpts *BackOffOpts, shouldRetryFunc func(error) bool) *Retrier {
return &Retrier{
Name: newName(name),
backOffOpts: backOffOpts,
shouldRetryFunc: shouldRetryFunc,
notifyRetryFuncs: []RetryNotifier{logRetry},
notifyGaveUpFuncs: []RetryNotifier{logGaveUp},
notifyShouldNotRetryFuncs: []RetryNotifier{logShouldNotRetry}}
}
// Retrier without logging functions included as defaults for notify funcs
func New(name string,
backOffOpts *BackOffOpts, shouldRetryFunc func(error) bool) *Retrier {
return &Retrier{
Name: newName(name),
backOffOpts: backOffOpts,
shouldRetryFunc: shouldRetryFunc,
notifyRetryFuncs: []RetryNotifier{},
notifyGaveUpFuncs: []RetryNotifier{},
notifyShouldNotRetryFuncs: []RetryNotifier{}}
}
func NewErrorTypeRetrier(name string,
backOffOpts *BackOffOpts, errorTypes ...interface{}) *Retrier {
return &Retrier{
Name: name,
backOffOpts: backOffOpts,
shouldRetryFunc: RetryWhenErrorTypeMatches(instancesToTypes(errorTypes))}
}
func (r *Retrier) Retry(f func() (interface{}, error)) (interface{}, error) {
var val interface{}
var err error
var next time.Duration
numTries := 0
b := r.newBackOff()
b.Reset()
for {
numTries++
if val, err = f(); err == nil {
return val, nil
}
if !r.shouldRetryFunc(err) {
r.notifyShouldNotRetry(err, numTries)
return val, err
}
if next = b.NextBackOff(); next == backoff.Stop {
r.notifyGaveUp(err, numTries)
return val, err
}
time.Sleep(next)
r.notifyRetry(err, numTries)
}
}
type RetryEvent struct {
Retrier *Retrier
Err error
NumTries int
}
func (r *Retrier) AddNotifyRetry(f RetryNotifier) {
r.notifyRetryFuncs = append(r.notifyRetryFuncs, f)
}
func (r *Retrier) AddNotifyGaveUp(f RetryNotifier) {
r.notifyGaveUpFuncs = append(r.notifyGaveUpFuncs, f)
}
func (r *Retrier) notifyShouldNotRetry(err error, numTries int) {
retryEvent := &RetryEvent{Retrier: r, Err: err, NumTries: numTries}
for _, notifyNoRetryFunc := range r.notifyShouldNotRetryFuncs {
notifyNoRetryFunc(retryEvent)
}
}
func (r *Retrier) notifyGaveUp(err error, numTries int) {
retryEvent := &RetryEvent{Retrier: r, Err: err, NumTries: numTries}
for _, notifyGaveUpFunc := range r.notifyGaveUpFuncs {
notifyGaveUpFunc(retryEvent)
}
}
func (r *Retrier) notifyRetry(err error, numTries int) {
retryEvent := &RetryEvent{Retrier: r, Err: err, NumTries: numTries}
for _, notifyRetryFunc := range r.notifyRetryFuncs {
notifyRetryFunc(retryEvent)
}
}
func logShouldNotRetry(re *RetryEvent) {
log.Printf("Error not qualified for retry: error=%s count#retry.%s.no_retry=1\n",
re.Err.Error(), re.Retrier.Name)
}
func logRetry(re *RetryEvent) {
log.Printf("Retrying after %d tries: error=%s count#retry.%s.retry_count=1\n",
re.NumTries, re.Err.Error(), re.Retrier.Name)
}
func logGaveUp(re *RetryEvent) {
log.Printf("Giving up after %d tries: error=%s count#retry.%s.gave_up_count=1\n",
re.NumTries, re.Err.Error(), re.Retrier.Name)
}
func RetryWhenErrorTypeMatches(errorTypes []reflect.Type) func(error) bool {
errorTypeSet := make(map[reflect.Type]bool)
for _, t := range errorTypes {
errorTypeSet[t] = true
}
return func(e error) bool {
errorType := reflect.TypeOf(e)
return errorTypeSet[errorType] == true
}
}
func (r *Retrier) SetBackOffOpts(b *BackOffOpts) {
r.backOffOpts = b
}
func (r *Retrier) newBackOff() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = r.backOffOpts.InitialInterval
b.MaxInterval = r.backOffOpts.MaxInterval
b.MaxElapsedTime = r.backOffOpts.MaxElapsedTime
return b
}
func instancesToTypes(instances []interface{}) []reflect.Type {
types := []reflect.Type{}
for _, instance := range instances {
types = append(types, reflect.TypeOf(instance))
}
return types
}