Skip to content
Open
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
6 changes: 5 additions & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
return emptyT, err
}

if errors.Is(err, context.Cause(config.context)) {
return emptyT, err
}

lastErr = err

config.onRetry(n, err)
Expand Down Expand Up @@ -184,7 +188,7 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (

errorLog = append(errorLog, unpackUnrecoverable(err))

if !config.retryIf(err) {
if !config.retryIf(err) || errors.Is(err, context.Cause(config.context)) {
break
}

Expand Down
39 changes: 39 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,42 @@ func TestIsRecoverable(t *testing.T) {
err = fmt.Errorf("wrapping: %w", err)
assert.False(t, IsRecoverable(err))
}

func TestNoRetryIfCtxCanceledAtemptsZero(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

var called bool
err := Do(
func() error {
cancel()
return context.Cause(ctx)
},
OnRetry(func(n uint, err error) {
called = true
}),
Context(ctx),
Attempts(0),
)
assert.Equal(t, context.Canceled, err)
assert.False(t, called, "OnRetry was called after cancelation")
}

func TestNoRetryIfCtxCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

var called bool
err := Do(
func() error {
cancel()
return context.Cause(ctx)
},
OnRetry(func(n uint, err error) {
called = true
}),
Context(ctx),
Attempts(5),
LastErrorOnly(true),
)
assert.Equal(t, context.Canceled, err)
assert.False(t, called, "OnRetry was called after cancelation")
}