Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cenkalti/backoff
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.1
Choose a base ref
...
head repository: cenkalti/backoff
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.0.2
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Feb 9, 2025

  1. updates Retry to use context.Cause

    This diff updates `Retry` to propagate errors using `context.Cause`
    instead of `Context.Err`. `context.Cause` was added in go 1.20 and allows
    users to propagate a specific error when cancelling a context. It is
    backwards compatible with `Context.Error`.
    
    * https://pkg.go.dev/context#Cause
    * https://pkg.go.dev/context#WithCancelCause
    sgg authored and cenkalti committed Feb 9, 2025
    Copy the full SHA
    432d4c1 View commit details
  2. ci: updates go version to 1.23

    sgg authored and cenkalti committed Feb 9, 2025
    Copy the full SHA
    a52b527 View commit details
Showing with 9 additions and 7 deletions.
  1. +1 −1 .github/workflows/go.yaml
  2. +2 −2 retry.go
  3. +6 −4 retry_test.go
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.23

- name: Build
run: go build -v ./...
4 changes: 2 additions & 2 deletions retry.go
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
}

// Stop retrying if context is cancelled.
if cerr := ctx.Err(); cerr != nil {
if cerr := context.Cause(ctx); cerr != nil {
return res, cerr
}

@@ -133,7 +133,7 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti
select {
case <-args.Timer.C():
case <-ctx.Done():
return res, ctx.Err()
return res, context.Cause(ctx)
}
}
}
10 changes: 6 additions & 4 deletions retry_test.go
Original file line number Diff line number Diff line change
@@ -89,8 +89,10 @@ func TestRetryContext(t *testing.T) {
var cancelOn = 3
var i = 0

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(context.Canceled)

expectedErr := errors.New("custom error")

// This function cancels context on "cancelOn" calls.
f := func() (bool, error) {
@@ -100,7 +102,7 @@ func TestRetryContext(t *testing.T) {
// cancelling the context in the operation function is not a typical
// use-case, however it allows to get predictable test results.
if i == cancelOn {
cancel()
cancel(expectedErr)
}

log.Println("error")
@@ -111,7 +113,7 @@ func TestRetryContext(t *testing.T) {
if err == nil {
t.Errorf("error is unexpectedly nil")
}
if !errors.Is(err, context.Canceled) {
if !errors.Is(err, expectedErr) {
t.Errorf("unexpected error: %s", err.Error())
}
if i != cancelOn {