/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
- require "redis/connection/registry"
- require "redis/errors"
- require "hiredis/connection"
- require "timeout"
- class Redis
- module Connection
- class Hiredis
- def self.connect(config)
- connection = ::Hiredis::Connection.new
- if config[:scheme] == "unix"
- connection.connect_unix(config[:path], Integer(config[:timeout] * 1_000_000))
- else
- connection.connect(config[:host], config[:port], Integer(config[:timeout] * 1_000_000))
- end
- instance = new(connection)
- instance.timeout = config[:timeout]
- instance
- rescue Errno::ETIMEDOUT
- raise TimeoutError
- end
- def initialize(connection)
- @connection = connection
- end
- def connected?
- @connection && @connection.connected?
- end
- def timeout=(timeout)
- # Hiredis works with microsecond timeouts
- @connection.timeout = Integer(timeout * 1_000_000)
- end
- def disconnect
- @connection.disconnect
- @connection = nil
- end
- def write(command)
- @connection.write(command.flatten(1))
- rescue Errno::EAGAIN
- raise TimeoutError
- end
- def read
- reply = @connection.read
- reply = CommandError.new(reply.message) if reply.is_a?(RuntimeError)
- reply
- rescue Errno::EAGAIN
- raise TimeoutError
- rescue RuntimeError => err
- raise ProtocolError.new(err.message)
- end
- end
- end
- end
- Redis::Connection.drivers << Redis::Connection::Hiredis