Skip to content

Commit

Permalink
go/backoff: Update cenkalti/backoff to 4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Oct 8, 2020
1 parent 20c2b93 commit 7d5e159
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ replace (
require (
github.com/blevesearch/bleve v1.0.12
github.com/btcsuite/btcutil v1.0.2
github.com/cenkalti/backoff/v4 v4.0.2
github.com/cenkalti/backoff/v4 v4.1.0
github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEe
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU=
github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs=
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down
31 changes: 5 additions & 26 deletions go/worker/common/p2p/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package error
import (
"context"
"errors"
"fmt"

"github.com/cenkalti/backoff/v4"
)
Expand Down Expand Up @@ -64,32 +63,12 @@ func IsPermanent(err error) bool {
// EnsurePermanent ensures an error will be correctly treated as (non-)permanent
// by `cenkalti/backoff/v4`.
//
// Note: `IsPermanent` notion of a permanent error differs from the `cenkalti/backoff/v4`:
// - it special cases `context.Canceled`
// - it correctly handles wrapped permanent errors
// Note: `IsPermanent` special cases `conext.Canceled` and doesn't ever handle
// it as a permanent error.
func EnsurePermanent(err error) error {
// XXX: once https://github.com/cenkalti/backoff/issues/107 is addressed
// we only need to handle the `context.Canceled` case.
upstreamCheck := func(err error) bool {
// https://github.com/cenkalti/backoff/blob/31cc31bb63269a3c813d1f26e0ab058452b7d803/retry.go#L56-L58
if _, ok := err.(*backoff.PermanentError); ok {
return true
}
return false
}

if IsPermanent(err) == upstreamCheck(err) {
return err
if errors.Is(err, context.Canceled) {
return context.Canceled
}

switch {
case IsPermanent(err):
// In case we consider error as permanent, re-wrap it to ensure
// `cenkalti/backoff/v4` will also treat it as such.
e := backoff.Permanent(err)
return e
default:
// In case we don't consider the error permanent, but upstream does, wrap it.
return fmt.Errorf("non-permanent: %w", err)
}
return err
}
24 changes: 7 additions & 17 deletions go/worker/common/p2p/error/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ func TestEnsurePermanent(t *testing.T) {
},
true,
},
{
"Relayable(Permanent(io.EOF))",
func() error {
return Relayable(Permanent(io.EOF))
},
false,
},
{
// This case is treated as permanent by backoff, but we treat it as
// a non-permanent error.
Expand All @@ -107,23 +114,6 @@ func TestEnsurePermanent(t *testing.T) {
},
true,
},
{
// This case is retried by backoff, since their permanent check
// doesn't unwrap the error.
"Relayable(Permanent(io.EOF))",
func() error {
return Relayable(Permanent(io.EOF))
},
true,
},
{
// This case tests EnsurePermanent on the previous test case.
"EnsurePermanent(Relayable(Permanent(io.EOF)))",
func() error {
return EnsurePermanent(Relayable(Permanent(io.EOF)))
},
false,
},
} {
ensureRetries(testCase.name, testCase.shouldRetry, testCase.testF)
}
Expand Down

0 comments on commit 7d5e159

Please sign in to comment.