retryer.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package request
  2. import (
  3. "time"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. )
  7. // Retryer is an interface to control retry logic for a given service.
  8. // The default implementation used by most services is the client.DefaultRetryer
  9. // structure, which contains basic retry logic using exponential backoff.
  10. type Retryer interface {
  11. RetryRules(*Request) time.Duration
  12. ShouldRetry(*Request) bool
  13. MaxRetries() int
  14. }
  15. // WithRetryer sets a config Retryer value to the given Config returning it
  16. // for chaining.
  17. func WithRetryer(cfg *aws.Config, retryer Retryer) *aws.Config {
  18. cfg.Retryer = retryer
  19. return cfg
  20. }
  21. // retryableCodes is a collection of service response codes which are retry-able
  22. // without any further action.
  23. var retryableCodes = map[string]struct{}{
  24. "RequestError": {},
  25. "RequestTimeout": {},
  26. ErrCodeResponseTimeout: {},
  27. "RequestTimeoutException": {}, // Glacier's flavor of RequestTimeout
  28. }
  29. var throttleCodes = map[string]struct{}{
  30. "ProvisionedThroughputExceededException": {},
  31. "Throttling": {},
  32. "ThrottlingException": {},
  33. "RequestLimitExceeded": {},
  34. "RequestThrottled": {},
  35. "TooManyRequestsException": {}, // Lambda functions
  36. "PriorRequestNotComplete": {}, // Route53
  37. }
  38. // credsExpiredCodes is a collection of error codes which signify the credentials
  39. // need to be refreshed. Expired tokens require refreshing of credentials, and
  40. // resigning before the request can be retried.
  41. var credsExpiredCodes = map[string]struct{}{
  42. "ExpiredToken": {},
  43. "ExpiredTokenException": {},
  44. "RequestExpired": {}, // EC2 Only
  45. }
  46. func isCodeThrottle(code string) bool {
  47. _, ok := throttleCodes[code]
  48. return ok
  49. }
  50. func isCodeRetryable(code string) bool {
  51. if _, ok := retryableCodes[code]; ok {
  52. return true
  53. }
  54. return isCodeExpiredCreds(code)
  55. }
  56. func isCodeExpiredCreds(code string) bool {
  57. _, ok := credsExpiredCodes[code]
  58. return ok
  59. }
  60. var validParentCodes = map[string]struct{}{
  61. ErrCodeSerialization: {},
  62. ErrCodeRead: {},
  63. }
  64. type temporaryError interface {
  65. Temporary() bool
  66. }
  67. func isNestedErrorRetryable(parentErr awserr.Error) bool {
  68. if parentErr == nil {
  69. return false
  70. }
  71. if _, ok := validParentCodes[parentErr.Code()]; !ok {
  72. return false
  73. }
  74. err := parentErr.OrigErr()
  75. if err == nil {
  76. return false
  77. }
  78. if aerr, ok := err.(awserr.Error); ok {
  79. return isCodeRetryable(aerr.Code())
  80. }
  81. if t, ok := err.(temporaryError); ok {
  82. return t.Temporary()
  83. }
  84. return isErrConnectionReset(err)
  85. }
  86. // IsErrorRetryable returns whether the error is retryable, based on its Code.
  87. // Returns false if error is nil.
  88. func IsErrorRetryable(err error) bool {
  89. if err != nil {
  90. if aerr, ok := err.(awserr.Error); ok {
  91. return isCodeRetryable(aerr.Code()) || isNestedErrorRetryable(aerr)
  92. }
  93. }
  94. return false
  95. }
  96. // IsErrorThrottle returns whether the error is to be throttled based on its code.
  97. // Returns false if error is nil.
  98. func IsErrorThrottle(err error) bool {
  99. if err != nil {
  100. if aerr, ok := err.(awserr.Error); ok {
  101. return isCodeThrottle(aerr.Code())
  102. }
  103. }
  104. return false
  105. }
  106. // IsErrorExpiredCreds returns whether the error code is a credential expiry error.
  107. // Returns false if error is nil.
  108. func IsErrorExpiredCreds(err error) bool {
  109. if err != nil {
  110. if aerr, ok := err.(awserr.Error); ok {
  111. return isCodeExpiredCreds(aerr.Code())
  112. }
  113. }
  114. return false
  115. }
  116. // IsErrorRetryable returns whether the error is retryable, based on its Code.
  117. // Returns false if the request has no Error set.
  118. //
  119. // Alias for the utility function IsErrorRetryable
  120. func (r *Request) IsErrorRetryable() bool {
  121. return IsErrorRetryable(r.Error)
  122. }
  123. // IsErrorThrottle returns whether the error is to be throttled based on its code.
  124. // Returns false if the request has no Error set
  125. //
  126. // Alias for the utility function IsErrorThrottle
  127. func (r *Request) IsErrorThrottle() bool {
  128. return IsErrorThrottle(r.Error)
  129. }
  130. // IsErrorExpired returns whether the error code is a credential expiry error.
  131. // Returns false if the request has no Error set.
  132. //
  133. // Alias for the utility function IsErrorExpiredCreds
  134. func (r *Request) IsErrorExpired() bool {
  135. return IsErrorExpiredCreds(r.Error)
  136. }