Skip to content

Commit

Permalink
refactor cancel context (sethvargo#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
quagmt committed Jan 15, 2022
1 parent 2b17a41 commit f691e5c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
5 changes: 1 addition & 4 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func Benchmark(b *testing.B) {
})

b.Run("sethvargo", func(b *testing.B) {
backoff, err := sethvargo.NewExponential(1 * time.Second)
if err != nil {
b.Fatal(err)
}
backoff := sethvargo.NewExponential(1 * time.Second)
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go 1.14
require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/lestrrat-go/backoff v1.0.0
github.com/sethvargo/go-retry v0.0.0-20200703211810-8390cace92d3
github.com/sethvargo/go-retry v0.2.2
)
10 changes: 7 additions & 3 deletions benchmark/go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
github.com/cenkalti/backoff v1.1.0 h1:QnvVp8ikKCDWOsFheytRCoYWYPO/ObCTBGxT19Hc+yE=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/lestrrat-go/backoff v1.0.0 h1:nR+UgAhdhwfw2i+xznuHRlj81oMYa7u3lXun0xcsXUU=
github.com/lestrrat-go/backoff v1.0.0/go.mod h1:c7OnDlnHsFXbH1vyIS8+txH+THcc+QFlSQTrJVe4EIM=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sethvargo/go-retry v0.0.0-20200703211810-8390cace92d3 h1:h/E1woky2/x1yuR7+xtNhNUmGGD5tbNna37QsNvlhy4=
github.com/sethvargo/go-retry v0.0.0-20200703211810-8390cace92d3/go.mod h1:JzIOdZqQDNpPkQDmcqgtteAcxFLtYpNF/zJCM1ysDg8=
github.com/sethvargo/go-retry v0.2.2 h1:TGPxHlApEgiGZHaxrfFN2U+wHEuK3V6KASblJzt78Yc=
github.com/sethvargo/go-retry v0.2.2/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
21 changes: 7 additions & 14 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ 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 {
// Return immediately if ctx is canceled
select {
case <-ctx.Done():
return ctx.Err()
default:
}

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

// ctx.Done() has priority, so we test it alone first
select {
case <-ctx.Done():
return ctx.Err()
default:
}

select {
case <-ctx.Done():
return ctx.Err()
Expand Down

0 comments on commit f691e5c

Please sign in to comment.