-
Notifications
You must be signed in to change notification settings - Fork 19
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
add exponential retry mechanism for rpc requests using utility function #593
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/utils/logging" | ||
"github.com/cenkalti/backoff/v4" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// WithMaxRetriesLog runs the operation until it succeeds or max retries has been reached. | ||
// It uses exponential back off. | ||
// It optionally logs information if logger is set. | ||
func WithMaxRetriesLog( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than separate with/without log functions, I think we should instead have a single method that takes a |
||
operation backoff.Operation, | ||
max uint64, | ||
logger logging.Logger, | ||
msg string, | ||
fields ...zapcore.Field, | ||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think logging |
||
) error { | ||
attempt := uint(1) | ||
expBackOff := backoff.WithMaxRetries(backoff.NewExponentialBackOff(), max) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand well, you want to use Instead of giving a |
||
notify := func(err error, duration time.Duration) { | ||
if logger == nil { | ||
return | ||
} | ||
fields := append(fields, zap.Uint("attempt", attempt), zap.Error(err), zap.Duration("backoff", duration)) | ||
logger.Warn(msg, fields...) | ||
attempt++ | ||
} | ||
err := backoff.RetryNotify(operation, expBackOff, notify) | ||
if err != nil && logger != nil { | ||
fields := append(fields, zap.Uint64("attempts", uint64(attempt)), zap.Error(err)) | ||
logger.Error(msg, fields...) | ||
} | ||
return err | ||
} | ||
|
||
// WithMaxRetries rens the operation until it succeeds or max retries has been reached. | ||
// It uses exponential back off. | ||
func WithMaxRetries(operation backoff.Operation, max uint64) error { | ||
return WithMaxRetriesLog(operation, max, nil, "") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithMaxRetries(t *testing.T) { | ||
t.Run("NotEnoughRetry", func(t *testing.T) { | ||
retryable := newMockRetryableFn(3) | ||
err := WithMaxRetries( | ||
func() (err error) { | ||
_, err = retryable.Run() | ||
return err | ||
}, | ||
2, | ||
) | ||
require.Error(t, err) | ||
}) | ||
t.Run("EnoughRetry", func(t *testing.T) { | ||
retryable := newMockRetryableFn(2) | ||
var res bool | ||
err := WithMaxRetries( | ||
func() (err error) { | ||
res, err = retryable.Run() | ||
return err | ||
}, | ||
2, | ||
) | ||
require.NoError(t, err) | ||
require.True(t, res) | ||
}) | ||
} | ||
|
||
type mockRetryableFn struct { | ||
counter uint64 | ||
trigger uint64 | ||
} | ||
|
||
func newMockRetryableFn(trigger uint64) mockRetryableFn { | ||
return mockRetryableFn{ | ||
counter: 0, | ||
trigger: trigger, | ||
} | ||
} | ||
|
||
func (m *mockRetryableFn) Run() (bool, error) { | ||
if m.counter == m.trigger { | ||
return true, nil | ||
} | ||
m.counter++ | ||
return false, errors.New("error") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a head's up, the Warp API integration is planned to be deprecated in the near future. It's still reasonable to integrate exponential backoff here for the time being.