PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/redis-3.0.7/lib/redis/connection/hiredis.rb

https://github.com/delowong/logstash
Ruby | 63 lines | 51 code | 11 blank | 1 comment | 4 complexity | 1bdca8b977cbb788abded9a1a181500a MD5 | raw file
 1require "redis/connection/registry"
 2require "redis/errors"
 3require "hiredis/connection"
 4require "timeout"
 5
 6class Redis
 7  module Connection
 8    class Hiredis
 9
10      def self.connect(config)
11        connection = ::Hiredis::Connection.new
12
13        if config[:scheme] == "unix"
14          connection.connect_unix(config[:path], Integer(config[:timeout] * 1_000_000))
15        else
16          connection.connect(config[:host], config[:port], Integer(config[:timeout] * 1_000_000))
17        end
18
19        instance = new(connection)
20        instance.timeout = config[:timeout]
21        instance
22      rescue Errno::ETIMEDOUT
23        raise TimeoutError
24      end
25
26      def initialize(connection)
27        @connection = connection
28      end
29
30      def connected?
31        @connection && @connection.connected?
32      end
33
34      def timeout=(timeout)
35        # Hiredis works with microsecond timeouts
36        @connection.timeout = Integer(timeout * 1_000_000)
37      end
38
39      def disconnect
40        @connection.disconnect
41        @connection = nil
42      end
43
44      def write(command)
45        @connection.write(command.flatten(1))
46      rescue Errno::EAGAIN
47        raise TimeoutError
48      end
49
50      def read
51        reply = @connection.read
52        reply = CommandError.new(reply.message) if reply.is_a?(RuntimeError)
53        reply
54      rescue Errno::EAGAIN
55        raise TimeoutError
56      rescue RuntimeError => err
57        raise ProtocolError.new(err.message)
58      end
59    end
60  end
61end
62
63Redis::Connection.drivers << Redis::Connection::Hiredis