-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate the S3 SDK from v1 to v2 (#16664)
Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>
- Loading branch information
Showing
7 changed files
with
298 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,75 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package s3backupstorage | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
) | ||
|
||
// ClosedConnectionRetryer implements the aws request.Retryer interface | ||
// ClosedConnectionRetryer implements the aws.Retryer interface | ||
// and is used to retry closed connection errors during MultipartUpload | ||
// operations. | ||
// operations. It is a simplified version of the RetryableConnectionError | ||
// implementation, which always retry on any type of connection error. | ||
type ClosedConnectionRetryer struct { | ||
awsRetryer request.Retryer | ||
awsRetryer aws.Retryer | ||
} | ||
|
||
// RetryRules is part of the Retryer interface. It defers to the underlying | ||
// aws Retryer to compute backoff rules. | ||
func (retryer *ClosedConnectionRetryer) RetryRules(r *request.Request) time.Duration { | ||
return retryer.awsRetryer.RetryRules(r) | ||
} | ||
|
||
// ShouldRetry is part of the Retryer interface. It retries on errors that occur | ||
// due to a closed network connection, and then falls back to the underlying aws | ||
// Retryer for checking additional retry conditions. | ||
func (retryer *ClosedConnectionRetryer) ShouldRetry(r *request.Request) bool { | ||
if retryer.MaxRetries() == 0 { | ||
// IsErrorRetryable returns true if the error should be retried. We first try | ||
// to see if the error is due to the use of a closed connection, if it is, | ||
// we retry, and if not, we default to what the aws.Retryer would do. | ||
func (retryer *ClosedConnectionRetryer) IsErrorRetryable(err error) bool { | ||
if retryer.MaxAttempts() == 0 { | ||
return false | ||
} | ||
|
||
if r.Retryable != nil { | ||
return *r.Retryable | ||
} | ||
|
||
if r.Error != nil { | ||
if awsErr, ok := r.Error.(awserr.Error); ok { | ||
if strings.Contains(awsErr.Error(), "use of closed network connection") { | ||
return true | ||
} | ||
if err != nil { | ||
if strings.Contains(err.Error(), "use of closed network connection") { | ||
return true | ||
} | ||
} | ||
|
||
return retryer.awsRetryer.ShouldRetry(r) | ||
return retryer.awsRetryer.IsErrorRetryable(err) | ||
} | ||
|
||
// MaxAttempts returns the maximum number of attempts that can be made for | ||
// an attempt before failing. A value of 0 implies that the attempt should | ||
// be retried until it succeeds if the errors are retryable. | ||
func (retryer *ClosedConnectionRetryer) MaxAttempts() int { | ||
return retryer.awsRetryer.MaxAttempts() | ||
} | ||
|
||
// RetryDelay returns the delay that should be used before retrying the | ||
// attempt. Will return error if the delay could not be determined. | ||
func (retryer *ClosedConnectionRetryer) RetryDelay(attempt int, opErr error) (time.Duration, error) { | ||
return retryer.awsRetryer.RetryDelay(attempt, opErr) | ||
} | ||
|
||
// GetRetryToken attempts to deduct the retry cost from the retry token pool. | ||
// Returning the token release function, or error. | ||
func (retryer *ClosedConnectionRetryer) GetRetryToken(ctx context.Context, opErr error) (releaseToken func(error) error, err error) { | ||
return retryer.awsRetryer.GetRetryToken(ctx, opErr) | ||
} | ||
|
||
// MaxRetries is part of the Retryer interface. It defers to the | ||
// underlying aws Retryer for the max number of retries. | ||
func (retryer *ClosedConnectionRetryer) MaxRetries() int { | ||
return retryer.awsRetryer.MaxRetries() | ||
// GetInitialToken returns the initial attempt token that can increment the | ||
// retry token pool if the attempt is successful. | ||
func (retryer *ClosedConnectionRetryer) GetInitialToken() (releaseToken func(error) error) { | ||
return retryer.awsRetryer.GetInitialToken() | ||
} |
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
Oops, something went wrong.