まず AWS SDK for Go を使ってAPIをコールする場合は、デフォルトでリトライするようになっています 1。そのため AWS SDK for Go を使うアプリケーション側でリトライを実装する必要はありません。AWS SDK for Go 上の実装は client.DefaultRetryer がリトライを実施します。リトライ時の待ち時間である time.Duration を計算するアルゴリズムは RetryRules メソッドとして実装されています。
待ち時間を計算するアルゴリズムはExponential Backoff And Jitter 2です。
リトライの再試行の待ち時間を計算する RetryRules メソッド
// RetryRules returns the delay duration before retrying this request again func(d DefaultRetryer) RetryRules(r *request.Request) time.Duration {
// if number of max retries is zero, no retries will be performed. if d.NumMaxRetries == 0 { return0 }
// Sets default value for retryer members d.setRetryerDefaults()
// minDelay is the minimum retryer delay minDelay := d.MinRetryDelay
var initialDelay time.Duration
isThrottle := r.IsErrorThrottle() if isThrottle { if delay, ok := getRetryAfterDelay(r); ok { initialDelay = delay } minDelay = d.MinThrottleDelay }
retryCount := r.RetryCount
// maxDelay the maximum retryer delay maxDelay := d.MaxRetryDelay
if isThrottle { maxDelay = d.MaxThrottleDelay }
var delay time.Duration
// Logic to cap the retry count based on the minDelay provided actualRetryCount := int(math.Log2(float64(minDelay))) + 1 if actualRetryCount < 63-retryCount { delay = time.Duration(1<<uint64(retryCount)) * getJitterDelay(minDelay) if delay > maxDelay { delay = getJitterDelay(maxDelay / 2) } } else { delay = getJitterDelay(maxDelay / 2) } return delay + initialDelay }
type Config struct { // ... // Retryer guides how HTTP requests should be retried in case of // recoverable failures. // // When nil or the value does not implement the request.Retryer interface, // the client.DefaultRetryer will be used. // // When both Retryer and MaxRetries are non-nil, the former is used and // the latter ignored. // // To set the Retryer field in a type-safe manner and with chaining, use // the request.WithRetryer helper function: // // cfg := request.WithRetryer(aws.NewConfig(), myRetryer) // Retryer RequestRetryer // ... }
上記のようにリトライアルゴリズムを差し替えることができます。もちろん client.DefaultRetryer を使って、リトライの設定(client.DefaultRetryer の MaxRetryDelay など)を変えることによってリトライの待ち時間の計算に影響を及ぼすこともできます。AWS SDK for Go が提供するデフォルトのリトライアルゴリズム・設定ではパフォーマンス上の問題があるケースなど、リトライのアルゴリズムや設定を差し替えたい場合に本記事が参考になれば幸いです。