Sha256: ff1b00e0ecfb7e28b5037f7cb90490f73fbc26bd5f101377e5895743fba03959

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require_relative "registry"
require_relative "../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&.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, err.message
      end
    end
  end
end

Redis::Connection.drivers << Redis::Connection::Hiredis

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
redis-4.5.1 lib/redis/connection/hiredis.rb
redis-4.5.0 lib/redis/connection/hiredis.rb
redis-4.4.0 lib/redis/connection/hiredis.rb
redis-4.3.1 lib/redis/connection/hiredis.rb
redis-4.3.0 lib/redis/connection/hiredis.rb
redis-4.2.5 lib/redis/connection/hiredis.rb
redis-4.2.4 lib/redis/connection/hiredis.rb
redis-4.2.3 lib/redis/connection/hiredis.rb
redis-4.2.2 lib/redis/connection/hiredis.rb
redis-4.2.1 lib/redis/connection/hiredis.rb
redis-4.2.0 lib/redis/connection/hiredis.rb