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

feat: retry ignore specified errors #3808

Merged
merged 1 commit into from
Mar 2, 2024
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
20 changes: 17 additions & 3 deletions core/fx/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fx

import (
"context"
"errors"
"time"

"github.com/zeromicro/go-zero/core/errorx"
Expand All @@ -14,9 +15,10 @@ type (
RetryOption func(*retryOptions)

retryOptions struct {
times int
interval time.Duration
timeout time.Duration
times int
interval time.Duration
timeout time.Duration
IgnoreErrors []error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use 'ignoreErrors'.

}
)

Expand Down Expand Up @@ -62,6 +64,11 @@ func retry(ctx context.Context, fn func(errChan chan error, retryCount int), opt
select {
case err := <-errChan:
if err != nil {
for _, ignoreErr := range options.IgnoreErrors {
if errors.Is(err, ignoreErr) {
return nil
}
}
berr.Add(err)
} else {
return nil
Expand Down Expand Up @@ -103,6 +110,13 @@ func WithTimeout(timeout time.Duration) RetryOption {
}
}

// WithIgnoreErrors Ignore the specified errors
func WithIgnoreErrors(IgnoreErrors []error) RetryOption {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use 'ignoreErrors'.

return func(options *retryOptions) {
options.IgnoreErrors = IgnoreErrors
}
}

func newRetryOptions() *retryOptions {
return &retryOptions{
times: defaultRetryTimes,
Expand Down
18 changes: 18 additions & 0 deletions core/fx/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ func TestRetryWithInterval(t *testing.T) {

}

func TestRetryWithWithIgnoreErrors(t *testing.T) {
ignoreErr1 := errors.New("ignore error1")
ignoreErr2 := errors.New("ignore error2")
ignoreErrs := []error{ignoreErr1, ignoreErr2}

assert.Nil(t, DoWithRetry(func() error {
return ignoreErr1
}, WithIgnoreErrors(ignoreErrs)))

assert.Nil(t, DoWithRetry(func() error {
return ignoreErr2
}, WithIgnoreErrors(ignoreErrs)))

assert.NotNil(t, DoWithRetry(func() error {
return errors.New("any")
}))
}

func TestRetryCtx(t *testing.T) {
t.Run("with timeout", func(t *testing.T) {
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
Expand Down
Loading