retry: abort retry loop on context errors (#1383)

Fixes #1377
This commit is contained in:
Jarek Kowalski
2021-10-13 19:10:02 -07:00
committed by GitHub
parent befaac501b
commit b7a4fa56bf
2 changed files with 20 additions and 0 deletions

View File

@@ -56,6 +56,11 @@ func internalRetry(ctx context.Context, desc string, attempt AttemptFunc, isRetr
var lastError error
for i := 0; i < count; i++ {
if cerr := ctx.Err(); cerr != nil {
// nolint:wrapcheck
return nil, cerr
}
v, err := attempt()
if err == nil {
return v, nil

View File

@@ -1,10 +1,12 @@
package retry
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/kopia/kopia/internal/testlogging"
)
@@ -58,3 +60,16 @@ func TestRetry(t *testing.T) {
})
}
}
func TestRetryContextCancel(t *testing.T) {
t.Parallel()
ctx := testlogging.Context(t)
canceledctx, cancel := context.WithCancel(ctx)
cancel()
require.ErrorIs(t, context.Canceled, WithExponentialBackoffNoValue(canceledctx, "canceled", func() error {
return errRetriable
}, isRetriable))
}