More flexible refactor. - #6
Conversation
| func OnErrors(errs ...error) func(err error) bool { | ||
| return func(err error) bool { | ||
| for _, checkErr := range errs { | ||
| if err == checkErr { |
There was a problem hiding this comment.
should we errors.Cause these?
| if !cond(err) { | ||
| return err | ||
| func (r *Retry) postCheck(err error) bool { | ||
| if err == nil { |
There was a problem hiding this comment.
What do you think about removing this err check, or only doing it if len(r.postConditions) == 0? If we did that then we can use retry to hammer something until it fails hard (in tests?). OnErrors would filter out anything not explicitly permitted in any case, if supplied as a condition.
There was a problem hiding this comment.
So you're saying we want to support a case where a retry continues if the error is nil?
I will definitely turn this check into a proper postCondition.
There was a problem hiding this comment.
Yep. By default though, with no postConditions we should still continue until err == nil, I think.
There was a problem hiding this comment.
Such a strange caller is strange enough to also declare an errContinue type
nytopop
left a comment
There was a problem hiding this comment.
LGTM, when this is merged we should bump semver.
|
@nytopop plz review Jitter method. |
| // Jitter adds some random jitter to the retry's sleep. | ||
| // | ||
| // It will multiply the sleep by a random between min and max. | ||
| func (r *Retry) Jitter(min, max float64) *Retry { |
There was a problem hiding this comment.
We could use a single relative percent/ratio here for simplicity. Then something like rand.Float64() * ratio and randomly add or remove that multiplier from the current timeout.
someretry.Jitter(0.25) // adds +/- 0-25% jitter| underlyingSleep := r.sleepDur | ||
| r.sleepDur = func() time.Duration { | ||
| dur := underlyingSleep() | ||
|
|
There was a problem hiding this comment.
doesn't seem to work :\
This should, I believe
mul := rnd.Float64() * rat
if rnd.Intn(2) == 1 {
mul *= (-1)
}
dur = time.Duration(float64(dur) + (float64(dur) * mul))There was a problem hiding this comment.
It's working in the tests?
There was a problem hiding this comment.
Inserting fmt printlns shows that the jitter is always identical
ratio:
original:
jittered:
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
0.9999
1ms
999.999µs
No description provided.