zhongwencool/maxwell

Wrap asynchronous responses with Stream

secretworry opened this issue · 1 comments

Here's the talk on using Stream to wrap the async response.

For now, the async response is assembled by the maxwell to form a whole block before actually respond to the user. While the async interfaces of underlying adapters are suppose to be used for the streaming scenarios.

So naturally, we are suppose to wrap the response with a Stream.

This should be fairly easy to implement, but I think there are some associated changes involved as well. Namely deprecating get_resp_body/2, as it won't work with this change, and I believe it is error-prone anyway, not to mention the fact that users can trivially implement what it does, and more correctly for their use case, in their own code. get_resp_body/1 can then ensure that a binary is always returned (or whatever datastructure it was decoded into by one of the middlewares). If it's a Stream when get_resp_body/1 is called, it can evaluate the stream and return the binary, otherwise it can return a binary or other datastructure as it does today. Likewise, we should update the JSON middlewares to evaluate the Stream and parse it (I believe Poison already supports streamed parsing, so it may be a simple change), but there's the question of custom parsing functions to think about.