Skip to content

Commit

Permalink
Fixing bug aws#947
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Dec 6, 2016
1 parent 173fcf0 commit f65bfa8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
14 changes: 13 additions & 1 deletion aws/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -297,7 +298,18 @@ func (r *Request) Send() error {

r.Handlers.Send.Run(r)
if r.Error != nil {
if strings.Contains(r.Error.Error(), "net/http: request canceled") {
awsErr, ok := r.Error.(awserr.Error)
timeoutErr := false
if ok {
netErr, ok := awsErr.OrigErr().(net.Error)
timeoutErr = ok && netErr.Timeout()
}

// There can be two types of canceled errors here.
// The first being a net.Error and the other being an error.
// If the request was timed out, we want to continue the retry
// process. Otherwise, return the canceled error.
if !timeoutErr && strings.Contains(r.Error.Error(), "net/http: request canceled") {
return r.Error
}

Expand Down
41 changes: 41 additions & 0 deletions aws/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"runtime"
"testing"
"time"
Expand All @@ -16,6 +17,9 @@ import (

"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/client/metadata"
"github.com/aws/aws-sdk-go/aws/corehandlers"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
Expand Down Expand Up @@ -378,3 +382,40 @@ func TestRequestRecoverTimeoutWithNilResponse(t *testing.T) {
assert.Equal(t, 1, int(r.RetryCount))
assert.Equal(t, "valid", out.Data)
}

func TestRequestRetryClientTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Millisecond)
}))

var handlers request.Handlers
handlers.Send.PushBackNamed(corehandlers.SendHandler)
handlers.AfterRetry.PushBackNamed(corehandlers.AfterRetryHandler)

sendCount := 0
handlers.Send.PushFront(func(r *request.Request) {
sendCount++
})

numMaxRetries := 2
op := &request.Operation{
Name: "Test",
HTTPMethod: "GET",
HTTPPath: "/",
}
c := &http.Client{
Timeout: time.Millisecond,
}

config := aws.NewConfig().
WithHTTPClient(c).
WithSleepDelay(time.Sleep)
clientInfo := metadata.ClientInfo{Endpoint: ts.URL}
retryer := client.DefaultRetryer{NumMaxRetries: numMaxRetries}
req := request.New(*config, clientInfo, handlers, retryer, op, nil, nil)

err := req.Send()
assert.Error(t, err)
expectedSendCount := numMaxRetries + 1
assert.Equal(t, expectedSendCount, sendCount)
}

0 comments on commit f65bfa8

Please sign in to comment.