Skip to content

Commit

Permalink
Merge pull request #1011 from hashicorp/f-aws-sdk-go-v1-compatible-re…
Browse files Browse the repository at this point in the history
…try.MaxBackoff

AWS SDK for Go v2 `MaxBackoff` delay to match AWS SDK for Go v1 default
  • Loading branch information
ewbankkit committed Apr 11, 2024
2 parents 4a8595f + 761ed31 commit 23258f8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<!-- markdownlint-disable single-title -->
# v2.0.0 (Unreleased)

# v2.0.0-beta.52 (2024-04-11)

BUG FIXES

* Updates dependencies.

ENHANCEMENTS

* Adds `MaxBackoff` parameter to configure the maximum backoff delay that is allowed to occur between retrying a failed request ([#1011](https://github.com/hashicorp/aws-sdk-go-base/pull/1011))

# v2.0.0-beta.51 (2024-04-04)

BUG FIXES
Expand Down
35 changes: 21 additions & 14 deletions aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func GetAwsConfig(ctx context.Context, c *Config) (context.Context, aws.Config,
}
}

resolveRetryer(baseCtx, c.TokenBucketRateLimiterCapacity, &awsConfig)
resolveRetryer(baseCtx, c, &awsConfig)

if !c.SkipCredsValidation {
if _, _, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(baseCtx, stsClient(baseCtx, awsConfig, c)); err != nil {
Expand All @@ -187,7 +187,7 @@ func GetAwsConfig(ctx context.Context, c *Config) (context.Context, aws.Config,

// Adapted from the per-service-client `resolveRetryer()` functions in the AWS SDK for Go v2
// e.g. https://github.com/aws/aws-sdk-go-v2/blob/main/service/accessanalyzer/api_client.go
func resolveRetryer(ctx context.Context, tokenBucketRateLimiterCapacity int, awsConfig *aws.Config) {
func resolveRetryer(ctx context.Context, c *Config, awsConfig *aws.Config) {
retryMode := awsConfig.RetryMode
if len(retryMode) == 0 {
defaultsMode := resolveDefaultsMode(ctx, awsConfig)
Expand All @@ -201,24 +201,31 @@ func resolveRetryer(ctx context.Context, tokenBucketRateLimiterCapacity int, aws
}

var standardOptions []func(*retry.StandardOptions)

if v, found, _ := awsconfig.GetRetryMaxAttempts(ctx, awsConfig.ConfigSources); found && v != 0 {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.MaxAttempts = v
})
}

newRetryer := func(retryMode aws.RetryMode, standardOptions []func(*retry.StandardOptions), tokenBucketRateLimiterCapacity int) aws.RetryerV2 {
var retryer aws.RetryerV2
if maxBackoff := c.MaxBackoff; maxBackoff > 0 {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.MaxBackoff = maxBackoff
})
}

if tokenBucketRateLimiterCapacity > 0 {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.RateLimiter = ratelimit.NewTokenRateLimit(uint(tokenBucketRateLimiterCapacity))
})
} else {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.RateLimiter = ratelimit.None
})
}
if tokenBucketRateLimiterCapacity := c.TokenBucketRateLimiterCapacity; tokenBucketRateLimiterCapacity > 0 {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.RateLimiter = ratelimit.NewTokenRateLimit(uint(tokenBucketRateLimiterCapacity))
})
} else {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.RateLimiter = ratelimit.None
})
}

newRetryer := func(retryMode aws.RetryMode, standardOptions []func(*retry.StandardOptions)) aws.RetryerV2 {
var retryer aws.RetryerV2

switch retryMode {
case aws.RetryModeAdaptive:
Expand All @@ -240,7 +247,7 @@ func resolveRetryer(ctx context.Context, tokenBucketRateLimiterCapacity int, aws
awsConfig.Retryer = func() aws.Retryer {
return &networkErrorShortcutter{
// Ensure that each invocation of this function returns an independent Retryer.
RetryerV2: newRetryer(retryMode, slices.Clone(standardOptions), tokenBucketRateLimiterCapacity),
RetryerV2: newRetryer(retryMode, slices.Clone(standardOptions)),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {
IamEndpoint string
Insecure bool
Logger logging.Logger
MaxBackoff time.Duration
MaxRetries int
NoProxy string
Profile string
Expand Down
2 changes: 1 addition & 1 deletion v2/awsv1shim/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6
github.com/google/go-cmp v0.6.0
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.51
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.52
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/terraform-plugin-log v0.9.0
go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws v0.50.0
Expand Down

0 comments on commit 23258f8

Please sign in to comment.