Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switch to using raw socket timeouts over buffered io (Linux only)

  • Loading branch information...
commit 607780f412e5416f831ed9a66e2e1b976fb2216e 1 parent 0cec209
@defunkt defunkt authored committed
View
1  lib/bertrpc.rb
@@ -8,4 +8,3 @@
require 'bertrpc/encodes'
require 'bertrpc/action'
require 'bertrpc/errors'
-require 'bertrpc/buffered_io'
View
17 lib/bertrpc/action.rb
@@ -44,7 +44,7 @@ def transaction(bert_request)
bert_response
rescue Errno::ECONNREFUSED
raise ConnectionError.new("Unable to connect to #{@svc.host}:#{@svc.port}")
- rescue Timeout::Error
+ rescue Errno::EAGAIN
raise ReadTimeoutError.new(@svc.host, @svc.port, @svc.timeout)
end
@@ -57,9 +57,18 @@ def transaction(bert_request)
# +port+ Integer port of the target TCP server
# +timeout+ Optional Integer (in seconds) of the read timeout
def connect_to(host, port, timeout = nil)
- io = BufferedIO.new(TCPSocket.new(host, port))
- io.read_timeout = timeout
- io
+ sock = TCPSocket.new(host, port)
+ sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
+
+ if timeout
+ secs = Integer(timeout)
+ usecs = Integer((timeout - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ end
+
+ sock
end
end
end
View
28 lib/bertrpc/buffered_io.rb
@@ -1,28 +0,0 @@
-module BERTRPC
- # Taken with love from memcache-client.
- #
- # See http://is.gd/4CWRA for the code and
- # http://is.gd/4CYde for the discussion.
- class BufferedIO < Net::BufferedIO # :nodoc:
- BUFSIZE = 1024 * 16
-
- if RUBY_VERSION < '1.9.1'
- def rbuf_fill
- begin
- @rbuf << @io.read_nonblock(BUFSIZE)
- rescue Errno::EWOULDBLOCK
- retry unless @read_timeout
- if IO.select([@io], nil, nil, @read_timeout)
- retry
- else
- raise Timeout::Error, 'IO timeout'
- end
- end
- end
- end
-
- def setsockopt(*args)
- @io.setsockopt(*args)
- end
- end
-end
View
2  test/action_test.rb
@@ -94,7 +94,7 @@ class ActionTest < Test::Unit::TestCase
io = stub()
io.expects(:write).with("\000\000\000\003")
io.expects(:write).with("foo")
- io.expects(:read).with(4).raises(Timeout::Error)
+ io.expects(:read).with(4).raises(Errno::EAGAIN)
@call.expects(:connect_to).returns(io)
begin
@call.transaction("foo")
Please sign in to comment.
Something went wrong with that request. Please try again.