This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_gen.go
285 lines (242 loc) · 8.9 KB
/
parser_gen.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"errors"
"flag"
"fmt"
"io"
"math/rand"
"strconv"
"strings"
"time"
"github.com/kamilsk/retry/v4/backoff"
"github.com/kamilsk/retry/v4/jitter"
"github.com/kamilsk/retry/v4/strategy"
"go.octolab.org/unsafe"
)
func init() {
var fLimit, fDelay, fWait, fBackoff, fTBackoff string
compliance = map[string]struct {
cursor interface{}
usage string
handler func(*flag.Flag) (strategy.Strategy, error)
}{
"limit": {cursor: &fLimit,
handler: generatedLimitStrategy},
"delay": {cursor: &fDelay,
handler: generatedDelayStrategy},
"wait": {cursor: &fWait,
handler: generatedWaitStrategy},
"backoff": {cursor: &fBackoff,
handler: generatedBackoffStrategy},
"tbackoff": {cursor: &fTBackoff,
handler: generatedBackoffWithJitterStrategy},
}
algorithms = map[string]func(args string) (backoff.Algorithm, error){
"inc": generatedIncrementalAlgorithm,
"lin": generatedLinearAlgorithm,
"exp": generatedExponentialAlgorithm,
"binexp": generatedBinaryExponentialAlgorithm,
"fib": generatedFibonacciAlgorithm,
}
transforms = map[string]func(args string) (jitter.Transformation, error){
"full": generatedFullTransformation,
"equal": generatedEqualTransformation,
"dev": generatedDeviationTransformation,
"ndist": generatedNormalDistributionTransformation,
}
usage = func(output io.Writer, md Metadata) func() {
return func() {
unsafe.DoSilent(fmt.Fprintf(output, `
Usage: %s [-timeout Timeout] [--debug] [--notify] [strategy flags] -- command
The strategy flags
-limit=X
Limit creates a Strategy that limits the number of attempts that Retry will
make.
-delay=Xs
Delay creates a Strategy that waits the given duration before the first
attempt is made.
-wait=Xs,...
Wait creates a Strategy that waits the given durations for each attempt after
the first. If the number of attempts is greater than the number of durations
provided, then the strategy uses the last duration provided.
-backoff=:algorithm
Backoff creates a Strategy that waits before each attempt, with a duration as
defined by the given backoff.Algorithm.
-tbackoff=":algorithm :transformation"
BackoffWithJitter creates a Strategy that waits before each attempt, with a
duration as defined by the given backoff.Algorithm and jitter.Transformation.
:algorithm
inc:Xs,Ys
Incremental creates a Algorithm that increments the initial duration
by the given increment for each attempt.
lin:Xs
Linear creates a Algorithm that linearly multiplies the factor
duration by the attempt number for each attempt.
exp:Xs,Y
Exponential creates a Algorithm that multiplies the factor duration by
an exponentially increasing factor for each attempt, where the factor is
calculated as the given base raised to the attempt number.
binexp:Xs
BinaryExponential creates a Algorithm that multiplies the factor
duration by an exponentially increasing factor for each attempt, where the
factor is calculated as "2" raised to the attempt number (2^attempt).
fib:Xs
Fibonacci creates a Algorithm that multiplies the factor duration by
an increasing factor for each attempt, where the factor is the Nth number in
the Fibonacci sequence.
:transformation
full
Full creates a Transformation that transforms a duration into a result
duration in [0, n) randomly, where n is the given duration.
The given generator is what is used to determine the random transformation.
If a nil generator is passed, a default one will be provided.
Inspired by https://www.awsarchitectureblog.com/2015/03/backoff.html
equal
Equal creates a Transformation that transforms a duration into a result
duration in [n/2, n) randomly, where n is the given duration.
The given generator is what is used to determine the random transformation.
If a nil generator is passed, a default one will be provided.
Inspired by https://www.awsarchitectureblog.com/2015/03/backoff.html
dev:X
Deviation creates a Transformation that transforms a duration into a result
duration that deviates from the input randomly by a given factor.
The given generator is what is used to determine the random transformation.
If a nil generator is passed, a default one will be provided.
Inspired by https://developers.google.com/api-client-library/java/google-http-java-client/backoff
ndist:X
NormalDistribution creates a Transformation that transforms a duration into a
result duration based on a normal distribution of the input and the given
standard deviation.
The given generator is what is used to determine the random transformation.
If a nil generator is passed, a default one will be provided.
Examples:
%[1]s -limit=3 -backoff=lin:10ms -- curl http://example.com
%[1]s -tbackoff="lin:10s full" -- curl https://example.com
%[1]s -timeout=500ms --notify --infinite -- git pull
Version %s (commit: %s, build date: %s, go version: %s, compiler: %s, platform: %s)
`, md.BinName, md.Version, md.Commit, md.BuildDate, md.GoVersion, md.Compiler, md.Platform))
}
}
}
// strategies
func generatedLimitStrategy(f *flag.Flag) (strategy.Strategy, error) {
attemptLimit, err := strconv.ParseUint(f.Value.String(), 10, 0)
if err != nil {
return nil, err
}
return strategy.Limit(uint(attemptLimit)), nil
}
func generatedDelayStrategy(f *flag.Flag) (strategy.Strategy, error) {
duration, err := time.ParseDuration(f.Value.String())
if err != nil {
return nil, err
}
return strategy.Delay(duration), nil
}
func generatedWaitStrategy(f *flag.Flag) (strategy.Strategy, error) {
args := strings.Split(f.Value.String(), ",")
durations := make([]time.Duration, 0, len(args))
for _, arg := range args {
duration, err := time.ParseDuration(arg)
if err != nil {
return nil, err
}
durations = append(durations, duration)
}
return strategy.Wait(durations...), nil
}
func generatedBackoffStrategy(f *flag.Flag) (strategy.Strategy, error) {
algorithm, err := parseAlgorithm(f.Value.String())
if err != nil {
return nil, err
}
return strategy.Backoff(algorithm), nil
}
func generatedBackoffWithJitterStrategy(f *flag.Flag) (strategy.Strategy, error) {
args := strings.Split(f.Value.String(), " ")
if len(args) != 2 {
return nil, errors.New("invalid argument count")
}
algorithm, err := parseAlgorithm(args[0])
if err != nil {
return nil, err
}
transform, err := parseTransform(args[1])
if err != nil {
return nil, err
}
return strategy.BackoffWithJitter(algorithm, transform), nil
}
// algorithms
func generatedIncrementalAlgorithm(raw string) (backoff.Algorithm, error) {
args := strings.Split(raw, ",")
if len(args) != 2 {
return nil, errors.New("invalid argument count")
}
initial, err := time.ParseDuration(args[0])
if err != nil {
return nil, err
}
increment, err := time.ParseDuration(args[1])
if err != nil {
return nil, err
}
return backoff.Incremental(initial, increment), nil
}
func generatedLinearAlgorithm(raw string) (backoff.Algorithm, error) {
factor, err := time.ParseDuration(raw)
if err != nil {
return nil, err
}
return backoff.Linear(factor), nil
}
func generatedExponentialAlgorithm(raw string) (backoff.Algorithm, error) {
args := strings.Split(raw, ",")
if len(args) != 2 {
return nil, errors.New("invalid argument count")
}
factor, err := time.ParseDuration(args[0])
if err != nil {
return nil, err
}
base, err := strconv.ParseFloat(args[1], 0)
if err != nil {
return nil, err
}
return backoff.Exponential(factor, base), nil
}
func generatedBinaryExponentialAlgorithm(raw string) (backoff.Algorithm, error) {
factor, err := time.ParseDuration(raw)
if err != nil {
return nil, err
}
return backoff.BinaryExponential(factor), nil
}
func generatedFibonacciAlgorithm(raw string) (backoff.Algorithm, error) {
factor, err := time.ParseDuration(raw)
if err != nil {
return nil, err
}
return backoff.Fibonacci(factor), nil
}
// transforms
func generatedFullTransformation(_ string) (jitter.Transformation, error) {
return jitter.Full(rand.New(rand.NewSource(time.Now().UnixNano()))), nil
}
func generatedEqualTransformation(_ string) (jitter.Transformation, error) {
return jitter.Equal(rand.New(rand.NewSource(time.Now().UnixNano()))), nil
}
func generatedDeviationTransformation(raw string) (jitter.Transformation, error) {
factor, err := strconv.ParseFloat(raw, 0)
if err != nil {
return nil, err
}
return jitter.Deviation(rand.New(rand.NewSource(time.Now().UnixNano())), factor), nil
}
func generatedNormalDistributionTransformation(raw string) (jitter.Transformation, error) {
standardDeviation, err := strconv.ParseFloat(raw, 0)
if err != nil {
return nil, err
}
return jitter.NormalDistribution(rand.New(rand.NewSource(time.Now().UnixNano())), standardDeviation), nil
}