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

Conversation

openmohan
Copy link

Fixes #9

@openmohan openmohan changed the title Draft: retry with request body and redirection retry with request body and redirection Dec 13, 2023
Copy link
Member

@mna mna left a comment

Choose a reason for hiding this comment

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

Hey Mohan,

Thanks for the contribution! There a small change needed to return a copy of the body (and not the same one, which would be unreadable once consumed). Other than that it looks good to me!

@@ -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 :)

@mna
Copy link
Member

mna commented Dec 19, 2023

Closing as per the discussion. Thanks again @openmohan !

@mna mna closed this Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants