default_retryer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package client
  2. import (
  3. "strconv"
  4. "time"
  5. "github.com/aws/aws-sdk-go/aws/request"
  6. "github.com/aws/aws-sdk-go/internal/sdkrand"
  7. )
  8. // DefaultRetryer implements basic retry logic using exponential backoff for
  9. // most services. If you want to implement custom retry logic, implement the
  10. // request.Retryer interface or create a structure type that composes this
  11. // struct and override the specific methods. For example, to override only
  12. // the MaxRetries method:
  13. //
  14. // type retryer struct {
  15. // client.DefaultRetryer
  16. // }
  17. //
  18. // // This implementation always has 100 max retries
  19. // func (d retryer) MaxRetries() int { return 100 }
  20. type DefaultRetryer struct {
  21. NumMaxRetries int
  22. }
  23. // MaxRetries returns the number of maximum returns the service will use to make
  24. // an individual API request.
  25. func (d DefaultRetryer) MaxRetries() int {
  26. return d.NumMaxRetries
  27. }
  28. // RetryRules returns the delay duration before retrying this request again
  29. func (d DefaultRetryer) RetryRules(r *request.Request) time.Duration {
  30. // Set the upper limit of delay in retrying at ~five minutes
  31. minTime := 30
  32. throttle := d.shouldThrottle(r)
  33. if throttle {
  34. if delay, ok := getRetryDelay(r); ok {
  35. return delay
  36. }
  37. minTime = 500
  38. }
  39. retryCount := r.RetryCount
  40. if throttle && retryCount > 8 {
  41. retryCount = 8
  42. } else if retryCount > 13 {
  43. retryCount = 13
  44. }
  45. delay := (1 << uint(retryCount)) * (sdkrand.SeededRand.Intn(minTime) + minTime)
  46. return time.Duration(delay) * time.Millisecond
  47. }
  48. // ShouldRetry returns true if the request should be retried.
  49. func (d DefaultRetryer) ShouldRetry(r *request.Request) bool {
  50. // If one of the other handlers already set the retry state
  51. // we don't want to override it based on the service's state
  52. if r.Retryable != nil {
  53. return *r.Retryable
  54. }
  55. if r.HTTPResponse.StatusCode >= 500 && r.HTTPResponse.StatusCode != 501 {
  56. return true
  57. }
  58. return r.IsErrorRetryable() || d.shouldThrottle(r)
  59. }
  60. // ShouldThrottle returns true if the request should be throttled.
  61. func (d DefaultRetryer) shouldThrottle(r *request.Request) bool {
  62. switch r.HTTPResponse.StatusCode {
  63. case 429:
  64. case 502:
  65. case 503:
  66. case 504:
  67. default:
  68. return r.IsErrorThrottle()
  69. }
  70. return true
  71. }
  72. // This will look in the Retry-After header, RFC 7231, for how long
  73. // it will wait before attempting another request
  74. func getRetryDelay(r *request.Request) (time.Duration, bool) {
  75. if !canUseRetryAfterHeader(r) {
  76. return 0, false
  77. }
  78. delayStr := r.HTTPResponse.Header.Get("Retry-After")
  79. if len(delayStr) == 0 {
  80. return 0, false
  81. }
  82. delay, err := strconv.Atoi(delayStr)
  83. if err != nil {
  84. return 0, false
  85. }
  86. return time.Duration(delay) * time.Second, true
  87. }
  88. // Will look at the status code to see if the retry header pertains to
  89. // the status code.
  90. func canUseRetryAfterHeader(r *request.Request) bool {
  91. switch r.HTTPResponse.StatusCode {
  92. case 429:
  93. case 503:
  94. default:
  95. return false
  96. }
  97. return true
  98. }