Skip to content

Commit

Permalink
feat: awss3 DownloadFiles use backoff with 4xx error cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtwinkle committed Aug 31, 2023
1 parent efea36e commit bb7f1d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aws/awss3/awss3.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,38 @@ func DownloadFiles(ctx context.Context, region awsconfig.Region, bucketName Buck
return nil, err
}
eg.Go(func() error {
ctx, cancel := context.WithCancel(ctx)
b := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
var gErr error
if err := backoff.Retry(func() error {
if _, err := downloader.Download(ctx, f, &s3.GetObjectInput{
Bucket: bucketName.AWSString(),
Key: s3Key.AWSString(),
}); err != nil {
var oe *smithy.OperationError
if errors.As(err, &oe) {
var resErr *awshttp.ResponseError
if errors.As(oe.Err, &resErr) {
switch resErr.Response.StatusCode {
case http.StatusNotFound:
gErr = ErrNotFound
cancel()
case http.StatusBadRequest, http.StatusUnauthorized, http.StatusPaymentRequired,
http.StatusForbidden, http.StatusMethodNotAllowed, http.StatusUnprocessableEntity,
http.StatusMisdirectedRequest, http.StatusRequestURITooLong, http.StatusRequestEntityTooLarge,
http.StatusPreconditionFailed:
gErr = resErr.Err
cancel()
}
}
}
return err
}
return nil
}, b); err != nil {
if gErr != nil {
return gErr
}
return err
}
return nil
Expand Down
11 changes: 11 additions & 0 deletions aws/awss3/awss3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,17 @@ func TestDownloadFiles(t *testing.T) {
}
}
})
t.Run("Error:BucketNotFound", func(t *testing.T) {
t.Parallel()
_, err := awss3.DownloadFiles(ctx, TestRegion, "NOT_FOUND", keys, t.TempDir())
assert.Error(t, err)
})
t.Run("Error:KeyNotFound", func(t *testing.T) {
t.Parallel()
dummyKeys := awss3.Keys{"dummy", "dummy", "dummy"}
_, err := awss3.DownloadFiles(ctx, TestRegion, TestBucket, dummyKeys, t.TempDir())
assert.Error(t, err)
})
}

func TestPutObject(t *testing.T) {
Expand Down

0 comments on commit bb7f1d6

Please sign in to comment.