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

impl: add a retry with result function #2837

Merged
merged 2 commits into from
Nov 10, 2023
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
21 changes: 21 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math"
Expand Down Expand Up @@ -197,6 +198,26 @@ func Retry(operation retryFunc, retryCount int, initialDelayMilliseconds int) er
return err
}

// Retry retries an operation with a return value
func RetryWithResult[T any](operation func() (T, error), retryCount int, initialDelayMilliseconds int) (result T, err error) {
result, err = operation()
if err == nil {
return result, nil
}
for i := 0; i < retryCount; i++ {
sleepDuration := time.Millisecond * time.Duration(int(math.Pow(2, float64(i)))*initialDelayMilliseconds)
logrus.Warnf("Retrying operation after %s due to %v", sleepDuration, err)
time.Sleep(sleepDuration)

result, err = operation()
if err == nil {
return result, nil
}
}

return result, fmt.Errorf("unable to complete operation after %d attempts, last error: %w", retryCount, err)
}

func Lgetxattr(path string, attr string) ([]byte, error) {
// Start with a 128 length byte array
dest := make([]byte, 128)
Expand Down
38 changes: 38 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,41 @@ func TestRetry(t *testing.T) {
t.Fatalf("Not expecting error: %v", err)
}
}

func makeRetryFuncWithResult(numFailures int) func() (int, error) {
i := -1

return func() (int, error) {
i++
if i < numFailures {
return i, fmt.Errorf("Failing with i=%v", i)
}
return i, nil
}
}

func TestRetryWithResult(t *testing.T) {
// test with a function that does not return an error
result, err := RetryWithResult(makeRetryFuncWithResult(0), 0, 10)
if err != nil || result != 0 {
t.Fatalf("Got result %d and error: %v", result, err)
}
result, err = RetryWithResult(makeRetryFuncWithResult(0), 3, 10)
if err != nil || result != 0 {
t.Fatalf("Got result %d and error: %v", result, err)
}

// test with a function that returns an error twice
result, err = RetryWithResult(makeRetryFuncWithResult(2), 0, 10)
if err == nil || result != 0 {
t.Fatalf("Got result %d and error: %v", result, err)
}
result, err = RetryWithResult(makeRetryFuncWithResult(2), 1, 10)
if err == nil || result != 1 {
t.Fatalf("Got result %d and error: %v", result, err)
}
result, err = RetryWithResult(makeRetryFuncWithResult(2), 2, 10)
if err != nil || result != 2 {
t.Fatalf("Got result %d and error: %v", result, err)
}
}
Loading