Skip to content

Commit 7f2a917

Browse files
authored
Merge pull request #933 from hashicorp/f-token_bucket_rate_limiter_capacity
Add `Config.TokenBucketRateLimiterCapacity`
2 parents 8bd71d0 + 17db66c commit 7f2a917

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

aws_config.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/aws/aws-sdk-go-v2/aws"
1717
"github.com/aws/aws-sdk-go-v2/aws/defaults"
1818
awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
19+
"github.com/aws/aws-sdk-go-v2/aws/ratelimit"
1920
"github.com/aws/aws-sdk-go-v2/aws/retry"
2021
"github.com/aws/aws-sdk-go-v2/config"
2122
"github.com/aws/aws-sdk-go-v2/credentials"
@@ -28,6 +29,7 @@ import (
2829
"github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints"
2930
"github.com/hashicorp/aws-sdk-go-base/v2/logging"
3031
"github.com/hashicorp/terraform-plugin-log/tflog"
32+
"golang.org/x/exp/slices"
3133
)
3234

3335
const loggerName string = "aws-base"
@@ -173,7 +175,7 @@ func GetAwsConfig(ctx context.Context, c *Config) (context.Context, aws.Config,
173175
}
174176
}
175177

176-
resolveRetryer(baseCtx, &awsConfig)
178+
resolveRetryer(baseCtx, c.TokenBucketRateLimiterCapacity, &awsConfig)
177179

178180
if !c.SkipCredsValidation {
179181
if _, _, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(baseCtx, stsClient(baseCtx, awsConfig, c)); err != nil {
@@ -186,7 +188,7 @@ func GetAwsConfig(ctx context.Context, c *Config) (context.Context, aws.Config,
186188

187189
// Adapted from the per-service-client `resolveRetryer()` functions in the AWS SDK for Go v2
188190
// e.g. https://github.com/aws/aws-sdk-go-v2/blob/main/service/accessanalyzer/api_client.go
189-
func resolveRetryer(ctx context.Context, awsConfig *aws.Config) {
191+
func resolveRetryer(ctx context.Context, tokenBucketRateLimiterCapacity int, awsConfig *aws.Config) {
190192
retryMode := awsConfig.RetryMode
191193
if len(retryMode) == 0 {
192194
defaultsMode := resolveDefaultsMode(ctx, awsConfig)
@@ -206,8 +208,15 @@ func resolveRetryer(ctx context.Context, awsConfig *aws.Config) {
206208
})
207209
}
208210

209-
newRetryer := func(retryMode aws.RetryMode, standardOptions []func(*retry.StandardOptions)) aws.RetryerV2 {
211+
newRetryer := func(retryMode aws.RetryMode, standardOptions []func(*retry.StandardOptions), tokenBucketRateLimiterCapacity int) aws.RetryerV2 {
210212
var retryer aws.RetryerV2
213+
214+
if tokenBucketRateLimiterCapacity > 0 {
215+
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
216+
so.RateLimiter = ratelimit.NewTokenRateLimit(uint(tokenBucketRateLimiterCapacity))
217+
})
218+
}
219+
211220
switch retryMode {
212221
case aws.RetryModeAdaptive:
213222
var adaptiveOptions []func(*retry.AdaptiveModeOptions)
@@ -228,7 +237,7 @@ func resolveRetryer(ctx context.Context, awsConfig *aws.Config) {
228237
awsConfig.Retryer = func() aws.Retryer {
229238
return &networkErrorShortcutter{
230239
// Ensure that each invocation of this function returns an independent Retryer.
231-
RetryerV2: newRetryer(retryMode, standardOptions),
240+
RetryerV2: newRetryer(retryMode, slices.Clone(standardOptions), tokenBucketRateLimiterCapacity),
232241
}
233242
}
234243
}

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Config struct {
6363
StsRegion string
6464
SuppressDebugLog bool
6565
Token string
66+
TokenBucketRateLimiterCapacity int
6667
UseDualStackEndpoint bool
6768
UseFIPSEndpoint bool
6869
UseLegacyWorkflow bool

0 commit comments

Comments
 (0)