Permalink
Browse files

Use floating point timeout duration

This reduces back and forth conversion, and defaults to the format that
is easier to work with.
  • Loading branch information...
pietern committed Mar 13, 2012
1 parent 394c0e4 commit acb15818857956db0a5b28faec8c31d89da9df93
Showing with 18 additions and 27 deletions.
  1. +3 −13 lib/redis/client.rb
  2. +5 −4 lib/redis/connection/hiredis.rb
  3. +6 −6 lib/redis/connection/ruby.rb
  4. +4 −4 lib/redis/connection/synchrony.rb
View
@@ -191,10 +191,10 @@ def without_socket_timeout
connect unless connected?
begin
- self.timeout = 0
+ connection.timeout = 0
yield
ensure
- self.timeout = @timeout if connected?
+ connection.timeout = @timeout if connected?
end
end
@@ -231,30 +231,20 @@ def logging(commands)
end
def establish_connection
- # Need timeout in usecs, like socket timeout.
- timeout = Integer(@timeout * 1_000_000)
-
if @path
connection.connect_unix(@path, timeout)
else
connection.connect(@host, @port, timeout)
end
- # If the timeout is set we set the low level socket options in order
- # to make sure a blocking read will return after the specified number
- # of seconds. This hack is from memcached ruby client.
- self.timeout = @timeout
+ connection.timeout = @timeout
rescue Timeout::Error
raise CannotConnectError, "Timed out connecting to Redis on #{location}"
rescue Errno::ECONNREFUSED
raise CannotConnectError, "Error connecting to Redis on #{location} (ECONNREFUSED)"
end
- def timeout=(timeout)
- connection.timeout = Integer(timeout * 1_000_000)
- end
-
def ensure_connected
tries = 0
@@ -14,18 +14,19 @@ def connected?
@connection.connected?
end
- def timeout=(usecs)
- @connection.timeout = usecs
+ def timeout=(timeout)
+ # Hiredis works with microsecond timeouts
+ @connection.timeout = Integer(timeout * 1_000_000)
end
def connect(host, port, timeout)
- @connection.connect(host, port, timeout)
+ @connection.connect(host, port, Integer(timeout * 1_000_000))
rescue Errno::ETIMEDOUT
raise Timeout::Error
end
def connect_unix(path, timeout)
- @connection.connect_unix(path, timeout)
+ @connection.connect_unix(path, Integer(timeout * 1_000_000))
rescue Errno::ETIMEDOUT
raise Timeout::Error
end
@@ -23,14 +23,14 @@ def connected?
end
def connect(host, port, timeout)
- with_timeout(timeout.to_f / 1_000_000) do
+ with_timeout(timeout) do
@sock = TCPSocket.new(host, port)
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
end
end
def connect_unix(path, timeout)
- with_timeout(timeout.to_f / 1_000_000) do
+ with_timeout(timeout) do
@sock = UNIXSocket.new(path)
end
end
@@ -42,11 +42,11 @@ def disconnect
@sock = nil
end
- def timeout=(usecs)
- secs = Integer(usecs / 1_000_000)
- usecs = Integer(usecs - (secs * 1_000_000)) # 0 - 999_999
+ def timeout=(timeout)
+ tv_sec = Integer(timeout)
+ tv_usec = Integer((timeout - tv_sec) * 1_000_000) # 0 - 999_999
- optval = [secs, usecs].pack("l_2")
+ optval = [tv_sec, tv_usec].pack("l_2")
begin
@sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
@@ -61,21 +61,21 @@ class Synchrony
include Redis::Connection::CommandHelper
def initialize
- @timeout = 5_000_000
+ @timeout = 5.0
@connection = nil
end
def connected?
@connection && @connection.connected?
end
- def timeout=(usecs)
- @timeout = usecs
+ def timeout=(timeout)
+ @timeout = timeout
end
def connect(host, port, timeout)
conn = EventMachine.connect(host, port, RedisClient) do |c|
- c.pending_connect_timeout = [Float(timeout / 1_000_000), 0.1].max
+ c.pending_connect_timeout = [timeout, 0.1].max
end
setup_connect_callbacks(conn, Fiber.current)

0 comments on commit acb1581

Please sign in to comment.