Repository navigation
return partial results early - #378
Conversation
When using Excon with a service that streams responses chunk by chunk, it currently blocks and waits until either chunk_size is reached or the full response is received. For some services, it is helpful to get partial results early and act on them (e.g. tickers or long-running requests). Socket#read will raise an error when the user tries to read before the buffer is ready. This is normal with chunky responses. So when the read jumps into the rescue clause, check if something is already in the buffer. If so, don't wait and retry, but deliver the result first and retry after. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
|
Closing for now. I realized this needs one more adjustment when used in a blocking way: In case of chunky response, it doesn't block but delivers only the partial result. It works as expcted when used in a block: |
|
Fixed the issue with blocking calls: The body is now read in full before the result is returned. |
…f just the first chunk Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
There was a problem hiding this comment.
Shouldn't we try to read again even if we didn't read anything?
There was a problem hiding this comment.
Yes, I believe that's what this code does: if the buffer.length == 0, i.e. if we didn't read anything, then we want to read again (with the code that follows below, i.e. IO.select + timeout). The buffer will only be empty, if the above read_nonblock didn't read anything. In that case we don't need to call the response block, since we have nothing to deliver yet.
The other way around: if we did read something, we want to return that result quickly. For example you get back to response.rb in line 104 with a response block (or 108 without), which will deliver the partial result and then try to read from the socket again until it breaks (EOF or failure).
There was a problem hiding this comment.
Ah ok, yeah I think that clarifies.
|
Sorry for the delay on review. Wanted to think about it a bit and ensure I reviewed it when I had a bit of time to be careful since the impact of changing things around socket#read could be pretty large. Added a comment/question above. Thanks! |
|
Sorry for my late response! Comment inline, and I'm back with more time now ;) |
|
I completely understand your hesitation to merge this, socket#read is a crucial one to get right. I hope we can add support for this type of chunking reads to Excon; I love to use it! |
|
No problem, thanks for getting back to me and being patient/understanding. Overall I think this looks good, but it is high risk. I think adding some tests around it as you suggest would be a great way to mitigate that risk (and avoid regressions). Would you be up for fleshing that out? Thanks! |
|
Sure, I will add some tests over weekend. Cheers |
|
Tiny progress update: My usual way with intercepting "rack.hijack" headers doesn't work with the test server. I'm looking for a way to do it via EM. Basically: send response + flush. |
|
Alright. Not super familiar with hijack myself, but do let me know if I can help. |
Needed to intercept server responses via rack.hijack. It is required to create asynchronous responses in Unicorn, which added support back in 4.6.0. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
unicorn tests are only used in the test suite to run the unix socket tests so far. Since Webrick can't handle rack.hijack headers and thus streaming a response, but unicorn can, make unicorn available for simple tests Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
unicorn by default will load a stack of middleware, including a standard rack layer for chunked responses. This gets in the way of streamed responses, which don't operate with the syntax of Transfer-Encoding: chunked. Unless it is required later on, disable the middleware layer here. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
Running streaming tests with unicorn 4.6.3 produces some unexplained errors with the test suite. Updating solved these issues. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
add a test for blocking calls against streamy endpoints. the result must block and only deliver once all parts have been delivered. add another test with a response_block, that should get each piece of the response at appropriate timing intervals. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
since they depend on unicode and that doesn't work right now. use an alternative server later on, that supports rack.hijack as well as java. Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
|
Got lucky: I realized unicorn was already configured for tests alongside rackup/webrick. Since 4.6 and after updating rack to 1.5 the rack.hijack feature became available for nice response streaming. Made a few updates (rack+unicorn) and adjusted the unicorn runner to make it work with regular http/netsocket tests (+ removed middlewares). Like other unicorn tests it doesn't work with JRuby. The tests simulate streaming with blocking and response_block calls. I also did some negative testing to make sure both fail without streaming or the bugfix. I don't know what's going on with travis rubinius tests right now. It looks like rubysl is missing (though that error didn't pop up before). The rest works as expected. |
Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
|
@geemus: That about covers it. What do you think? Note: In all of these test runs I saw one unrelated anomaly in SSL on ruby 1.9.2 which is not reproducible (tried a few times). I think it may be worth looking into... |
|
That 1.9.2 broken pipe issue crops up now and again, haven't really ever pinned it down. Improving the rubinius tests sounds good, but a separate pull sounds like a good plan. I'll review the rest presently |
|
I worry a little that the timing based tests could be brittle, but otherwise this seems good. I'll bring it in and we can just keep an eye on how stable the tests are over time. If you want to help out with the rubinius stuff you mentioned that would be awesome. Thanks! |
|
Awesome, thank you! I'm already taking a look at rubinius and am happy to help out here. I'll open a separate issue with some findings to tackle this one. Timing tests: Fully agree; I hope they will stay stable due to ~1 second gates, but let's keep watching. |
|
Sounds good. Hopefully this should resolve the initial issue you were having above as well. Thanks for the patch! |
When using Excon with a service that streams responses chunk by chunk, it currently blocks and waits until either chunk_size is reached or the full response is received. For some services, it is helpful to get partial results early and act on them (e.g. tickers or long-running requests).
Socket#read will raise an error when the user tries to read before the buffer is ready. This is normal with chunky responses. So when the read jumps into the rescue clause, check if something is already in the buffer. If so, don't wait and retry, but deliver the result first and retry after.
This should solve #350 as well.
Signed-off-by: Dominik Richter dominik.richter@gmail.com