Module: Dalli::Socket::InstanceMethods

Included in:
SSLSocket, TCP, UNIX
Defined in:
lib/dalli/socket.rb

Overview

Common methods for all socket implementations.

Constant Summary collapse

FILTERED_OUT_OPTIONS =
%i[username password].freeze

Instance Method Summary collapse

Instance Method Details

#logged_optionsObject



44
45
46
# File 'lib/dalli/socket.rb', line 44

def logged_options
  options.except(*FILTERED_OUT_OPTIONS)
end

#read_available(reusable_buffer = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dalli/socket.rb', line 15

def read_available(reusable_buffer = nil)
  if reusable_buffer
    value = read_nonblock(8196, reusable_buffer, exception: false)
    case value
    when :wait_writable, :wait_readable
      return reusable_buffer.clear
    when nil
      raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
    end
  else
    value = ''.b
  end

  buffer = ''.b
  loop do
    result = read_nonblock(8196, buffer, exception: false)
    case result
    when :wait_writable, :wait_readable
      buffer.clear
      return value
    when nil
      raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
    else
      value << result
    end
  end
end

#readfull(count) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dalli/socket.rb', line 51

def readfull(count)
  value = String.new(capacity: count + 1)

  until value.bytesize == count
    result = read_nonblock(count - value.bytesize, exception: false)
    case result
    when :wait_readable
      wait_readable(options[:socket_timeout]) or raise Timeout::Error, "IO timeout: #{logged_options.inspect}"
    when :wait_writable
      wait_writable(options[:socket_timeout]) or raise Timeout::Error, "IO timeout: #{logged_options.inspect}"
    when nil
      raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}"
    else
      value << result
    end
  end

  value
end