-
Notifications
You must be signed in to change notification settings - Fork 30
/
backoff.go
49 lines (40 loc) · 830 Bytes
/
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
package appstore
import (
"math/rand"
"time"
)
type Backoff interface {
Pause() time.Duration
}
type OneSecondBackoff struct{}
func (bo *OneSecondBackoff) Pause() time.Duration {
return time.Duration(1) * time.Second
}
type JitterBackoff struct {
Initial time.Duration
Max time.Duration
Multiplier float64
cur time.Duration
}
func (bo *JitterBackoff) Pause() time.Duration {
if bo.Initial == 0 {
bo.Initial = time.Second
}
if bo.cur == 0 {
bo.cur = bo.Initial
}
if bo.Max == 0 {
bo.Max = 30 * time.Second
}
if bo.Multiplier < 1 {
bo.Multiplier = 2
}
// https://www.awsarchitectureblog.com/2015/03/backoff.html
d := time.Duration(1 + rand.Int63n(int64(bo.cur)))
bo.cur = time.Duration(float64(bo.cur) * bo.Multiplier)
// stop the backoff
if bo.cur > bo.Max {
d = -1
}
return d
}