Skip to content

Commit

Permalink
service/ec2: Fix max retries with client customizations
Browse files Browse the repository at this point in the history
Fixes #3374 by correcting the EC2 API client's customization for
ModifyNetworkInterfaceAttribute and AssignPrivateIpAddresses operations
to use the aws.Config.MaxRetries value if set. Previously the API client's
customizations would ignore MaxRetries specified in the SDK's
aws.Config.MaxRetries field.
  • Loading branch information
jasdel committed Aug 7, 2020
1 parent b811ea8 commit 804c113
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion service/ec2/customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ func init() {

// only set the retryer on request if config doesn't have a retryer
if r.Config.Retryer == nil && (r.Operation.Name == opModifyNetworkInterfaceAttribute || r.Operation.Name == opAssignPrivateIpAddresses) {
maxRetries := client.DefaultRetryerMaxNumRetries
if m := r.Config.MaxRetries; m != nil && *m != aws.UseServiceDefaultRetries {
maxRetries = *m
}
r.Retryer = client.DefaultRetryer{
NumMaxRetries: client.DefaultRetryerMaxNumRetries,
NumMaxRetries: maxRetries,
MinRetryDelay: customRetryerMinRetryDelay,
MinThrottleDelay: customRetryerMinRetryDelay,
MaxRetryDelay: customRetryerMaxRetryDelay,
Expand Down
57 changes: 57 additions & 0 deletions service/ec2/customizations_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package ec2_test

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
sdkclient "github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting/unit"
"github.com/aws/aws-sdk-go/service/ec2"
)
Expand Down Expand Up @@ -46,3 +51,55 @@ func TestCopySnapshotPresignedURL(t *testing.T) {
t.Errorf("expect %v to match, got %v", r.String(), u)
}
}

func TestNoCustomRetryerWithMaxRetries(t *testing.T) {
cases := map[string]struct {
Config aws.Config
ExpectMaxRetries int
}{
"With custom retrier": {
Config: aws.Config{
Retryer: sdkclient.DefaultRetryer{
NumMaxRetries: 10,
},
},
ExpectMaxRetries: 10,
},
"with max retries": {
Config: aws.Config{
MaxRetries: aws.Int(10),
},
ExpectMaxRetries: 10,
},
"no options set": {
ExpectMaxRetries: sdkclient.DefaultRetryerMaxNumRetries,
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
client := ec2.New(unit.Session, &aws.Config{
DisableParamValidation: aws.Bool(true),
}, c.Config.Copy())
client.ModifyNetworkInterfaceAttributeWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
client.AssignPrivateIpAddressesWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
})
}

}

func checkRetryerMaxRetries(t *testing.T, maxRetries int) func(*request.Request) {
return func(r *request.Request) {
r.Handlers.Send.Clear()
r.Handlers.Send.PushBack(func(rr *request.Request) {
if e, a := maxRetries, rr.Retryer.MaxRetries(); e != a {
t.Errorf("%s, expect %v max retries, got %v", rr.Operation.Name, e, a)
}
rr.HTTPResponse = &http.Response{
StatusCode: 200,
Header: http.Header{},
Body: ioutil.NopCloser(&bytes.Buffer{}),
}
})
}
}

0 comments on commit 804c113

Please sign in to comment.