class Hiredis::Ruby::Connection

Constants

COMMAND_DELIMITER

Attributes

sock[R]

Public Class Methods

errno_to_class() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 11
def self.errno_to_class
  Errno::Mapping
end
new() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 176
def initialize
  @sock = nil
  @timeout = nil
end

Public Instance Methods

_connect(host, port, timeout) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 30
def _connect(host, port, timeout)
  sock = nil

  begin
    Timeout.timeout(timeout) do
      sock = TCPSocket.new(host, port)
    end
  rescue SocketError => se
    raise se.message
  rescue Timeout::Error
    raise Errno::ETIMEDOUT
  end

  sock
end
_connect_sockaddr(af, sockaddr, timeout) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 143
def _connect_sockaddr(af, sockaddr, timeout)
  sock = Socket.new(af, Socket::SOCK_STREAM, 0)

  begin
    sock.connect_nonblock(sockaddr)
  rescue Errno::EINPROGRESS
    if IO.select(nil, [sock], nil, timeout)
      # Writable, check for errors
      optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
      errno = optval.unpack("i").first

      # Raise socket error if there is any
      raise self.class.errno_to_class[errno] if errno > 0
    else
      # Timeout (TODO: replace with own Timeout class)
      raise Errno::ETIMEDOUT
    end
  end

  sock
rescue
  sock.close if sock

  # Re-raise
  raise
end
_connect_unix(path, timeout) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 46
def _connect_unix(path, timeout)
  sock = nil

  begin
    Timeout.timeout(timeout) do
      sock = UNIXSocket.new(path)
    end
  rescue SocketError => se
    raise se.message
  rescue Timeout::Error
    raise Errno::ETIMEDOUT
  end

  sock
end
_write(sock, data, timeout) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 62
def _write(sock, data, timeout)
  begin
    Timeout.timeout(timeout) do
      sock.write(data)
    end
  rescue Timeout::Error
    raise Errno::EAGAIN
  end
end
connect(host, port, usecs = nil) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 185
def connect(host, port, usecs = nil)
  # Temporarily override timeout on #connect
  timeout = usecs ? (usecs / 1_000_000.0) : @timeout

  # Optionally disconnect current socket
  disconnect if connected?

  sock = _connect(host, port, timeout)
  sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1

  @reader = ::Hiredis::Ruby::Reader.new
  @sock = sock

  nil
end
connect_unix(path, usecs = 0) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 201
def connect_unix(path, usecs = 0)
  # Temporarily override timeout on #connect
  timeout = usecs ? (usecs / 1_000_000.0) : @timeout

  # Optionally disconnect current socket
  disconnect if connected?

  sock = _connect_unix(path, timeout)

  @reader = ::Hiredis::Ruby::Reader.new
  @sock = sock

  nil
end
connected?() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 181
def connected?
  !! @sock
end
disconnect() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 216
def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end
fileno() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 235
def fileno
  raise "not connected" unless connected?

  @sock.fileno
end
flush() click to toggle source

No-op for now..

# File lib/hiredis/ruby/connection.rb, line 260
def flush
end
read() click to toggle source
# File lib/hiredis/ruby/connection.rb, line 263
def read
  raise "not connected" unless connected?

  while (reply = @reader.gets) == false
    begin
      @reader.feed @sock.read_nonblock(1024)
    rescue Errno::EAGAIN
      if IO.select([@sock], [], [], @timeout)
        # Readable, try again
        retry
      else
        # Timed out, raise
        raise Errno::EAGAIN
      end
    end
  end

  reply
rescue ::EOFError
  raise Errno::ECONNRESET
end
timeout=(usecs) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 223
def timeout=(usecs)
  raise ArgumentError.new("timeout cannot be negative") if usecs < 0

  if usecs == 0
    @timeout = nil
  else
    @timeout = usecs / 1_000_000.0
  end

  nil
end
write(args) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 243
def write(args)
  command = []
  command << "*#{args.size}"
  args.each do |arg|
    arg = arg.to_s
    command << "$#{string_size arg}"
    command << arg
  end

  data = command.join(COMMAND_DELIMITER) + COMMAND_DELIMITER

  _write(@sock, data, @timeout)

  nil
end

Protected Instance Methods

string_size(string) click to toggle source
# File lib/hiredis/ruby/connection.rb, line 288
def string_size(string)
  string.to_s.bytesize
end