Class: Redis::Connection::Ruby

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/redis/connection/ruby.rb

Constant Summary collapse

MINUS =
"-"
PLUS =
"+"
COLON =
":"
DOLLAR =
"$"
ASTERISK =
"*"

Constants included from CommandHelper

CommandHelper::COMMAND_DELIMITER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#build_command

Constructor Details

#initialize(sock) ⇒ Ruby

Returns a new instance of Ruby.



351
352
353
# File 'lib/redis/connection/ruby.rb', line 351

def initialize(sock)
  @sock = sock
end

Class Method Details

.connect(config) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/redis/connection/ruby.rb', line 297

def self.connect(config)
  if config[:scheme] == "unix"
    raise ArgumentError, "SSL incompatible with unix sockets" if config[:ssl]

    sock = UNIXSocket.connect(config[:path], config[:connect_timeout])
  elsif config[:scheme] == "rediss" || config[:ssl]
    sock = SSLSocket.connect(config[:host], config[:port], config[:connect_timeout], config[:ssl_params])
  else
    sock = TCPSocket.connect(config[:host], config[:port], config[:connect_timeout])
  end

  instance = new(sock)
  instance.timeout = config[:read_timeout]
  instance.write_timeout = config[:write_timeout]
  instance.set_tcp_keepalive config[:tcp_keepalive]
  instance.set_tcp_nodelay if sock.is_a? TCPSocket
  instance
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


355
356
357
# File 'lib/redis/connection/ruby.rb', line 355

def connected?
  !!@sock
end

#disconnectObject



359
360
361
362
363
364
# File 'lib/redis/connection/ruby.rb', line 359

def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end

#format_bulk_reply(line) ⇒ Object



409
410
411
412
413
414
415
416
# File 'lib/redis/connection/ruby.rb', line 409

def format_bulk_reply(line)
  bulklen = line.to_i
  return if bulklen == -1

  reply = encode(@sock.read(bulklen))
  @sock.read(2) # Discard CRLF.
  reply
end

#format_error_reply(line) ⇒ Object



397
398
399
# File 'lib/redis/connection/ruby.rb', line 397

def format_error_reply(line)
  CommandError.new(line.strip)
end

#format_integer_reply(line) ⇒ Object



405
406
407
# File 'lib/redis/connection/ruby.rb', line 405

def format_integer_reply(line)
  line.to_i
end

#format_multi_bulk_reply(line) ⇒ Object



418
419
420
421
422
423
# File 'lib/redis/connection/ruby.rb', line 418

def format_multi_bulk_reply(line)
  n = line.to_i
  return if n == -1

  Array.new(n) { read }
end

#format_reply(reply_type, line) ⇒ Object



386
387
388
389
390
391
392
393
394
395
# File 'lib/redis/connection/ruby.rb', line 386

def format_reply(reply_type, line)
  case reply_type
  when MINUS    then format_error_reply(line)
  when PLUS     then format_status_reply(line)
  when COLON    then format_integer_reply(line)
  when DOLLAR   then format_bulk_reply(line)
  when ASTERISK then format_multi_bulk_reply(line)
  else raise ProtocolError, reply_type
  end
end

#format_status_reply(line) ⇒ Object



401
402
403
# File 'lib/redis/connection/ruby.rb', line 401

def format_status_reply(line)
  line.strip
end

#get_tcp_keepaliveObject



326
327
328
329
330
331
332
# File 'lib/redis/connection/ruby.rb', line 326

def get_tcp_keepalive
  {
    time: @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE).int,
    intvl: @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL).int,
    probes: @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT).int
  }
end

#readObject



378
379
380
381
382
383
384
# File 'lib/redis/connection/ruby.rb', line 378

def read
  line = @sock.gets
  reply_type = line.slice!(0, 1)
  format_reply(reply_type, line)
rescue Errno::EAGAIN
  raise TimeoutError
end

#set_tcp_keepalive(keepalive) ⇒ Object



317
318
319
320
321
322
323
324
# File 'lib/redis/connection/ruby.rb', line 317

def set_tcp_keepalive(keepalive)
  return unless keepalive.is_a?(Hash)

  @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE,  true)
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPIDLE,  keepalive[:time])
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPINTVL, keepalive[:intvl])
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPCNT,   keepalive[:probes])
end

#set_tcp_nodelayObject



344
345
346
# File 'lib/redis/connection/ruby.rb', line 344

def set_tcp_nodelay
  @sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end

#timeout=(timeout) ⇒ Object



366
367
368
# File 'lib/redis/connection/ruby.rb', line 366

def timeout=(timeout)
  @sock.timeout = timeout if @sock.respond_to?(:timeout=)
end

#write(command) ⇒ Object



374
375
376
# File 'lib/redis/connection/ruby.rb', line 374

def write(command)
  @sock.write(build_command(command))
end

#write_timeout=(timeout) ⇒ Object



370
371
372
# File 'lib/redis/connection/ruby.rb', line 370

def write_timeout=(timeout)
  @sock.write_timeout = timeout
end