Skip to content

Commit e344516

Browse files
Only check for ctx exipry in one place (#33)
This code reduces the ctx check to only one location, and perhaps makes the code simpler. Just discard this pull request if you disagree :)
1 parent 05bfdd6 commit e344516

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

retry.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ func DoValue[T any](ctx context.Context, b Backoff, f RetryFuncValue[T]) (T, err
5454

5555
for {
5656
// Return immediately if ctx is canceled
57-
select {
58-
case <-ctx.Done():
59-
return nilT, ctx.Err()
60-
default:
57+
if err := context.Cause(ctx); err != nil {
58+
return nilT, err
6159
}
6260

6361
v, err := f(ctx)
@@ -76,20 +74,13 @@ func DoValue[T any](ctx context.Context, b Backoff, f RetryFuncValue[T]) (T, err
7674
return nilT, rerr.Unwrap()
7775
}
7876

79-
// ctx.Done() has priority, so we test it alone first
80-
select {
81-
case <-ctx.Done():
82-
return nilT, ctx.Err()
83-
default:
84-
}
85-
77+
// Wait until next attempt or until the context expires. Any error will
78+
// be caught at the top of the loop.
8679
t := time.NewTimer(next)
8780
select {
8881
case <-ctx.Done():
8982
t.Stop()
90-
return nilT, ctx.Err()
9183
case <-t.C:
92-
continue
9384
}
9485
}
9586
}

0 commit comments

Comments
 (0)