Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fetcher] Optionally Force Retry #275

Merged
merged 3 commits into from
Dec 11, 2020
Merged
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
15 changes: 15 additions & 0 deletions fetcher/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,18 @@ func WithMaxConnections(connections int) Option {
f.maxConnections = connections
}
}

// WithForceRetry overrides the default
// retry handling logic and treats every error
// as retriable.
//
// This is particularly useful when accessing a Rosetta
// implementation via a load balancer where there may be
// periods of inconsistency (i.e. we get a network status
// from one implementation and query another that has not
// yet synced the most recent block).
func WithForceRetry() Option {
return func(f *Fetcher) {
f.forceRetry = true
}
}
2 changes: 1 addition & 1 deletion fetcher/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (f *Fetcher) RequestFailedError(
return &Error{
Err: fmt.Errorf("%w: %s %s", ErrRequestFailed, message, err.Error()),
ClientErr: rosettaErr,
Retry: ((rosettaErr != nil && rosettaErr.Retriable) || transientError(err)) &&
Retry: ((rosettaErr != nil && rosettaErr.Retriable) || transientError(err) || f.forceRetry) &&
!errors.Is(err, context.Canceled),
}
}
Expand Down
1 change: 1 addition & 0 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Fetcher struct {
maxRetries uint64
retryElapsedTime time.Duration
insecureTLS bool
forceRetry bool

// connectionSemaphore is used to limit the
// number of concurrent requests we make.
Expand Down
19 changes: 17 additions & 2 deletions fetcher/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestNetworkStatusRetry(t *testing.T) {
expectedError error
retriableError bool

fetcherForceRetry bool
fetcherMaxRetries uint64
shouldCancel bool
}{
Expand All @@ -91,6 +92,13 @@ func TestNetworkStatusRetry(t *testing.T) {
expectedError: ErrRequestFailed,
fetcherMaxRetries: 5,
},
"non-retriable failure (with force)": {
network: basicNetwork,
errorsBeforeSuccess: 2,
expectedStatus: basicNetworkStatus,
fetcherMaxRetries: 5,
fetcherForceRetry: true,
},
"exhausted retries": {
network: basicNetwork,
errorsBeforeSuccess: 2,
Expand Down Expand Up @@ -148,10 +156,17 @@ func TestNetworkStatusRetry(t *testing.T) {

defer ts.Close()

opts := []Option{
WithRetryElapsedTime(5 * time.Second),
WithMaxRetries(test.fetcherMaxRetries),
}
if test.fetcherForceRetry {
opts = append(opts, WithForceRetry())
}

f := New(
ts.URL,
WithRetryElapsedTime(5*time.Second),
WithMaxRetries(test.fetcherMaxRetries),
opts...,
)
status, err := f.NetworkStatusRetry(
ctx,
Expand Down