Skip to content
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

Dealing with a canceled Context #8

Merged
merged 4 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (e *retryableError) Error() string {
// Do wraps a function with a backoff to retry. The provided context is the same
// context passed to the RetryFunc.
func Do(ctx context.Context, b Backoff, f RetryFunc) error {
// If ctx is already canceled, then return immediately without calling f
select {
case <-ctx.Done():
return ctx.Err()
default:
}

for {
err := f(ctx)
if err == nil {
Expand All @@ -66,6 +73,13 @@ func Do(ctx context.Context, b Backoff, f RetryFunc) error {
return rerr.Unwrap()
}

// ctx.Done() has priority, so we test it alone first
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only Go had a priority for select...

select {
case <-ctx.Done():
return ctx.Err()
default:
}

select {
case <-ctx.Done():
return ctx.Err()
Expand Down
32 changes: 32 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package retry_test

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -173,3 +174,34 @@ func ExampleDo_customRetry() {
// handle error
}
}

func TestCancel(t *testing.T) {
for i := 0; i < 100000; i++ {
ctx, cancel := context.WithCancel(context.Background())

calls := 0
rf := func(ctx context.Context) error {
calls++
// Never succeed.
// Always return a RetryableError
return retry.RetryableError(errors.New("nope"))
}

const delay time.Duration = time.Millisecond
b := retry.NewConstant(delay)

const maxRetries = 5
b = retry.WithMaxRetries(maxRetries, b)

const jitter time.Duration = 5 * time.Millisecond
b = retry.WithJitter(jitter, b)

// Here we cancel the Context *before* the call to Do
cancel()
retry.Do(ctx, b, rf)

if calls > 1 {
t.Errorf("rf was called %d times instead of 0 or 1", calls)
}
}
}