Module: Redis2::Connection::SocketMixin

Included in:
TCPSocket, UNIXSocket
Defined in:
lib/redis2/connection/ruby.rb

Constant Summary collapse

CRLF =
"\r\n".freeze

Instance Method Summary collapse

Instance Method Details

#_read_from_socket(nbytes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redis2/connection/ruby.rb', line 47

def _read_from_socket(nbytes)
  begin
    read_nonblock(nbytes)

  rescue Errno::EWOULDBLOCK, Errno::EAGAIN
    if IO.select([self], nil, nil, @timeout)
      retry
    else
      raise Redis2::TimeoutError
    end
  end

rescue EOFError
  raise Errno::ECONNRESET
end

#getsObject



37
38
39
40
41
42
43
44
45
# File 'lib/redis2/connection/ruby.rb', line 37

def gets
  crlf = nil

  while (crlf = @buffer.index(CRLF)) == nil
    @buffer << _read_from_socket(1024)
  end

  @buffer.slice!(0, crlf + CRLF.bytesize)
end

#initialize(*args) ⇒ Object



12
13
14
15
16
17
# File 'lib/redis2/connection/ruby.rb', line 12

def initialize(*args)
  super(*args)

  @timeout = nil
  @buffer = ""
end

#read(nbytes) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/redis2/connection/ruby.rb', line 27

def read(nbytes)
  result = @buffer.slice!(0, nbytes)

  while result.bytesize < nbytes
    result << _read_from_socket(nbytes - result.bytesize)
  end

  result
end

#timeout=(timeout) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/redis2/connection/ruby.rb', line 19

def timeout=(timeout)
  if timeout && timeout > 0
    @timeout = timeout
  else
    @timeout = nil
  end
end