Sha256: 38eac87a924e89f1ccc95dfc7a6c0e382e3206be90949b6b14776a03b5099929

Contents?: true

Size: 1.5 KB

Versions: 15

Compression:

Stored size: 1.5 KB

Contents

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

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
redis-3.2.0 lib/redis/connection/hiredis.rb
redis-3.1.0 lib/redis/connection/hiredis.rb
redis-3.0.7 lib/redis/connection/hiredis.rb
redis-3.0.6 lib/redis/connection/hiredis.rb
redis-3.0.5 lib/redis/connection/hiredis.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/redis-3.0.4/lib/redis/connection/hiredis.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/redis-3.0.4/lib/redis/connection/hiredis.rb
redis-3.0.4 lib/redis/connection/hiredis.rb
redis-3.0.3 lib/redis/connection/hiredis.rb
remq-0.0.4 vendor/bundle/gems/redis-3.0.2/lib/redis/connection/hiredis.rb
remq-0.0.3 vendor/bundle/gems/redis-3.0.2/lib/redis/connection/hiredis.rb
redis-3.0.2 lib/redis/connection/hiredis.rb
redis-3.0.1 lib/redis/connection/hiredis.rb
redis-3.0.0 lib/redis/connection/hiredis.rb
redis-3.0.0.rc2 lib/redis/connection/hiredis.rb