Class: Redis::Connection::Celluloid

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ Celluloid

Returns a new instance of Celluloid.



28
29
30
# File 'lib/redis/connection/celluloid.rb', line 28

def initialize(sock)
  @sock = sock
end

Class Method Details

.connect(config) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/redis/connection/celluloid.rb', line 17

def self.connect(config)
  # TODO: config[:timeout] support
  if config[:scheme] == "unix"
    sock = ::Celluloid::IO::UNIXSocket.open(config[:path])
  else
    sock = ::Celluloid::IO::TCPSocket.open(config[:host], config[:port])
  end

  new(sock)
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/redis/connection/celluloid.rb', line 32

def connected?
  !!@sock
end

#disconnectObject



36
37
38
39
40
# File 'lib/redis/connection/celluloid.rb', line 36

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

#format_bulk_reply(line) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/redis/connection/celluloid.rb', line 85

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



73
74
75
# File 'lib/redis/connection/celluloid.rb', line 73

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

#format_integer_reply(line) ⇒ Object



81
82
83
# File 'lib/redis/connection/celluloid.rb', line 81

def format_integer_reply(line)
  line.to_i
end

#format_multi_bulk_reply(line) ⇒ Object



93
94
95
96
97
98
# File 'lib/redis/connection/celluloid.rb', line 93

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/redis/connection/celluloid.rb', line 62

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.new(reply_type)
  end
end

#format_status_reply(line) ⇒ Object



77
78
79
# File 'lib/redis/connection/celluloid.rb', line 77

def format_status_reply(line)
  line.strip
end

#readObject



52
53
54
55
56
57
58
59
60
# File 'lib/redis/connection/celluloid.rb', line 52

def read
  line = @sock.gets
  raise Errno::ECONNRESET unless line
  reply_type = line.slice!(0, 1)
  format_reply(reply_type, line)

rescue Errno::EAGAIN
  raise TimeoutError
end

#timeout=(timeout) ⇒ Object



42
43
44
45
46
# File 'lib/redis/connection/celluloid.rb', line 42

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

#write(command) ⇒ Object



48
49
50
# File 'lib/redis/connection/celluloid.rb', line 48

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