-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
45 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
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,44 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/swayne275/go-retry/backoff" | ||
) | ||
|
||
// TODO tests should include retryable errors and non retryable errors | ||
|
||
// ConstantRetry is a wrapper around retry that uses a constant backoff. It will | ||
// retry the function f until it returns a non-retryable error, or the context is canceled. | ||
func ConstantRetry(ctx context.Context, t time.Duration, f RetryFunc) error { | ||
b, err := backoff.NewConstant(t) | ||
if err != nil { | ||
return fmt.Errorf("failed to create constant backoff: %w", err) | ||
} | ||
|
||
return Do(ctx, b, f) | ||
} | ||
|
||
// ExponentialRetry is a wrapper around retry that uses an exponential backoff. It will | ||
// retry the function f until it returns a non-retryable error, or the context is canceled. | ||
func ExponentialRetry(ctx context.Context, base time.Duration, f RetryFunc) error { | ||
b, err := backoff.NewExponential(base) | ||
if err != nil { | ||
return fmt.Errorf("failed to create exponential backoff: %w", err) | ||
} | ||
|
||
return Do(ctx, b, f) | ||
} | ||
|
||
// FibonacciRetry is a wrapper around retry that uses a FibonacciRetry backoff. It will | ||
// retry the function f until it returns a non-retryable error, or the context is canceled. | ||
func FibonacciRetry(ctx context.Context, base time.Duration, f RetryFunc) error { | ||
b, err := backoff.NewFibonacci(base) | ||
if err != nil { | ||
return fmt.Errorf("failed to create fibonacci backoff: %w", err) | ||
|
||
} | ||
return Do(ctx, b, f) | ||
} |
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,156 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConstantRetry(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("exit_on_context_cancelled", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(_ context.Context) error { | ||
return RetryableError(fmt.Errorf("some retryable err")) | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(10 * time.Nanosecond) | ||
cancel() | ||
}() | ||
|
||
if err := ConstantRetry(ctx, 1*time.Nanosecond, f); err != context.Canceled { | ||
t.Errorf("expected %q to be %q", err, context.Canceled) | ||
} | ||
}) | ||
|
||
t.Run("exit_on_RetryFunc_nonretryable_error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cnt := 0 | ||
nonRetryableCnt := 3 | ||
errNonRetryable := fmt.Errorf("some non-retryable error") | ||
f := func(_ context.Context) error { | ||
cnt++ | ||
|
||
if cnt > nonRetryableCnt { | ||
return errNonRetryable | ||
} | ||
|
||
return RetryableError(fmt.Errorf("some non-retryable error")) | ||
} | ||
|
||
err := ConstantRetry(context.Background(), 1*time.Nanosecond, f) | ||
if !errors.Is(err, errFunctionReturnedNonRetryableError) { | ||
t.Errorf("expected %q to be %q", err, errFunctionReturnedNonRetryableError) | ||
} | ||
if !errors.Is(err, errNonRetryable) { | ||
t.Errorf("expected %q to be %q", err, errNonRetryable) | ||
} | ||
if cnt != nonRetryableCnt+1 { | ||
t.Errorf("expected %d to be %d", cnt, nonRetryableCnt+1) | ||
} | ||
}) | ||
} | ||
|
||
func TestExponentialRetry(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("exit_on_context_cancelled", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(_ context.Context) error { | ||
return RetryableError(fmt.Errorf("some retryable err")) | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(10 * time.Nanosecond) | ||
cancel() | ||
}() | ||
|
||
if err := ExponentialRetry(ctx, 1*time.Nanosecond, f); err != context.Canceled { | ||
t.Errorf("expected %q to be %q", err, context.Canceled) | ||
} | ||
}) | ||
|
||
t.Run("exit_on_RetryFunc_nonretryable_error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cnt := 0 | ||
nonRetryableCnt := 3 | ||
errNonRetryable := fmt.Errorf("some non-retryable error") | ||
f := func(_ context.Context) error { | ||
cnt++ | ||
|
||
if cnt > nonRetryableCnt { | ||
return errNonRetryable | ||
} | ||
|
||
return RetryableError(fmt.Errorf("some non-retryable error")) | ||
} | ||
|
||
err := ExponentialRetry(context.Background(), 1*time.Nanosecond, f) | ||
if !errors.Is(err, errFunctionReturnedNonRetryableError) { | ||
t.Errorf("expected %q to be %q", err, errFunctionReturnedNonRetryableError) | ||
} | ||
if !errors.Is(err, errNonRetryable) { | ||
t.Errorf("expected %q to be %q", err, errNonRetryable) | ||
} | ||
if cnt != nonRetryableCnt+1 { | ||
t.Errorf("expected %d to be %d", cnt, nonRetryableCnt+1) | ||
} | ||
}) | ||
} | ||
|
||
func TestFibonacciRetry(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("exit_on_context_cancelled", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(_ context.Context) error { | ||
return RetryableError(fmt.Errorf("some retryable err")) | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(10 * time.Nanosecond) | ||
cancel() | ||
}() | ||
|
||
if err := FibonacciRetry(ctx, 1*time.Nanosecond, f); err != context.Canceled { | ||
t.Errorf("expected %q to be %q", err, context.Canceled) | ||
} | ||
}) | ||
|
||
t.Run("exit_on_RetryFunc_nonretryable_error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cnt := 0 | ||
nonRetryableCnt := 3 | ||
errNonRetryable := fmt.Errorf("some non-retryable error") | ||
f := func(_ context.Context) error { | ||
cnt++ | ||
|
||
if cnt > nonRetryableCnt { | ||
return errNonRetryable | ||
} | ||
|
||
return RetryableError(fmt.Errorf("some non-retryable error")) | ||
} | ||
|
||
err := FibonacciRetry(context.Background(), 1*time.Nanosecond, f) | ||
if !errors.Is(err, errFunctionReturnedNonRetryableError) { | ||
t.Errorf("expected %q to be %q", err, errFunctionReturnedNonRetryableError) | ||
} | ||
if !errors.Is(err, errNonRetryable) { | ||
t.Errorf("expected %q to be %q", err, errNonRetryable) | ||
} | ||
if cnt != nonRetryableCnt+1 { | ||
t.Errorf("expected %d to be %d", cnt, nonRetryableCnt+1) | ||
} | ||
}) | ||
} |