我们可以在AWS Go SDK的服务调用中配置重试尝试吗?

huangapple go评论78阅读模式
英文:

Can we configure retry attempts on AWS Go SDK service calls

问题

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

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

英文:

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

Can we configure the retry object to set retry attempts to 5?

答案1

得分: 1

你可以为SDK定义一个自定义重试策略

func main() {
	sess := session.Must(
		session.NewSession(&aws.Config{
			// 使用自定义的重试器提供自定义的重试规则。
			Retryer: CustomRetryer{
				DefaultRetryer: client.DefaultRetryer{
					NumMaxRetries: client.DefaultRetryerMaxNumRetries,
				},
			},

			// 直接使用SDK的SharedCredentialsProvider而不是SDK的默认凭证链。
			// 这确保应用程序可以调用Config.Credentials.Expire。
			// 这与SDK的默认凭证链相反,后者永远不会重新读取共享凭证文件。
			Credentials: credentials.NewCredentials(&credentials.SharedCredentialsProvider{
				Filename: defaults.SharedCredentialsFilename(),
				Profile:  "default",
			}),
			Region: aws.String(endpoints.UsWest2RegionID),
		}),
	)
	// 向AfterRetry处理程序堆栈添加一个请求处理程序,该处理程序在SDK确定是否重试后执行。
	// 此处理程序强制SDK的凭证过期,并且下一次调用Credentials.Get将尝试刷新凭证。
	sess.Handlers.AfterRetry.PushBack(func(req *request.Request) {
		if aerr, ok := req.Error.(awserr.RequestFailure); ok && aerr != nil {
			if aerr.Code() == "InvalidClaimException" {
				// 根据错误代码强制凭证过期。下一次调用Credentials.Get将尝试刷新凭证。
				req.Config.Credentials.Expire()
			}
		}
	})

}

在这里查看示例代码

英文:

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

答案2

得分: 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-Standard

英文:

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

huangapple
  • 本文由 发表于 2021年9月27日 16:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/69343418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定