forked from rfyiamcool/backoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.go
120 lines (99 loc) · 1.99 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
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
package backoff
import (
"context"
"math"
"math/rand"
"time"
)
var (
defaultFactor float64 = 2
defaultJitter = false
defaultMinDelay = 100 * time.Millisecond
defaultMaxDelay = 2 * time.Second
)
type Backoff struct {
Attempts float64
Factor float64
//Jitter eases contention by randomizing backoff steps
Jitter bool
// Min and Max are the minimum and maximum values of the backoff control
MinDelay time.Duration
MaxDelay time.Duration
}
type BackoffOption func(*Backoff)
func WithMinDelay(d time.Duration) BackoffOption {
return func(b *Backoff) {
b.MinDelay = d
}
}
func WithMaxDelay(d time.Duration) BackoffOption {
return func(b *Backoff) {
b.MaxDelay = d
}
}
func WithJitterFlag(f bool) BackoffOption {
return func(b *Backoff) {
b.Jitter = f
}
}
func WithFactor(v float64) BackoffOption {
return func(b *Backoff) {
b.Factor = v
}
}
func NewBackOff(opts ...BackoffOption) *Backoff {
var (
bo = &Backoff{
Attempts: 0,
Factor: defaultFactor,
Jitter: defaultJitter,
MinDelay: defaultMinDelay,
MaxDelay: defaultMaxDelay,
}
)
for _, option := range opts {
option(bo)
}
return bo
}
func (b *Backoff) beRevise() {
if b.MinDelay == 0 {
b.MinDelay = defaultMinDelay
}
if b.MaxDelay == 0 {
b.MaxDelay = defaultMaxDelay
}
if b.Factor == 0 {
b.Factor = defaultFactor
}
}
func (b *Backoff) Duration() time.Duration {
dur := float64(b.MinDelay) * math.Pow(b.Factor, b.Attempts)
if b.Jitter == true {
dur = rand.Float64()*(dur-float64(b.MinDelay)) + float64(b.MinDelay)
}
if dur > float64(b.MaxDelay) {
return b.MaxDelay
}
b.Attempts++
return time.Duration(dur)
}
// Sleep
func (b *Backoff) Sleep() {
time.Sleep(b.Duration())
}
// SleepCtx
func (b *Backoff) SleepCtx(ctx context.Context) {
var timer = time.NewTimer(b.Duration())
select {
case <-timer.C:
return
case <-ctx.Done():
timer.Stop()
return
}
}
//Resets the current value of the counter back to Min
func (b *Backoff) Reset() {
b.Attempts = 0
}