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

Contemplate not buffering data if Body is NopCloser or some in-memory type? #3

Open
virtuald opened this issue Jan 31, 2018 · 1 comment

Comments

@virtuald
Copy link

It seems like if you could figure out whether the Body supported multiple reads (for example, an in-memory buffer), would save some copies.

Unfortunately, it seems that NopCloser isn't exported, but maybe you could export your own wrapper around it that could be used as an indicator?

@mna
Copy link
Member

mna commented Feb 1, 2018

Hello Dustin,

Unfortunately, with only the Request struct, there's not much that can be done to detect if the body is buffered, as for typical requests it will be wrapped in an unexported ioutil.NopCloser by the call to http.NewRequest, losing the type of the underlying reader. However, since Go 1.8, there's a new GetBody field on the Request that can be used to get a fresh copy of the body, saving the copying for the most usual in-memory body readers (bytes.Buffer/Reader, strings.Reader). For other types, it can be set by the caller. The request's body is also now set to the sentinel value http.NoBody when the body is non-nil but known to be empty, this should also be checked by rehttp.

Both of these new features should be checked before falling back on the current behaviour. The source code will need to be refactored a bit to use a Go1.8+ file and a pre-Go1.8 one.

Thanks for raising the idea, there's definitely something to be done to improve the package. Can't make any promises on when I'll have time to tackle this, though.

Martin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants