Automatically close request/response bodies when they are decoded #85
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Request.Decode()
andResponse.Decode()
are convenience methods that we use extensively for unmarshalling HTTP bodies. These are not suitable as methods that can be called repeatedly to yield multiple values in a streaming fashion. These methods' implementations however, usedjson.Decoder
internally, which is meant for streaming use.The problem with this is that
json.Decoder
may not read the entire stream: quite reasonably it reads only as much as it needs in order to yield a value, and presumes the stream is infinite. The way this was used inside.Decode()
meant that a response body may never get closed. That's because it's common for a response body to be terminated with new line (\n
) character, which theDecoder
may leave unread. This would mean that thedoneReader
wouldn't see the body as being fully-read and leave it open.This switches the implementations of
.Decode()
to usejson.Unmarshal
, which is not streaming. Instead of reading only chunks of the body, these methods now call.BodyBytes()
, which explicitly closes the underlying reader.