Skip to content

Commit

Permalink
Merge pull request #1492 from fengxsong/retry_options
Browse files Browse the repository at this point in the history
feat: add a function to determine whether a retry is necessary when error occured
  • Loading branch information
openshift-merge-robot authored Jun 12, 2023
2 parents 2c1849f + 8c5e6c4 commit 2bc6436
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

// Options defines the option to retry.
type Options struct {
MaxRetry int // The number of times to possibly retry.
Delay time.Duration // The delay to use between retries, if set.
MaxRetry int // The number of times to possibly retry.
Delay time.Duration // The delay to use between retries, if set.
IsErrorRetryable func(error) bool
}

// RetryOptions is deprecated, use Options.
Expand All @@ -31,8 +32,11 @@ func RetryIfNecessary(ctx context.Context, operation func() error, options *Opti

// IfNecessary retries the operation in exponential backoff with the retry Options.
func IfNecessary(ctx context.Context, operation func() error, options *Options) error {
if options.IsErrorRetryable == nil {
options.IsErrorRetryable = IsErrorRetryable
}
err := operation()
for attempt := 0; err != nil && isRetryable(err) && attempt < options.MaxRetry; attempt++ {
for attempt := 0; err != nil && options.IsErrorRetryable(err) && attempt < options.MaxRetry; attempt++ {
delay := time.Duration(int(math.Pow(2, float64(attempt)))) * time.Second
if options.Delay != 0 {
delay = options.Delay
Expand All @@ -49,7 +53,11 @@ func IfNecessary(ctx context.Context, operation func() error, options *Options)
return err
}

func isRetryable(err error) bool {
// IsErrorRetryable makes a HEURISTIC determination whether it is worth retrying upon encountering an error.
// That heuristic is NOT STABLE and it CAN CHANGE AT ANY TIME.
// Callers that have a hard requirement for specific treatment of a class of errors should make their own check
// instead of relying on this function maintaining its past behavior.
func IsErrorRetryable(err error) bool {
switch err {
case nil:
return false
Expand All @@ -72,26 +80,26 @@ func isRetryable(err error) bool {
}
return true
case *net.OpError:
return isRetryable(e.Err)
return IsErrorRetryable(e.Err)
case *url.Error: // This includes errors returned by the net/http client.
if e.Err == io.EOF { // Happens when a server accepts a HTTP connection and sends EOF
return true
}
return isRetryable(e.Err)
return IsErrorRetryable(e.Err)
case syscall.Errno:
return isErrnoRetryable(e)
case errcode.Errors:
// if this error is a group of errors, process them all in turn
for i := range e {
if !isRetryable(e[i]) {
if !IsErrorRetryable(e[i]) {
return false
}
}
return true
case *multierror.Error:
// if this error is a group of errors, process them all in turn
for i := range e.Errors {
if !isRetryable(e.Errors[i]) {
if !IsErrorRetryable(e.Errors[i]) {
return false
}
}
Expand All @@ -102,11 +110,11 @@ func isRetryable(err error) bool {
}
if unwrappable, ok := e.(unwrapper); ok {
err = unwrappable.Unwrap()
return isRetryable(err)
return IsErrorRetryable(err)
}
case unwrapper: // Test this last, because various error types might implement .Unwrap()
err = e.Unwrap()
return isRetryable(err)
return IsErrorRetryable(err)
}

return false
Expand Down

0 comments on commit 2bc6436

Please sign in to comment.