Add support for streaming on requests (and add better handling for streaming in responses) #2734
Labels
breaking change
Change that proposes a non-backward compatible breaking change
proposal
Proposed Specification or API change
Related issue - my apologies if this is a double post - I wasn't sure how to make sure the discussion was being viewed by the appropriate people: #1243
Problem
Feign currently uses a byte[] when processing the request body. This does not work for transfers of large request bodies (for example, uploading large files).
Requested solution
Make request bodies be designed around streams.
Status/Findings
I just finished up creating a bunch of shims to make streaming requests work in Feign, and would like to see if ya'll would be open to having a discussion about how to make this part of the main library. This works, but it is ugly.
Doing this properly will require a breaking change, so I am hesitant to create a pull request without some good discussion about the best way to do it, where we can and can't break API, etc...
Possible Approach
Based on the workaround implementation I created, I believe that the general approach will be as follows:
First, the existing Response.Body class is perfectly suitable for handing both Request and Responses. It actually looks like Response.Body was designed with this in mind. I think that refactoring this into a standalone Body interface, with concrete implementations for ByteArrayBody, InputStreamBody, NoContentBody, and possibly a FileContentBody. I also suggest removing calls specific to encoding, etc... from this class. I would like to get rid of null checks on the body parameter and just use a no-op NoContentBody if possible.
Next, there will be some surgical changes to Client.Default (for requests, we need to write a stream instead of a byte[])
There are some questions about calling close() (or not calling close() ) at the end of the response.
Another big impact area is going to be in logger implementations (which need to either not do body logging on streamed bodies, or need to use a different approach that monitors the stream but does not actually read the stream as part of the logging call).
And I think there should be some discussion about supporting InputStream as a body parameter type, and possibly OutputStream as a client method response parameter type. Maybe these could be handled with specialized encoder/decoder implementations, but it's worth discussion about whether these should be part of the core library. Might also discuss a File type as a body parameter type (this would allow setting content-length header and make the body retryable, which would worthwhile.
Finally, all of my work has been on the synchronous implementations - so evaluation with respect to Async will be needed.
Conclusion
All in all, while the design discussion will be important, I am confident that it is possible to make this change without massive amounts of surgery. And I think this would be a huge enhancement to Feign. This is a huge gap with Jax-RS that would be good to close.
Is there interest in pursuing this? I'm happy to do the grunt work on the pull request, but I think this is a big enough breaking change that there should be discussion to get the design right before I start slinging code.
The text was updated successfully, but these errors were encountered: