-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackoff.go
51 lines (44 loc) · 1.12 KB
/
backoff.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
package chainload
import (
"context"
"math/rand"
"time"
metrics "github.com/rcrowley/go-metrics"
"go.uber.org/zap"
)
type backOff struct {
wait, maxWait time.Duration
lgr *zap.Logger
}
func (b *backOff) do(ctx context.Context, fn func() error) bool {
return b.doTimed(ctx, metrics.NilTimer{}, fn)
}
func (b *backOff) doTimed(ctx context.Context, timer metrics.Timer, fn func() error) bool {
wait := b.wait
t := time.Now()
err := fn()
for errs := 0; err != nil; errs++ {
if ctx.Err() != nil {
return false
}
if wait = randBetweenDur(3/2*wait, 5/2*wait); wait > b.maxWait {
wait = b.maxWait
}
b.lgr.Warn("Operation failed - pausing", zap.Duration("wait", wait), zap.Int("attempt", errs), zap.Error(err))
select {
case <-time.After(wait):
t = time.Now()
err = fn()
case <-ctx.Done():
return false
}
}
timer.UpdateSince(t)
return true
}
func randBetweenDur(start, end time.Duration) time.Duration {
return (start + time.Duration(rand.Int63n(int64(end-start)))).Round(time.Second)
}
func randBetween(start, end uint64) uint64 {
return start + uint64(rand.Int63n(int64(end-start)))
}