Sha256: bf33963403de7f661757a10e466491b28bc996f8485c098fee0881198c106bec

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require_relative '../polyphony'

require 'redis'
require 'hiredis/reader'

# Polyphony-based Redis driver
class Driver
  def self.connect(config)
    raise 'unix sockets not supported' if config[:scheme] == 'unix'

    # connection.connect_unix(config[:path], connect_timeout)

    raise 'ssl not supported' if config[:scheme] == 'rediss' || config[:ssl]

    # raise NotImplementedError, "SSL not supported by hiredis driver"

    new(config[:host], config[:port])
    # connection.connect(config[:host], config[:port], connect_timeout)
  end

  def initialize(host, port)
    @connection = Polyphony::Net.tcp_connect(host, port)
    @reader = ::Hiredis::Reader.new
  end

  def connected?
    @connection && !@connection.closed?
  end

  def timeout=(timeout)
    # ignore timeout for now
  end

  def disconnect
    @connection.close
    @connection = nil
  end

  def write(command)
    @connection.write(format_command(command))
  end

  def format_command(args)
    (+"*#{args.size}\r\n").tap do |s|
      args.each do |a|
        a = a.to_s
        s << "$#{a.bytesize}\r\n#{a}\r\n"
      end
    end
  end

  def read
    reply = @reader.gets
    return reply if reply

    while (data = @connection.readpartial(8192))
      @reader.feed(data)
      reply = @reader.gets
      return reply unless reply == false
    end
  end
end

Redis::Connection.drivers << Driver

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
polyphony-0.30 lib/polyphony/redis.rb
polyphony-0.29 lib/polyphony/redis.rb
polyphony-0.28 lib/polyphony/redis.rb
polyphony-0.27 lib/polyphony/redis.rb
polyphony-0.26 lib/polyphony/redis.rb
polyphony-0.25 lib/polyphony/redis.rb
polyphony-0.24 lib/polyphony/redis.rb
polyphony-0.23 lib/polyphony/redis.rb
polyphony-0.22 lib/polyphony/redis.rb
polyphony-0.21 lib/polyphony/redis.rb
polyphony-0.20 lib/polyphony/redis.rb