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

retry with request body and redirection #10

Closed
Closed
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
3 changes: 3 additions & 0 deletions rehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
for {
var cancel context.CancelFunc = func() {} // empty unless a timeout is set
reqWithTimeout := req
reqWithTimeout.GetBody = func() (io.ReadCloser, error) {
return req.Body, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. It works with the test because the body is not consumed in the redirects (only in the retries), but the Go documentation for the GetBody field mentions:

// GetBody defines an optional func to return a new copy of
// Body. It is used for client requests when a redirect requires
// reading the body more than once. Use of GetBody still
// requires setting Body.

Returning req.Body would not allow reading the body more than once. I think it should do similar to what is done on line 363 below when a retry is needed, reset the bytes.Reader to the beginning and create a new nop closer:

if _, err := br.Seek(0, 0); err != nil {
	return nil, err
}
return ioutil.NopCloser(br), nil

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mna In the (m *mockRoundTripper) RoundTrip we are consuming the body.

I think the issue itself is invalid.

  1. The redirect is on the http.Client level. The rehttp does not have to follow a redirect.
  2. It is the responsibility of the request creator to set the GetBody, although by default go provides support to common readers
  3. And for every retry, we seek the cursor to start of the body.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@openmohan

I think the issue itself is invalid.

You mean #9, i.e. there's nothing to fix in rehttp regarding GetBody ? If so I think you may be right. The reason the other Go package had to make changes regarding this was because AIUI it creates a new request, but rehttp does not create new requests, it uses the one provided by the caller. As you mention, it is the caller's responsibility to set GetBody, and for retries rehttp already handles that part.

And actually setting the GetBody field in rehttp would possibly overwrite whatever the caller did set, which would be wrong.

I'll link to this discussion in the issue #9 in case the person that created the issue has more input for this, but otherwise I think I'll just close it (and the PR) in a while if I don't hear back from them. Let me know if I misunderstood what you meant, though!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct @mna :)

}
if t.PerAttemptTimeout != 0 {
reqWithTimeout, cancel = getPerAttemptTimeoutInfo(ctx, req, t.PerAttemptTimeout)
}
Expand Down
83 changes: 66 additions & 17 deletions rehttp_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,73 @@ func TestMockClientPreventRetryWithBody(t *testing.T) {
}

func TestMockClientRetryWithBody(t *testing.T) {
retFn := func(att int, req *http.Request) (*http.Response, error) {
return nil, tempErr{}
newRequest := func(body io.Reader) *http.Request {
req, err := http.NewRequest("POST", "http://example.com", body)
assert.NoError(t, err)
req.Header.Set("Content-Type", "text/plain")
return req
}

tests := []struct {
name string
req *http.Request
retFn func(att int, req *http.Request) (*http.Response, error)
retries int
requestBodies []string
err error
}{
{
name: "temp-error",
req: newRequest(strings.NewReader("hello")),
retFn: func(att int, req *http.Request) (*http.Response, error) {
return nil, tempErr{}
},
err: tempErr{},
retries: 4,
requestBodies: []string{"hello", "hello", "hello", "hello"},
},
{
name: "307-redirect",
req: newRequest(strings.NewReader("hello")),
retFn: func(att int, req *http.Request) (*http.Response, error) {
if att >= 1 {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))}, nil
}
return &http.Response{StatusCode: 307, Body: io.NopCloser(strings.NewReader(""))}, nil
},
retries: 4,
requestBodies: []string{"hello", "hello", "hello", "hello"},
},
{
name: "empty-body",
req: newRequest(nil),
retFn: func(att int, req *http.Request) (*http.Response, error) {
if att >= 1 {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))}, nil
}
return &http.Response{StatusCode: 307, Body: io.NopCloser(strings.NewReader(""))}, nil
},
retries: 4,
requestBodies: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
mock := &mockRoundTripper{t: t, retFn: tt.retFn}

tr := NewTransport(mock, RetryAny(RetryMaxRetries(3), RetryStatusInterval(300, 500)), ConstDelay(0))

client := &http.Client{
Transport: tr,
}
_, err := client.Do(tt.req)
assert.ErrorIs(t, err, tt.err)
assert.Equal(t, tt.retries, mock.Calls())
assert.Equal(t, tt.requestBodies, mock.Bodies())
})
}
mock := &mockRoundTripper{t: t, retFn: retFn}

tr := NewTransport(mock, RetryAll(RetryMaxRetries(1), RetryTemporaryErr()), ConstDelay(0))

client := &http.Client{
Transport: tr,
}
_, err := client.Post("http://example.com", "text/plain", strings.NewReader("hello"))
if assert.NotNil(t, err) {
uerr, ok := err.(*url.Error)
require.True(t, ok)
assert.Equal(t, tempErr{}, uerr.Err)
}
assert.Equal(t, 2, mock.Calls())
assert.Equal(t, []string{"hello", "hello"}, mock.Bodies())
}

func TestMockClientRetryTimeout(t *testing.T) {
Expand Down