-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrategy.go
113 lines (93 loc) · 2.66 KB
/
strategy.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
package repeater
import (
"math/rand"
"time"
)
// Strategy defines how to calculate delays between retries
type Strategy interface {
// NextDelay returns delay for the next attempt, attempt starts from 1
NextDelay(attempt int) time.Duration
}
// FixedDelay implements fixed time delay between attempts
type FixedDelay struct {
Delay time.Duration
}
// NewFixedDelay creates a new FixedDelay strategy
func NewFixedDelay(delay time.Duration) FixedDelay {
return FixedDelay{Delay: delay}
}
// NextDelay returns fixed delay
func (s FixedDelay) NextDelay(_ int) time.Duration {
return s.Delay
}
// BackoffType represents the backoff strategy type
type BackoffType int
const (
// BackoffConstant keeps delays the same between attempts
BackoffConstant BackoffType = iota
// BackoffLinear increases delays linearly between attempts
BackoffLinear
// BackoffExponential increases delays exponentially between attempts
BackoffExponential
)
// backoff implements various backoff strategies with optional jitter
type backoff struct {
initial time.Duration
maxDelay time.Duration
btype BackoffType
jitter float64
}
type backoffOption func(*backoff)
// WithMaxDelay sets maximum delay for the backoff strategy
func WithMaxDelay(d time.Duration) backoffOption { //nolint:revive // unexported type is used in the same package
return func(b *backoff) {
b.maxDelay = d
}
}
// WithBackoffType sets backoff type for the strategy
func WithBackoffType(t BackoffType) backoffOption { //nolint:revive // unexported type is used in the same package
return func(b *backoff) {
b.btype = t
}
}
// WithJitter sets jitter factor for the backoff strategy
func WithJitter(factor float64) backoffOption { //nolint:revive // unexported type is used in the same package
return func(b *backoff) {
b.jitter = factor
}
}
func newBackoff(initial time.Duration, opts ...backoffOption) *backoff {
b := &backoff{
initial: initial,
maxDelay: 30 * time.Second,
btype: BackoffExponential,
jitter: 0.1,
}
for _, opt := range opts {
opt(b)
}
return b
}
// NextDelay returns delay for the next attempt
func (s backoff) NextDelay(attempt int) time.Duration {
if attempt <= 0 {
return 0
}
var delay time.Duration
switch s.btype {
case BackoffConstant:
delay = s.initial
case BackoffLinear:
delay = s.initial * time.Duration(attempt)
case BackoffExponential:
delay = s.initial * time.Duration(1<<(attempt-1))
}
if s.maxDelay > 0 && delay > s.maxDelay {
delay = s.maxDelay
}
if s.jitter > 0 {
jitter := float64(delay) * s.jitter
delay = time.Duration(float64(delay) + (rand.Float64()*jitter - jitter/2)) //nolint:gosec // no need for secure random here
}
return delay
}