-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry on AWS retry error and throttle codes (#11)
* Retry on AWS retry error and throttle codes Copy some internal, inaccessible retry logic from the AWS SDK * Streamline tags in terraform test fixtures
- Loading branch information
Showing
13 changed files
with
159 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package provider | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/request" | ||
) | ||
|
||
//nolint:gochecknoglobals | ||
var ( | ||
// copied from github.com/aws-sdk-go/aws/request/retryer.go | ||
retryableCodes = map[string]struct{}{ | ||
request.ErrCodeRequestError: {}, | ||
"RequestTimeout": {}, | ||
request.ErrCodeResponseTimeout: {}, | ||
"RequestTimeoutException": {}, // Glacier's flavor of RequestTimeout | ||
} | ||
|
||
// copied from github.com/aws-sdk-go/aws/request/retryer.go | ||
throttleCodes = map[string]struct{}{ | ||
"ProvisionedThroughputExceededException": {}, | ||
"ThrottledException": {}, // SNS, XRay, ResourceGroupsTagging API | ||
"Throttling": {}, | ||
"ThrottlingException": {}, | ||
"RequestLimitExceeded": {}, | ||
"RequestThrottled": {}, | ||
"RequestThrottledException": {}, | ||
"TooManyRequestsException": {}, // Lambda functions | ||
"PriorRequestNotComplete": {}, // Route53 | ||
"TransactionInProgressException": {}, | ||
"EC2ThrottledException": {}, // EC2 | ||
} | ||
|
||
// copied from github.com/aws-sdk-go/aws/request/retryer.go | ||
credsExpiredCodes = map[string]struct{}{ | ||
"ExpiredToken": {}, | ||
"ExpiredTokenException": {}, | ||
"RequestExpired": {}, // EC2 Only | ||
} | ||
) | ||
|
||
// shouldRetry returns true if the request should be retried. | ||
// Note: the given error is checked against retryable error codes of the AWS SDK API v1, | ||
// since Terraform AWS Provider also uses v1. | ||
func shouldRetry(err error) bool { | ||
return isCodeRetryable(err) || isCodeThrottle(err) | ||
} | ||
|
||
func isCodeThrottle(err error) bool { | ||
for throttleCode := range throttleCodes { | ||
if strings.Contains(err.Error(), throttleCode) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func isCodeRetryable(err error) bool { | ||
for retryableCode := range retryableCodes { | ||
if strings.Contains(err.Error(), retryableCode) { | ||
return true | ||
} | ||
} | ||
|
||
return isCodeExpiredCreds(err) | ||
} | ||
|
||
func isCodeExpiredCreds(err error) bool { | ||
for credsExpiredCode := range credsExpiredCodes { | ||
if strings.Contains(err.Error(), credsExpiredCode) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func Test_shouldRetry(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arg error | ||
want bool | ||
}{ | ||
{ | ||
name: "a 'Throttling' error that is retryable", | ||
arg: fmt.Errorf("ThrottlingException: Rate exceeded"), | ||
want: true, | ||
}, | ||
|
||
{ | ||
name: "a 'RequestExpired' error that is retryable", | ||
arg: fmt.Errorf("RequestExpired: request has expired"), | ||
want: true, | ||
}, | ||
{ | ||
name: "a 'RequestError' error that is retryable", | ||
arg: fmt.Errorf("RequestError: send request failed"), | ||
want: true, | ||
}, | ||
{ | ||
name: "some error that is not retryable", | ||
arg: fmt.Errorf("SomeError: foo bar"), | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := shouldRetry(tt.arg); got != tt.want { | ||
t.Errorf("shouldRetry() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters