Sha256: d5b1e4d01653db6a4fe91cc84ac4468863246d0f489e77b9da2c6a6900032f66

Contents?: true

Size: 1.67 KB

Versions: 14

Compression:

Stored size: 1.67 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
        connect_timeout = (config.fetch(:connect_timeout, 0) * 1_000_000).to_i

        if config[:scheme] == "unix"
          connection.connect_unix(config[:path], connect_timeout)
        elsif config[:scheme] == "rediss" || config[:ssl]
          raise NotImplementedError, "SSL not supported by hiredis driver"
        else
          connection.connect(config[:host], config[:port], connect_timeout)
        end

        instance = new(connection)
        instance.timeout = config[:read_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

14 entries across 13 versions & 6 rubygems

Version Path
finsync_redis-3.3.5 lib/redis/connection/hiredis.rb
redis-3.3.5 lib/redis/connection/hiredis.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/redis-3.3.3/lib/redis/connection/hiredis.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/lib/redis/connection/hiredis.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/lib/redis/connection/hiredis.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/lib/redis/connection/hiredis.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/redis-3.3.3/lib/redis/connection/hiredis.rb
redis-3.3.3 lib/redis/connection/hiredis.rb
abaci-0.3.0 vendor/bundle/gems/redis-3.3.2/lib/redis/connection/hiredis.rb
abaci-0.3.0 vendor/bundle/gems/redis-3.3.1/lib/redis/connection/hiredis.rb
redis-3.3.2 lib/redis/connection/hiredis.rb
redis-3.3.1 lib/redis/connection/hiredis.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/redis-3.3.0/lib/redis/connection/hiredis.rb
redis-3.3.0 lib/redis/connection/hiredis.rb