Author: bryanduxbury
Date: Fri Sep 17 20:17:21 2010
New Revision: 998303
URL: http://svn.apache.org/viewvc?rev=998303&view=rev
Log:
THRIFT-899. rb: Ruby read timeouts can sometimes be 2x what they should be
This patch makes sure that we don't wait longer than necessary for timeouts.
Patch: Ryan King
Modified:
incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
incubator/thrift/trunk/lib/rb/spec/socket_spec_shared.rb
Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb?rev=998303&r1=998302&r2=998303&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb Fri Sep 17
20:17:21 2010
@@ -97,12 +97,13 @@ module Thrift
data = @handle.readpartial(sz)
else
# it's possible to interrupt select for something other than the
timeout
- # so we need to ensure we've waited long enough
+ # so we need to ensure we've waited long enough, but not too long
start = Time.now
- rd = nil # scoping
- loop do
- rd, = IO.select([...@handle], nil, nil, @timeout)
- break if (rd and not rd.empty?) or Time.now - start >= @timeout
+ timespent = 0
+ rd = loop do
+ rd, = IO.select([...@handle], nil, nil, @timeout - timespent)
+ timespent = Time.now - start
+ break rd if (rd and not rd.empty?) or timespent >= @timeout
end
if rd.nil? or rd.empty?
raise TransportException.new(TransportException::TIMED_OUT,
"Socket: Timed out reading #{sz} bytes from #...@desc}")
Modified: incubator/thrift/trunk/lib/rb/spec/socket_spec_shared.rb
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/socket_spec_shared.rb?rev=998303&r1=998302&r2=998303&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/socket_spec_shared.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/socket_spec_shared.rb Fri Sep 17
20:17:21 2010
@@ -91,7 +91,7 @@ shared_examples_for "a socket" do
it "should raise an error when read times out" do
@socket.timeout = 0.5
@socket.open
- IO.should_receive(:select).with([...@handle], nil, nil,
0.5).at_least(1).times.and_return(nil)
+ IO.should_receive(:select).once {sleep(0.5); nil}
lambda { @socket.read(17) }.should raise_error(Thrift::TransportException)
{ |e| e.type.should == Thrift::TransportException::TIMED_OUT }
end