rehttp icon indicating copy to clipboard operation
rehttp copied to clipboard

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

Open virtuald opened this issue 6 years ago • 1 comments

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?

virtuald avatar Jan 31 '18 22:01 virtuald

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

mna avatar Feb 01 '18 03:02 mna