lib/redis/connection/synchrony.rb in redis-3.0.0.rc1 vs lib/redis/connection/synchrony.rb in redis-3.0.0.rc2

- old
+ new

@@ -58,34 +58,40 @@ end class Synchrony include Redis::Connection::CommandHelper - def initialize - @timeout = 5_000_000 - @connection = nil - end + def self.connect(config) + if config[:scheme] == "unix" + conn = EventMachine.connect_unix_domain(config[:path], RedisClient) + else + conn = EventMachine.connect(config[:host], config[:port], RedisClient) do |c| + c.pending_connect_timeout = [config[:timeout], 0.1].max + end + end - def connected? - @connection && @connection.connected? + fiber = Fiber.current + conn.callback { fiber.resume } + conn.errback { fiber.resume :refused } + + raise Errno::ECONNREFUSED if Fiber.yield == :refused + + instance = new(conn) + instance.timeout = config[:timeout] + instance end - def timeout=(usecs) - @timeout = usecs + def initialize(connection) + @connection = connection 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 - end - - setup_connect_callbacks(conn, Fiber.current) + def connected? + @connection && @connection.connected? end - def connect_unix(path, timeout) - conn = EventMachine.connect_unix_domain(path, RedisClient) - setup_connect_callbacks(conn, Fiber.current) + def timeout=(timeout) + @timeout = timeout end def disconnect @connection.close_connection @connection = nil @@ -103,27 +109,9 @@ elsif type == :error raise payload else raise "Unknown type #{type.inspect}" end - end - - private - - def setup_connect_callbacks(conn, f) - conn.callback do - @connection = conn - f.resume conn - end - - conn.errback do - @connection = conn - f.resume :refused - end - - r = Fiber.yield - raise Errno::ECONNREFUSED if r == :refused - r end end end end