6 "github.com/aws/aws-sdk-go/aws"
7 "github.com/aws/aws-sdk-go/aws/awserr"
10 // Retryer is an interface to control retry logic for a given service.
11 // The default implementation used by most services is the service.DefaultRetryer
12 // structure, which contains basic retry logic using exponential backoff.
13 type Retryer interface {
14 RetryRules(*Request) time.Duration
15 ShouldRetry(*Request) bool
19 // WithRetryer sets a config Retryer value to the given Config returning it
21 func WithRetryer(cfg *aws.Config, retryer Retryer) *aws.Config {
26 // retryableCodes is a collection of service response codes which are retry-able
27 // without any further action.
28 var retryableCodes = map[string]struct{}{
31 ErrCodeResponseTimeout: {},
32 "RequestTimeoutException": {}, // Glacier's flavor of RequestTimeout
35 var throttleCodes = map[string]struct{}{
36 "ProvisionedThroughputExceededException": {},
38 "ThrottlingException": {},
39 "RequestLimitExceeded": {},
40 "RequestThrottled": {},
41 "TooManyRequestsException": {}, // Lambda functions
42 "PriorRequestNotComplete": {}, // Route53
45 // credsExpiredCodes is a collection of error codes which signify the credentials
46 // need to be refreshed. Expired tokens require refreshing of credentials, and
47 // resigning before the request can be retried.
48 var credsExpiredCodes = map[string]struct{}{
50 "ExpiredTokenException": {},
51 "RequestExpired": {}, // EC2 Only
54 func isCodeThrottle(code string) bool {
55 _, ok := throttleCodes[code]
59 func isCodeRetryable(code string) bool {
60 if _, ok := retryableCodes[code]; ok {
64 return isCodeExpiredCreds(code)
67 func isCodeExpiredCreds(code string) bool {
68 _, ok := credsExpiredCodes[code]
72 var validParentCodes = map[string]struct{}{
73 ErrCodeSerialization: struct{}{},
74 ErrCodeRead: struct{}{},
77 type temporaryError interface {
81 func isNestedErrorRetryable(parentErr awserr.Error) bool {
86 if _, ok := validParentCodes[parentErr.Code()]; !ok {
90 err := parentErr.OrigErr()
95 if aerr, ok := err.(awserr.Error); ok {
96 return isCodeRetryable(aerr.Code())
99 if t, ok := err.(temporaryError); ok {
103 return isErrConnectionReset(err)
106 // IsErrorRetryable returns whether the error is retryable, based on its Code.
107 // Returns false if error is nil.
108 func IsErrorRetryable(err error) bool {
110 if aerr, ok := err.(awserr.Error); ok {
111 return isCodeRetryable(aerr.Code()) || isNestedErrorRetryable(aerr)
117 // IsErrorThrottle returns whether the error is to be throttled based on its code.
118 // Returns false if error is nil.
119 func IsErrorThrottle(err error) bool {
121 if aerr, ok := err.(awserr.Error); ok {
122 return isCodeThrottle(aerr.Code())
128 // IsErrorExpiredCreds returns whether the error code is a credential expiry error.
129 // Returns false if error is nil.
130 func IsErrorExpiredCreds(err error) bool {
132 if aerr, ok := err.(awserr.Error); ok {
133 return isCodeExpiredCreds(aerr.Code())
139 // IsErrorRetryable returns whether the error is retryable, based on its Code.
140 // Returns false if the request has no Error set.
142 // Alias for the utility function IsErrorRetryable
143 func (r *Request) IsErrorRetryable() bool {
144 return IsErrorRetryable(r.Error)
147 // IsErrorThrottle returns whether the error is to be throttled based on its code.
148 // Returns false if the request has no Error set
150 // Alias for the utility function IsErrorThrottle
151 func (r *Request) IsErrorThrottle() bool {
152 return IsErrorThrottle(r.Error)
155 // IsErrorExpired returns whether the error code is a credential expiry error.
156 // Returns false if the request has no Error set.
158 // Alias for the utility function IsErrorExpiredCreds
159 func (r *Request) IsErrorExpired() bool {
160 return IsErrorExpiredCreds(r.Error)