-
Notifications
You must be signed in to change notification settings - Fork 6
/
delay.go
99 lines (82 loc) · 1.92 KB
/
delay.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
package retry
import (
"math"
"time"
)
// Delay strategy has no limit. It implements a fixed
// wait time between retries.
type DelayStrategy struct {
Wait time.Duration
lastTime time.Time
}
func (s *DelayStrategy) Next() bool {
if !s.lastTime.IsZero() {
timeSince := TimeFunc().Sub(s.lastTime)
if timeSince < s.Wait {
SleepFunc(s.Wait - timeSince)
}
}
s.lastTime = TimeFunc()
return true
}
func (s *DelayStrategy) HasNext() bool {
return true
}
// Exponential backoff. No iteration limit, but it gets
// slower every time. Resettable.
type ExponentialBackoffStrategy struct {
InitialDelay time.Duration
MaxDelay time.Duration // default: no limit
count float64
lastTime time.Time
}
func (s *ExponentialBackoffStrategy) Next() bool {
if !s.lastTime.IsZero() {
// Calculate the next delay
nextDelay := time.Duration(math.Pow(s.count, 2)) * s.InitialDelay
if s.MaxDelay > 0 && nextDelay > s.MaxDelay {
nextDelay = s.MaxDelay
}
timeSince := TimeFunc().Sub(s.lastTime)
if timeSince < nextDelay {
SleepFunc(nextDelay - timeSince)
}
}
s.lastTime = TimeFunc()
s.count++
return true
}
func (s *ExponentialBackoffStrategy) HasNext() bool {
return true
}
func (s *ExponentialBackoffStrategy) Reset() {
var t time.Time
s.lastTime = t
s.count = 0
}
// Maximum time strategy. No iteration limit. Limit on max time.
// Timer starts automatically on first try. Resettable
type MaximumTimeStrategy struct {
Duration time.Duration
startTime time.Time
}
func (s *MaximumTimeStrategy) Next() bool {
if s.startTime.IsZero() {
s.startTime = TimeFunc()
return true
}
return s.HasNext()
}
func (s *MaximumTimeStrategy) elapsed() time.Duration {
if s.startTime.IsZero() {
return 0
}
return TimeFunc().Sub(s.startTime)
}
func (s *MaximumTimeStrategy) HasNext() bool {
return s.elapsed() < s.Duration
}
func (s *MaximumTimeStrategy) Reset() {
var t time.Time
s.startTime = t
}