-
Notifications
You must be signed in to change notification settings - Fork 2
More flexible refactor. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
retry.go
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we errors.Cause
these?
retry.go
Outdated
if !cond(err) { | ||
return err | ||
func (r *Retry) postCheck(err error) bool { | ||
if err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. By default though, with no postConditions we should still continue until err == nil
, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such a strange caller is strange enough to also declare an errContinue
type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, when this is merged we should bump semver.
@nytopop plz review Jitter method. |
retry.go
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's working in the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops nvm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.