-1

AWS 默认为其服务调用提供重试支持,通常设置为最多 3 次尝试。

我们可以配置重试对象以将重试次数设置为 5 吗?

4

2 回答 2

2

您可以为 SDK 定义自定义重试策略以使用:

func main() {
    sess := session.Must(
        session.NewSession(&aws.Config{
            // Use a custom retryer to provide custom retry rules.
            Retryer: CustomRetryer{
                DefaultRetryer: client.DefaultRetryer{
                    NumMaxRetries: client.DefaultRetryerMaxNumRetries,
                }},

            // Use the SDK's SharedCredentialsProvider directly instead of the
            // SDK's default credential chain. This ensures that the
            // application can call Config.Credentials.Expire. This  is counter
            // to the SDK's default credentials chain, which  will never reread
            // the shared credentials file.
            Credentials: credentials.NewCredentials(&credentials.SharedCredentialsProvider{
                Filename: defaults.SharedCredentialsFilename(),
                Profile:  "default",
            }),
            Region: aws.String(endpoints.UsWest2RegionID),
        }),
    )
    // Add a request handler to the AfterRetry handler stack that is used by the
    // SDK to be executed after the SDK has determined if it will retry.
    // This handler forces the SDK's Credentials to be expired, and next call to
    // Credentials.Get will attempt to refresh the credentials.
    sess.Handlers.AfterRetry.PushBack(func(req *request.Request) {
        if aerr, ok := req.Error.(awserr.RequestFailure); ok && aerr != nil {
            if aerr.Code() == "InvalidClaimException" {
                // Force the credentials to expire based on error code.  Next
                // call to Credentials.Get will attempt to refresh credentials.
                req.Config.Credentials.Expire()
            }
        }
    })

在此处查看示例代码

于 2021-09-27T08:31:28.700 回答
1

是的,AWS 支持配置其重试和超时功能。以下是在 AWS Golang SDK v2 中将最大重试次数增加到 5 次的两种方法:

  1. 在 AWS Config 对象 cfg 上配置重试逻辑,它可以使用 NewFromConfig 函数与各种 AWS 服务客户端一起使用
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRetryer(func() aws.Retryer {
    return retry.AddWithMaxAttempts(retry.NewStandard(), 5)
}))

client := s3.NewFromConfig(cfg)
  1. 仅为特定 AWS 服务客户端配置重试逻辑
customRetry := retry.NewStandard(func(o *retry.StandardOptions) {
        o.MaxAttempts = 5
    })

sqsClient := sqs.NewFromConfig(creds,
    func(o *sqs.Options) {
        o.Retryer = customRetry
    },
)

更多信息可以在https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/retries-timeouts/https://pkg.go.dev/github.com/aws找到/aws-sdk-go-v2/aws/retry#hdr-标准

于 2021-09-27T08:38:50.950 回答