-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sqs client retryer #1099
Add sqs client retryer #1099
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package sqsevent | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/client" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/sqs" | ||
) | ||
|
||
type SqsRetryer struct { | ||
client.DefaultRetryer | ||
} | ||
|
||
func (r SqsRetryer) ShouldRetry(req *request.Request) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: personally I would prefer more descriptive variable names (perhaps retryer instead or r), just so I don't have to search for the type. |
||
return r.DefaultRetryer.ShouldRetry(req) || | ||
(req.Error != nil && strings.Contains(req.Error.Error(), "connection reset")) | ||
} | ||
|
||
func GetSqsClient(sess *session.Session) *sqs.SQS { | ||
return sqs.New(sess, &aws.Config{ | ||
Retryer: SqsRetryer{ | ||
DefaultRetryer: client.DefaultRetryer{ | ||
// Monitor continuously monitors SQS for events every 2 seconds | ||
NumMaxRetries: client.DefaultRetryerMaxNumRetries, | ||
MinRetryDelay: client.DefaultRetryerMinRetryDelay, | ||
MaxRetryDelay: 1200 * time.Millisecond, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any particular reason why we don't set MaxRetryDelay to be DefaultRetryerMaxRetryDelay and same for DefaultRetryerMinThrottleDelay? https://github.com/aws/aws-sdk-go/blob/7112c0a0c2d01713a9db2d57f0e5722225baf5b5/aws/client/default_retryer.go#L49C2-L49C29 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by adding doc comment. |
||
MinThrottleDelay: client.DefaultRetryerMinThrottleDelay, | ||
MaxThrottleDelay: 1200 * time.Millisecond, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package sqsevent_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-node-termination-handler/pkg/monitor/sqsevent" | ||
h "github.com/aws/aws-node-termination-handler/pkg/test" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/client" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
) | ||
|
||
type temporaryError struct { | ||
error | ||
temp bool | ||
} | ||
|
||
func TestGetSqsClient(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test case seems unnecessary because it's testing an implementation detail rather than behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would keep test case to test custom retryer config is consumed or not. |
||
retryer := getSqsRetryer(t) | ||
|
||
h.Equals(t, client.DefaultRetryerMaxNumRetries, retryer.NumMaxRetries) | ||
h.Equals(t, time.Duration(1200*time.Millisecond), retryer.MaxRetryDelay) | ||
} | ||
|
||
func TestShouldRetry(t *testing.T) { | ||
retryer := getSqsRetryer(t) | ||
|
||
testCases := []struct { | ||
name string | ||
req *request.Request | ||
shouldRetry bool | ||
}{ | ||
{ | ||
name: "AWS throttling error", | ||
req: &request.Request{ | ||
Error: awserr.New("ThrottlingException", "Rate exceeded", nil), | ||
}, | ||
shouldRetry: true, | ||
}, | ||
{ | ||
name: "AWS validation error", | ||
req: &request.Request{ | ||
Error: awserr.New("ValidationError", "Invalid parameter", nil), | ||
}, | ||
shouldRetry: false, | ||
}, | ||
{ | ||
name: "read connection reset by peer error", | ||
req: &request.Request{ | ||
Error: &temporaryError{ | ||
error: &net.OpError{ | ||
Op: "read", | ||
Err: fmt.Errorf("read: connection reset by peer"), | ||
}, | ||
temp: false, | ||
}}, | ||
shouldRetry: true, | ||
}, | ||
{ | ||
name: "read unknown error", | ||
req: &request.Request{ | ||
Error: &temporaryError{ | ||
error: &net.OpError{ | ||
Op: "read", | ||
Err: fmt.Errorf("read unknown error"), | ||
}, | ||
temp: false, | ||
}}, | ||
shouldRetry: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result := retryer.ShouldRetry(tc.req) | ||
h.Equals(t, tc.shouldRetry, result) | ||
}) | ||
} | ||
} | ||
|
||
func getSqsRetryer(t *testing.T) sqsevent.SqsRetryer { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String("us-east-1"), | ||
}) | ||
h.Ok(t, err) | ||
|
||
sqsClient := sqsevent.GetSqsClient(sess) | ||
h.Assert(t, sqsClient.Client.Config.Region != nil, "Region should not be nil") | ||
h.Equals(t, "us-east-1", *sqsClient.Client.Config.Region) | ||
|
||
retryer, ok := sqsClient.Client.Config.Retryer.(sqsevent.SqsRetryer) | ||
h.Assert(t, ok, "Retryer should be of type SqsRetryer") | ||
return retryer | ||
} | ||
|
||
func (e *temporaryError) Temporary() bool { | ||
return e.temp | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also add retry for asg client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could keep use default retryer for asg client.