forked from cenkalti/backoff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear.go
44 lines (37 loc) · 1.04 KB
/
linear.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
package backoff
// LinearBackOff is a back-off policy whose back-off time is multiplied by mult and incremented by incr
// each time it is called.
// mult can be one ;).
import "time"
// grows linearly until
type LinearBackOff struct {
InitialInterval time.Duration
Multiplier float64
Increment time.Duration
MaxInterval time.Duration
currentInterval time.Duration
}
var _ Interface = (*LinearBackOff)(nil)
func NewLinear(from, to, incr time.Duration, mult float64) *LinearBackOff {
return &LinearBackOff{
InitialInterval: from,
MaxInterval: to,
currentInterval: from,
Increment: incr,
Multiplier: mult,
}
}
func (lb *LinearBackOff) Reset() {
lb.currentInterval = lb.InitialInterval
}
func (lb *LinearBackOff) increment() {
lb.currentInterval = time.Duration(float64(lb.currentInterval) * lb.Multiplier)
lb.currentInterval += lb.Increment
if lb.currentInterval > lb.MaxInterval {
lb.currentInterval = lb.MaxInterval
}
}
func (lb *LinearBackOff) BackOff() {
time.Sleep(lb.currentInterval)
lb.increment()
}