-
Notifications
You must be signed in to change notification settings - Fork 146
feat: add dynamic backoff #54
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package retrier | ||
|
|
||
| import "time" | ||
|
|
||
| type errWithBackoff struct { | ||
| err error | ||
| backoff time.Duration | ||
| } | ||
|
|
||
| func ErrWithBackoff(err error, backoff time.Duration) error { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add godocs for any new public types/methods/etc. |
||
| return &errWithBackoff{ | ||
| err: err, | ||
| backoff: backoff, | ||
| } | ||
| } | ||
|
|
||
| func (e *errWithBackoff) Error() string { | ||
| return e.err.Error() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package retrier | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "math/rand" | ||
| "sync" | ||
| "time" | ||
|
|
@@ -89,7 +90,15 @@ func (r *Retrier) RunFn(ctx context.Context, work func(ctx context.Context, retr | |
| return ret | ||
| } | ||
|
|
||
| timer := time.NewTimer(r.calcSleep(retries)) | ||
| var err *errWithBackoff | ||
| var backoff time.Duration | ||
| if errors.As(ret, &err) { | ||
| backoff = err.backoff | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the response gives us a backoff value, does it still make sense to add some jitter to that? I'm not honestly sure...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with eapache's point. |
||
| } else { | ||
| backoff = r.calcSleep(retries) | ||
| } | ||
|
|
||
| timer := time.NewTimer(backoff) | ||
| if err := r.sleep(ctx, timer); err != nil { | ||
| if r.surfaceWorkErrors { | ||
| return ret | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,24 @@ func TestRetrierRunFnWithInfinite(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestRetrierWithDynamicBackoff(t *testing.T) { | ||
| r := New([]time.Duration{0, 10 * time.Millisecond}, nil) | ||
| st := time.Now() | ||
|
|
||
| err := r.Run(genWork([]error{ErrWithBackoff(errFoo, 500*time.Millisecond)})) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer to swap these times (so the retrier defaults to 500ms and the error overrides it to 10ms), so the test runs that much faster. |
||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
| if i != 2 { | ||
| t.Error("run wrong number of times") | ||
| } | ||
|
|
||
| if time.Since(st) < 500*time.Millisecond { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tried to avoid doing time-based assertions in the test suite, they tend to be quite flaky.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the test results may be flaky, but I couldn't think of any other way to check 🤔
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, I don't assert on how long it actually sleeps (I trust go's timer to work) and I simply unit-test the return value of Perhaps for this PR you could pass |
||
| t.Error("not wait dynamic backoff") | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.