-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_retrier.go
123 lines (114 loc) · 3.33 KB
/
backoff_retrier.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
package retry
import (
"context"
"math"
"time"
)
// Retrier is a function for retrying the given callback at max the given number of times.
// stop must be called to stop retrying.
type Retrier func(numTimes int, cb func(stop func()) error) error
// RetrierCtx is a function for retrying the given callback at max the given number of times.
// stop must be called to stop retrying.
type RetrierCtx func(ctx context.Context, numTimes int, cb func(stop func()) error) error
// BackOffRetrier retries a given callback, backing off on failure.
type BackOffRetrier struct {
initialDelay time.Duration
backOffCoefficient float64
}
// NewBackOffRetrier returns a new back off retrier.
func NewBackOffRetrier(initialDelay time.Duration, backOffCoefficient float64) *BackOffRetrier {
return &BackOffRetrier{initialDelay: initialDelay, backOffCoefficient: backOffCoefficient}
}
// Retry retries the given callback at max the given number of times.
// It stops as soon as a `nil` error is returned.
func (r *BackOffRetrier) Retry(numTimes int, cb func() error) error {
var sleepDur time.Duration
delay := r.initialDelay
return Retry(numTimes, func() error {
if sleepDur > 0 {
time.Sleep(sleepDur)
}
err := cb()
if err != nil {
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
}
// RetryCtx retries the given callback at max the given number of times.
// It stops as soon as a `nil` error is returned.
func (r *BackOffRetrier) RetryCtx(ctx context.Context, numTimes int, cb func() error) error {
var sleepDur time.Duration
delay := r.initialDelay
return Retry(numTimes, func() error {
err := ctx.Err()
if err != nil {
return err
}
if sleepDur > 0 {
time.Sleep(sleepDur)
}
err = cb()
if err != nil {
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
}
// RetryWithStop retries the given callback at max the given number of times.
// It stops only when `stop` is called.
func (r *BackOffRetrier) RetryWithStop(numTimes int, cb func(stop func()) error) error {
var sleepDur time.Duration
delay := r.initialDelay
return RetryWithStop(numTimes, func(stop func()) error {
if sleepDur > 0 {
time.Sleep(sleepDur)
}
err := cb(stop)
if err != nil {
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
}
// RetryWithStopCtx retries the given callback at max the given number of times.
// It stops only when `stop` is called.
func (r *BackOffRetrier) RetryWithStopCtx(ctx context.Context, numTimes int, cb func(stop func()) error) error {
var sleepDur time.Duration
delay := r.initialDelay
return RetryWithStop(numTimes, func(stop func()) error {
err := ctx.Err()
if err != nil {
stop()
return err
}
if sleepDur > 0 {
time.Sleep(sleepDur)
}
err = cb(stop)
if err != nil {
if sleepDur == 0 {
// First attempt. Don't multiply yet.
sleepDur = delay
} else {
sleepDur = time.Duration(math.Round(r.backOffCoefficient * float64(delay)))
}
}
return err
})
}