-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Few fixes and improvements: - add a `expbackoff.Retry` util function. (will refactor existing retries to this in next PR) - add debug logs to relayer cursors - Enable chaos errors in relayer. issue: none
- Loading branch information
1 parent
6b8a0a5
commit 1be29e1
Showing
7 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package expbackoff | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/omni-network/omni/lib/errors" | ||
) | ||
|
||
const defaultRetries = 3 | ||
|
||
// WithRetryCount sets the number of retries to n. | ||
// Note this is only applicable for use with Retry. | ||
func WithRetryCount(n int) func(config *Config) { | ||
return func(c *Config) { | ||
c.retryCount = n | ||
} | ||
} | ||
|
||
// Retry calls the provided function multiple times (default=3) with backoff until: | ||
// - The function returns nil (Retry returns nil) | ||
// - The context is canceled (Retry returns the context error) | ||
// - The retry count is exhausted (Retry returns the last error). | ||
func Retry(ctx context.Context, fn func() error, opts ...func(*Config)) error { | ||
var remaining int // Workaround to extract retry count from options. | ||
opts = append(opts, func(c *Config) { | ||
remaining = c.retryCount | ||
}) | ||
|
||
backoff := New(ctx, opts...) | ||
for { | ||
remaining-- | ||
|
||
err := fn() | ||
if err == nil { | ||
return nil | ||
} else if remaining <= 0 { | ||
return errors.Wrap(err, "max retries") | ||
} | ||
|
||
backoff() | ||
|
||
if ctx.Err() != nil { | ||
return errors.Wrap(ctx.Err(), "retry timeout") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//nolint:paralleltest // Parallel tests not supported since test-alias globals are used. | ||
package expbackoff_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/omni-network/omni/lib/expbackoff" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
// Disable backoff for testing. | ||
expbackoff.SetAfterForT(t, func(d time.Duration) <-chan time.Time { | ||
ch := make(chan time.Time, 1) | ||
ch <- time.Now() | ||
|
||
return ch | ||
}) | ||
|
||
ctx := context.Background() | ||
|
||
t.Run("default", func(t *testing.T) { | ||
var count int | ||
err := expbackoff.Retry(ctx, func() error { | ||
count++ | ||
return io.EOF | ||
}) | ||
require.ErrorIs(t, err, io.EOF) | ||
require.Equal(t, 3, count) // Default backoff count | ||
}) | ||
|
||
t.Run("with count", func(t *testing.T) { | ||
const maxRetries = 5 | ||
var count int | ||
err := expbackoff.Retry(ctx, func() error { | ||
count++ | ||
return io.EOF | ||
}, expbackoff.WithRetryCount(maxRetries)) | ||
require.ErrorIs(t, err, io.EOF) | ||
require.Equal(t, maxRetries, count) // Default backoff count | ||
}) | ||
|
||
t.Run("context cancel", func(t *testing.T) { | ||
// Cancel the context | ||
ctx, cancel := context.WithCancel(ctx) | ||
cancel() | ||
|
||
var count int | ||
err := expbackoff.Retry(ctx, func() error { | ||
count++ | ||
return io.EOF | ||
}) | ||
require.ErrorIs(t, err, context.Canceled) | ||
require.Equal(t, 1, count) // No retries | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters