From 9072d9c30c16b050b35d6a5d2694b7e6b665919a Mon Sep 17 00:00:00 2001 From: skotambkar Date: Mon, 16 Dec 2019 22:20:13 -0800 Subject: [PATCH] Reduces timeout and number of max retries for EC2Metadata client --- CHANGELOG_PENDING.md | 3 +++ aws/ec2metadata/api_test.go | 13 ++++++++++--- aws/ec2metadata/service.go | 4 +++- aws/ec2metadata/service_test.go | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 8a1927a39ca..1ddd7be8b02 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -3,3 +3,6 @@ ### SDK Enhancements ### SDK Bugs +* `aws/ec2metadata` : Reduces request timeout for EC2Metadata client along with maximum number of retries ([#3066](https://github.com/aws/aws-sdk-go/pull/3066)) + * Reduces latency while fetching response from EC2Metadata client running in a container to around 3 seconds + * Fixes [#2972](https://github.com/aws/aws-sdk-go/issues/2972) diff --git a/aws/ec2metadata/api_test.go b/aws/ec2metadata/api_test.go index 2d1abac4f4c..9139cc424f6 100644 --- a/aws/ec2metadata/api_test.go +++ b/aws/ec2metadata/api_test.go @@ -997,9 +997,12 @@ func TestRequestTimeOut(t *testing.T) { c.Handlers.Complete.PushBack(op.addToOperationPerformedList) + start := time.Now() resp, err := c.GetMetadata("/some/path") - expectedOperationsPerformed := []string{"GetToken", "GetMetadata"} + if e, a := 1*time.Second, time.Since(start); e < a { + t.Fatalf("expected duration of test to be less than %v, got %v", e, a) + } if e, a := "IMDSProfileForSDKGo", resp; e != a { t.Fatalf("Expected %v, got %v", e, a) @@ -1009,13 +1012,16 @@ func TestRequestTimeOut(t *testing.T) { t.Fatalf("Expected no error, got %v", err) } + expectedOperationsPerformed := []string{"GetToken", "GetMetadata"} if e, a := expectedOperationsPerformed, op.operationsPerformed; !reflect.DeepEqual(e, a) { t.Fatalf("expect %v operations, got %v", e, a) } + start = time.Now() resp, err = c.GetMetadata("/some/path") - - expectedOperationsPerformed = []string{"GetToken", "GetMetadata", "GetMetadata"} + if e, a := 1*time.Second, time.Since(start); e < a { + t.Fatalf("expected duration of test to be less than %v, got %v", e, a) + } if e, a := "IMDSProfileForSDKGo", resp; e != a { t.Fatalf("Expected %v, got %v", e, a) @@ -1025,6 +1031,7 @@ func TestRequestTimeOut(t *testing.T) { t.Fatalf("Expected no error, got %v", err) } + expectedOperationsPerformed = []string{"GetToken", "GetMetadata", "GetMetadata"} if e, a := expectedOperationsPerformed, op.operationsPerformed; !reflect.DeepEqual(e, a) { t.Fatalf("expect %v operations, got %v", e, a) } diff --git a/aws/ec2metadata/service.go b/aws/ec2metadata/service.go index 8eccac05aa2..b8b2940d744 100644 --- a/aws/ec2metadata/service.go +++ b/aws/ec2metadata/service.go @@ -80,8 +80,10 @@ func NewClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegio // use a shorter timeout than default because the metadata // service is local if it is running, and to fail faster // if not running on an ec2 instance. - Timeout: 5 * time.Second, + Timeout: 1 * time.Second, } + // max number of retries on the client operation + cfg.MaxRetries = aws.Int(2) } svc := &EC2Metadata{ diff --git a/aws/ec2metadata/service_test.go b/aws/ec2metadata/service_test.go index 8e625336473..bd94856fd5a 100644 --- a/aws/ec2metadata/service_test.go +++ b/aws/ec2metadata/service_test.go @@ -24,7 +24,7 @@ func TestClientOverrideDefaultHTTPClientTimeout(t *testing.T) { t.Errorf("expect %v, not to equal %v", e, a) } - if e, a := 5*time.Second, svc.Config.HTTPClient.Timeout; e != a { + if e, a := 1*time.Second, svc.Config.HTTPClient.Timeout; e != a { t.Errorf("expect %v to be %v", e, a) } }