-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleeper.go
118 lines (105 loc) · 2.95 KB
/
sleeper.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
package main
import (
"math/rand"
"time"
)
const (
DefaultMaxInterval = 15 * time.Minute
DefaultInitialInterval = 500 * time.Millisecond
DefaultRandomizationFactor = 0.3
DefaultMaxRandomization = 2 * time.Minute
DefaultUpMultiplier = 1.5
DefaultDownMultiplier = 0.9
DefaultDownMultiplierThreshold = 10
)
func NewAutoSleeper() *AutoSleeper {
return &AutoSleeper{
MaxInterval: DefaultMaxInterval,
InitialInterval: DefaultInitialInterval,
RandomizationFactor: DefaultRandomizationFactor,
MaxRandomization: DefaultMaxRandomization,
UpMultiplier: DefaultUpMultiplier,
DownMultiplier: DefaultDownMultiplier,
DownMultiplierThreshold: DefaultDownMultiplierThreshold,
}
}
type AutoSleeperMetrics struct {
TotalInvocation int
TotalWentUp int
TotalWentDown int
TotalSlept int
TotalSleepTime time.Duration
}
type AutoSleeper struct {
InitialInterval time.Duration
MaxInterval time.Duration
MaxRandomization time.Duration
UpMultiplier float64
DownMultiplier float64
RandomizationFactor float64
DownMultiplierThreshold int
metrics AutoSleeperMetrics
currentInterval time.Duration
currentSuccess int
}
func (s *AutoSleeper) GetMetrics() AutoSleeperMetrics {
return s.metrics
}
func (s *AutoSleeper) SleepOnFailure() {
s.metrics.TotalInvocation += 1
s.goUp()
s.sleep()
}
func (s *AutoSleeper) SleepOnSuccess() {
s.metrics.TotalInvocation += 1
if s.currentInterval == 0 {
return
}
s.currentSuccess += 1
if s.currentSuccess == s.DownMultiplierThreshold {
s.goDown()
s.currentSuccess = 0
}
s.sleep()
}
func (s *AutoSleeper) sleep() {
s.metrics.TotalSleepTime += s.currentInterval
s.metrics.TotalSlept += 1
time.Sleep(s.currentInterval)
}
func (s *AutoSleeper) goDown() {
s.metrics.TotalWentDown += 1
interval := float64(s.currentInterval) * s.DownMultiplier
random := getNextRandomInterval(interval, s.RandomizationFactor, float64(s.MaxRandomization))
if random < float64(s.InitialInterval) {
s.currentInterval = 0
return
}
s.currentInterval = time.Duration(random)
}
func (s *AutoSleeper) goUp() {
s.metrics.TotalWentUp += 1
if s.currentInterval == 0 {
s.currentInterval = s.InitialInterval
return
}
interval := float64(s.currentInterval) * s.UpMultiplier
random := getNextRandomInterval(interval, s.RandomizationFactor, float64(s.MaxRandomization))
if random > float64(s.MaxInterval) {
s.currentInterval = s.MaxInterval
return
}
s.currentInterval = time.Duration(random)
}
func getNextRandomInterval(currentInterval, randomizationFactor, maxRandomization float64) float64 {
if randomizationFactor == 0 {
return currentInterval
}
delta := randomizationFactor * currentInterval
if delta > maxRandomization {
delta = maxRandomization
}
randomization := 2 * delta * rand.Float64()
minInterval := currentInterval - delta
return minInterval + randomization
}