Module: Redis::Connection::SocketMixin

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

Constant Summary collapse

CRLF =
"\r\n".freeze

Instance Method Summary collapse

Instance Method Details

#_read_from_socket(nbytes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/redis/connection/ruby.rb', line 62

def _read_from_socket(nbytes)

  begin
    read_nonblock(nbytes)

  rescue IO::WaitReadable
    if IO.select([self], nil, nil, @timeout)
      retry
    else
      raise Redis::TimeoutError
    end
  rescue IO::WaitWritable
    if IO.select(nil, [self], nil, @timeout)
      retry
    else
      raise Redis::TimeoutError
    end
  end

rescue EOFError
  raise Errno::ECONNRESET
end

#_write_to_socket(data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis/connection/ruby.rb', line 85

def _write_to_socket(data)
  begin
    write_nonblock(data)

  rescue IO::WaitWritable
    if IO.select(nil, [self], nil, @write_timeout)
      retry
    else
      raise Redis::TimeoutError
    end
  rescue IO::WaitReadable
    if IO.select([self], nil, nil, @write_timeout)
      retry
    else
      raise Redis::TimeoutError
    end
  end

rescue EOFError
  raise Errno::ECONNRESET
end

#getsObject



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

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



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

def initialize(*args)
  super(*args)

  @timeout = @write_timeout = nil
  @buffer = "".dup
end

#read(nbytes) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/redis/connection/ruby.rb', line 42

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



26
27
28
29
30
31
32
# File 'lib/redis/connection/ruby.rb', line 26

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

#write(data) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/redis/connection/ruby.rb', line 107

def write(data)
  return super(data) unless @write_timeout

  length = data.bytesize
  total_count = 0
  loop do
    count = _write_to_socket(data)

    total_count += count
    return total_count if total_count >= length
    data = data.byteslice(count..-1)
  end
end

#write_timeout=(timeout) ⇒ Object



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

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