-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategies.go
92 lines (78 loc) · 1.67 KB
/
strategies.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
package repeater
import (
"context"
"math"
"math/rand"
"time"
"go.pr0ger.dev/x/cryptosource"
)
type ExponentialBackoff struct {
InitialInterval time.Duration
MaxElapsedTime time.Duration
Multiplier float64
RandomizationFactor float64
}
func (s ExponentialBackoff) Start(ctx context.Context) <-chan struct{} {
timeout := time.After(s.MaxElapsedTime)
ch := make(chan struct{})
go func() {
defer close(ch)
for count := 0; ; count++ {
select {
case <-ctx.Done():
return
case <-timeout:
return
case ch <- struct{}{}:
}
random := 1 - (rand.New(cryptosource.NewSource()).Float64()*2*s.RandomizationFactor - s.RandomizationFactor)
time.Sleep(s.InitialInterval * time.Duration(math.Pow(s.Multiplier, float64(count))*random))
}
}()
return ch
}
type fixedDelay struct {
Repeats int
Delay time.Duration
}
func (s fixedDelay) Start(ctx context.Context) <-chan struct{} {
ch := make(chan struct{})
go func() {
defer close(ch)
for i := 0; i < s.Repeats; i++ {
select {
case <-ctx.Done():
return
case ch <- struct{}{}:
}
time.Sleep(s.Delay)
}
}()
return ch
}
type once struct{}
func (once) Start(context.Context) <-chan struct{} {
ch := make(chan struct{})
go func() {
ch <- struct{}{}
close(ch)
}()
return ch
}
func DefaultExponentialBackoff() Strategy {
return &ExponentialBackoff{
InitialInterval: 500 * time.Millisecond,
MaxElapsedTime: 15 * time.Minute,
Multiplier: 1.5,
RandomizationFactor: 0.5,
}
}
func FixedDelay(repeats int, delay time.Duration) Strategy {
return &fixedDelay{
Repeats: repeats,
Delay: delay,
}
}
func Once() Strategy {
return &once{}
}