feat: retry ignore specified errors (#3808)
This commit is contained in:
@@ -2,6 +2,7 @@ package fx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/errorx"
|
||||
@@ -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
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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
|
||||
@@ -103,6 +110,13 @@ func WithTimeout(timeout time.Duration) RetryOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithIgnoreErrors Ignore the specified errors
|
||||
func WithIgnoreErrors(IgnoreErrors []error) RetryOption {
|
||||
return func(options *retryOptions) {
|
||||
options.IgnoreErrors = IgnoreErrors
|
||||
}
|
||||
}
|
||||
|
||||
func newRetryOptions() *retryOptions {
|
||||
return &retryOptions{
|
||||
times: defaultRetryTimes,
|
||||
|
||||
Reference in New Issue
Block a user