forked from hashicorp/consul-template
/
retry.go
170 lines (133 loc) · 3.68 KB
/
retry.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package config
import (
"fmt"
"math"
"time"
)
const (
// DefaultRetryAttempts is the default number of maximum retry attempts.
DefaultRetryAttempts = 12
// DefaultRetryBackoff is the default base for the exponential backoff
// algorithm.
DefaultRetryBackoff = 250 * time.Millisecond
// DefaultRetryMaxBackoff is the default maximum of backoff time
DefaultRetryMaxBackoff = 1 * time.Minute
)
// RetryFunc is the signature of a function that supports retries.
type RetryFunc func(int) (bool, time.Duration)
// RetryConfig is a shared configuration for upstreams that support retires on
// failure.
type RetryConfig struct {
// Attempts is the total number of maximum attempts to retry before letting
// the error fall through.
// 0 means unlimited.
Attempts *int
// Backoff is the base of the exponentialbackoff. This number will be
// multiplied by the next power of 2 on each iteration.
Backoff *time.Duration
// MaxBackoff is an upper limit to the sleep time between retries
// A MaxBackoff of zero means there is no limit to the exponential growth of the backoff.
MaxBackoff *time.Duration `mapstructure:"max_backoff"`
// Enabled signals if this retry is enabled.
Enabled *bool
}
// DefaultRetryConfig returns a configuration that is populated with the
// default values.
func DefaultRetryConfig() *RetryConfig {
return &RetryConfig{}
}
// Copy returns a deep copy of this configuration.
func (c *RetryConfig) Copy() *RetryConfig {
if c == nil {
return nil
}
var o RetryConfig
o.Attempts = c.Attempts
o.Backoff = c.Backoff
o.MaxBackoff = c.MaxBackoff
o.Enabled = c.Enabled
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *RetryConfig) Merge(o *RetryConfig) *RetryConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Attempts != nil {
r.Attempts = o.Attempts
}
if o.Backoff != nil {
r.Backoff = o.Backoff
}
if o.MaxBackoff != nil {
r.MaxBackoff = o.MaxBackoff
}
if o.Enabled != nil {
r.Enabled = o.Enabled
}
return r
}
// RetryFunc returns the retry function associated with this configuration.
func (c *RetryConfig) RetryFunc() RetryFunc {
return func(retry int) (bool, time.Duration) {
if !BoolVal(c.Enabled) {
return false, 0
}
if IntVal(c.Attempts) > 0 && retry > IntVal(c.Attempts)-1 {
return false, 0
}
baseSleep := TimeDurationVal(c.Backoff)
maxSleep := TimeDurationVal(c.MaxBackoff)
if maxSleep > 0 {
attemptsTillMaxBackoff := int(math.Log2(maxSleep.Seconds() / baseSleep.Seconds()))
if retry > attemptsTillMaxBackoff {
return true, maxSleep
}
}
base := math.Pow(2, float64(retry))
sleep := time.Duration(base) * baseSleep
return true, sleep
}
}
// Finalize ensures there no nil pointers.
func (c *RetryConfig) Finalize() {
if c.Attempts == nil {
c.Attempts = Int(DefaultRetryAttempts)
}
if c.Backoff == nil {
c.Backoff = TimeDuration(DefaultRetryBackoff)
}
if c.MaxBackoff == nil {
c.MaxBackoff = TimeDuration(DefaultRetryMaxBackoff)
}
if c.Enabled == nil {
c.Enabled = Bool(true)
}
}
// GoString defines the printable version of this struct.
func (c *RetryConfig) GoString() string {
if c == nil {
return "(*RetryConfig)(nil)"
}
return fmt.Sprintf("&RetryConfig{"+
"Attempts:%s, "+
"Backoff:%s, "+
"MaxBackoff:%s, "+
"Enabled:%s"+
"}",
IntGoString(c.Attempts),
TimeDurationGoString(c.Backoff),
TimeDurationGoString(c.MaxBackoff),
BoolGoString(c.Enabled),
)
}