Skip to content
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

aws/request: Add handling for retrying temporary errors during unmarshal #1289

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions aws/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,65 @@ func TestIsNoBodyReader(t *testing.T) {
}
}
}

func TestRequest_TemporaryRetry(t *testing.T) {
done := make(chan struct{})

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1024")
w.WriteHeader(http.StatusOK)

w.Write(make([]byte, 100))

f := w.(http.Flusher)
f.Flush()

<-done
}))

client := &http.Client{
Timeout: 100 * time.Millisecond,
}

svc := awstesting.NewClient(&aws.Config{
Region: unit.Session.Config.Region,
MaxRetries: aws.Int(1),
HTTPClient: client,
DisableSSL: aws.Bool(true),
Endpoint: aws.String(server.URL),
})

req := svc.NewRequest(&request.Operation{
Name: "name", HTTPMethod: "GET", HTTPPath: "/path",
}, &struct{}{}, &struct{}{})

req.Handlers.Unmarshal.PushBack(func(r *request.Request) {
defer req.HTTPResponse.Body.Close()
_, err := io.Copy(ioutil.Discard, req.HTTPResponse.Body)
r.Error = awserr.New(request.ErrCodeSerialization, "error", err)
})

err := req.Send()
if err == nil {
t.Errorf("expect error, got none")
}
close(done)

aerr := err.(awserr.Error)
if e, a := request.ErrCodeSerialization, aerr.Code(); e != a {
t.Errorf("expect %q error code, got %q", e, a)
}

if e, a := 1, req.RetryCount; e != a {
t.Errorf("expect %d retries, got %d", e, a)
}

type temporary interface {
Temporary() bool
}

terr := aerr.OrigErr().(temporary)
if !terr.Temporary() {
t.Errorf("expect temporary error, was not")
}
}
8 changes: 8 additions & 0 deletions aws/request/retryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ var validParentCodes = map[string]struct{}{
ErrCodeRead: struct{}{},
}

type temporaryError interface {
Temporary() bool
}

func isNestedErrorRetryable(parentErr awserr.Error) bool {
if parentErr == nil {
return false
Expand All @@ -92,6 +96,10 @@ func isNestedErrorRetryable(parentErr awserr.Error) bool {
return isCodeRetryable(aerr.Code())
}

if t, ok := err.(temporaryError); ok {
return t.Temporary()
}

return isErrConnectionReset(err)
}

Expand Down
52 changes: 49 additions & 3 deletions aws/request/retryer_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
package request

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws/awserr"
)

func TestRequestThrottling(t *testing.T) {
req := Request{}

req.Error = awserr.New("Throttling", "", nil)
assert.True(t, req.IsErrorThrottle())
if e, a := true, req.IsErrorThrottle(); e != a {
t.Errorf("expect %t to be throttled, was %t", e, a)
}
}

type mockTempError bool

func (e mockTempError) Error() string {
return fmt.Sprintf("mock temporary error: %t", e.Temporary())
}
func (e mockTempError) Temporary() bool {
return bool(e)
}

func TestIsErrorRetryable(t *testing.T) {
cases := []struct {
Err error
IsTemp bool
}{
{
Err: awserr.New(ErrCodeSerialization, "temporary error", mockTempError(true)),
IsTemp: true,
},
{
Err: awserr.New(ErrCodeSerialization, "temporary error", mockTempError(false)),
IsTemp: false,
},
{
Err: awserr.New(ErrCodeSerialization, "some error", errors.New("blah")),
IsTemp: false,
},
{
Err: awserr.New("SomeError", "some error", nil),
IsTemp: false,
},
{
Err: awserr.New("RequestError", "some error", nil),
IsTemp: true,
},
}

for i, c := range cases {
retryable := IsErrorRetryable(c.Err)
if e, a := c.IsTemp, retryable; e != a {
t.Errorf("%d, expect %t temporary error, got %t", i, e, a)
}
}
}