Skip to content

return partial results early - #378

Merged
geemus merged 10 commits into
excon:masterfrom
arlimus:chunky-response
May 13, 2014
Merged

geemus merged 10 commits into
excon:masterfrom
arlimus:chunky-response

Conversation

@arlimus

@arlimus arlimus commented Apr 18, 2014

Copy link
Copy Markdown
Contributor

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

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>
@arlimus

arlimus commented Apr 19, 2014

Copy link
Copy Markdown
Contributor Author

Closing for now. I realized this needs one more adjustment when used in a blocking way:

res = Excon.get( url )

In case of chunky response, it doesn't block but delivers only the partial result.

It works as expcted when used in a block:

Excon.get( url, response_block: lambda {|c,r,t| ... })

@arlimus arlimus closed this Apr 19, 2014
@arlimus arlimus reopened this Apr 19, 2014
@arlimus

arlimus commented Apr 19, 2014

Copy link
Copy Markdown
Contributor Author

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>
Comment thread lib/excon/socket.rb Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we try to read again even if we didn't read anything?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, yeah I think that clarifies.

@geemus

geemus commented May 1, 2014

Copy link
Copy Markdown
Contributor

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!

@arlimus

arlimus commented May 8, 2014

Copy link
Copy Markdown
Contributor Author

Sorry for my late response! Comment inline, and I'm back with more time now ;)
Btw: I was thinking about adding a test for these special types of chunking reads. What do you think? (Would also help demonstrate this)

@arlimus

arlimus commented May 8, 2014

Copy link
Copy Markdown
Contributor Author

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!

@geemus

geemus commented May 9, 2014

Copy link
Copy Markdown
Contributor

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!

@arlimus

arlimus commented May 9, 2014

Copy link
Copy Markdown
Contributor Author

Sure, I will add some tests over weekend. Cheers

@arlimus

arlimus commented May 12, 2014

Copy link
Copy Markdown
Contributor Author

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.

@geemus

geemus commented May 12, 2014

Copy link
Copy Markdown
Contributor

Alright. Not super familiar with hijack myself, but do let me know if I can help.

arlimus added 7 commits May 13, 2014 02:16
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>
@arlimus

arlimus commented May 13, 2014

Copy link
Copy Markdown
Contributor Author

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>
@arlimus

arlimus commented May 13, 2014

Copy link
Copy Markdown
Contributor Author

@geemus: That about covers it. What do you think?
Regarding travis+rubinius issues: It can be patched, but maybe not in scope of this issue? (whichever way you prefer)

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...

@geemus

geemus commented May 13, 2014

Copy link
Copy Markdown
Contributor

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

@geemus

geemus commented May 13, 2014

Copy link
Copy Markdown
Contributor

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!

geemus added a commit that referenced this pull request May 13, 2014
@geemus
geemus merged commit d17933e into excon:master May 13, 2014
@arlimus

arlimus commented May 13, 2014

Copy link
Copy Markdown
Contributor Author

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.

@arlimus
arlimus deleted the chunky-response branch May 13, 2014 14:56
@geemus

geemus commented May 13, 2014

Copy link
Copy Markdown
Contributor

Sounds good. Hopefully this should resolve the initial issue you were having above as well. Thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants