Home > other >  Can we configure retry attempts on AWS Golang SDK service calls
Can we configure retry attempts on AWS Golang SDK service calls

Time:09-27

AWS by default provides retries support on its service calls, which is usually set to a max of 3 attempts. Can we configure it.

CodePudding user response:

you can define a custom retry strategy for the SDK to use:

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()
            }
        }
    })

See the sample code here

CodePudding user response:

Yes, AWS provides support to configure their retry and timeouts features. Here are two ways to increase the max number of retries to 5 in AWS Golang SDK v2:

  1. Configure the retry logic on the AWS Config object cfg and it can be used with various AWS service clients using NewFromConfig function
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRetryer(func() aws.Retryer {
    return retry.AddWithMaxAttempts(retry.NewStandard(), 5)
}))

client := s3.NewFromConfig(cfg)
  1. Configure the retry logic only for a specific AWS service client
customRetry := retry.NewStandard(func(o *retry.StandardOptions) {
        o.MaxAttempts = 5
    })

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

More info can be found at https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/retries-timeouts/ and https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws/retry#hdr-Standard

  • Related