/
fromflags.go
249 lines (208 loc) · 6.65 KB
/
fromflags.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright 2018 Turbine Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package executor
//go:generate mockgen -source $GOFILE -destination mock_$GOFILE -package $GOPACKAGE --write_package_comment=false
import (
"log"
"runtime"
"time"
tbnflag "github.com/turbinelabs/nonstdlib/flag"
)
// DelayType represents an algorithm for computing retry delays.
type DelayType string
const (
// ConstantDelayType specifies a constant delay between
// retries.
ConstantDelayType DelayType = "constant"
// ExponentialDelayType specifies an exponentially increasing
// delay between retries.
ExponentialDelayType DelayType = "exponential"
flagDefaultDelayType = ExponentialDelayType
flagDefaultInitialDelay = 100 * time.Millisecond
flagDefaultMaxDelay = 30 * time.Second
flagDefaultMaxAttempts = 8
flagDefaultTimeout = 0 * time.Second
flagDefaultAttemptTimeout = 0 * time.Second
)
// FromFlags validates and constructs an Executor from command line
// flags.
type FromFlags interface {
// Returns the configured Executor. Multiple invocations
// return the same Executor even if the arguments
// change. DiagnosticsCallback may be nil.
Make(*log.Logger) Executor
}
// FromFlagsDefaults represents default values for Executor
// flags. Values are ignored if they are the zero value for their
// type.
type FromFlagsDefaults struct {
DelayType DelayType
InitialDelay time.Duration
MaxDelay time.Duration
MaxAttempts int
Parallelism int
Timeout time.Duration
AttemptTimeout time.Duration
}
// NewFromFlags constructs a FromFlags with application-agnostic
// default flag values. Most callers should use NewFromFlagsWithDefaults.
func NewFromFlags(f tbnflag.FlagSet) FromFlags {
return NewFromFlagsWithDefaults(f, FromFlagsDefaults{})
}
// NewFromFlagsWithDefaults constructs a FromFlags with
// application-provided default flag values.
func NewFromFlagsWithDefaults(
f tbnflag.FlagSet,
defaults FromFlagsDefaults,
) FromFlags {
delayTypeChoice :=
tbnflag.NewChoice(string(ConstantDelayType), string(ExponentialDelayType)).
WithDefault(string(defaults.DefaultDelayType()))
ff := &fromFlags{
delayType: delayTypeChoice,
}
f.Var(
&ff.delayType,
"delay-type",
"Specifies the retry delay type.",
)
f.DurationVar(
&ff.initialDelay,
"delay",
defaults.DefaultInitialDelay(),
"Specifies the initial delay for the exponential delay type. "+
"Specifies the delay for constant delay type.",
)
f.DurationVar(
&ff.maxDelay,
"max-delay",
defaults.DefaultMaxDelay(),
"Specifies the maximum delay for the exponential delay type. "+
"Ignored for the constant delay type.",
)
f.IntVar(
&ff.maxAttempts,
"max-attempts",
defaults.DefaultMaxAttempts(),
"Specifies the maximum number of attempts made, inclusive of the original attempt.",
)
f.IntVar(
&ff.parallelism,
"parallelism",
defaults.DefaultParallelism(),
"Specifies the maximum number of concurrent attempts running.",
)
f.DurationVar(
&ff.timeout,
"timeout",
defaults.DefaultTimeout(),
"Specifies the default timeout for actions. A timeout of 0 means no timeout.",
)
f.DurationVar(
&ff.attemptTimeout,
"attempt-timeout",
defaults.DefaultAttemptTimeout(),
"Specifies the default timeout for individual action attempts. A timeout of 0 "+
"means no timeout.",
)
return ff
}
// DefaultDelayType returns the default delay type. If not overridden
// the default delay type is ExponentialDelayType.
func (defaults FromFlagsDefaults) DefaultDelayType() DelayType {
if defaults.DelayType != DelayType("") {
return defaults.DelayType
}
return flagDefaultDelayType
}
// DefaultInitialDelay returns the default initial delay. If not
// overridden, the default initial delay is 100 milliseconds.
func (defaults FromFlagsDefaults) DefaultInitialDelay() time.Duration {
if defaults.InitialDelay != 0 {
return defaults.InitialDelay
}
return flagDefaultInitialDelay
}
// DefaultMaxDelay returns the default maximum delay. If not
// overridden, the default maximum delay is 30 seconds.
func (defaults FromFlagsDefaults) DefaultMaxDelay() time.Duration {
if defaults.MaxDelay != 0 {
return defaults.MaxDelay
}
return flagDefaultMaxDelay
}
// DefaultMaxAttempts returns the default maximum number of
// attempts. If not overridden, the default max attempts is 8.
func (defaults FromFlagsDefaults) DefaultMaxAttempts() int {
if defaults.MaxAttempts != 0 {
return defaults.MaxAttempts
}
return flagDefaultMaxAttempts
}
// DefaultParallelism returns the default parallelism. If not
// overridden, the default parallelism is 2 times the number of system
// CPU cores.
func (defaults FromFlagsDefaults) DefaultParallelism() int {
if defaults.Parallelism != 0 {
return defaults.Parallelism
}
return runtime.NumCPU() * 2
}
// DefaultTimeout returns the default global timeout. If not
// overridden, the default timeout is 0 (timeouts disabled).
func (defaults FromFlagsDefaults) DefaultTimeout() time.Duration {
if defaults.Timeout != 0 {
return defaults.Timeout
}
return flagDefaultTimeout
}
// DefaultAttemptTimeout returns the default per-attempt timeout. If
// not overridden, the default attempt timeout is 0 (attempt timeouts
// disabled).
func (defaults FromFlagsDefaults) DefaultAttemptTimeout() time.Duration {
if defaults.AttemptTimeout != 0 {
return defaults.AttemptTimeout
}
return flagDefaultAttemptTimeout
}
type fromFlags struct {
delayType tbnflag.Choice
initialDelay time.Duration
maxDelay time.Duration
maxAttempts int
parallelism int
timeout time.Duration
attemptTimeout time.Duration
executor Executor
}
func (ff *fromFlags) Make(log *log.Logger) Executor {
if ff.executor == nil {
var delayFunc DelayFunc
switch DelayType(ff.delayType.String()) {
case ExponentialDelayType:
delayFunc = NewExponentialDelayFunc(ff.initialDelay, ff.maxDelay)
case ConstantDelayType:
delayFunc = NewConstantDelayFunc(ff.initialDelay)
}
options := []Option{
WithRetryDelayFunc(delayFunc),
WithMaxAttempts(ff.maxAttempts),
WithParallelism(ff.parallelism),
WithTimeout(ff.timeout),
WithAttemptTimeout(ff.attemptTimeout),
WithLogger(log),
}
ff.executor = NewGoroutineExecutor(options...)
}
return ff.executor
}