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

WAIT_RCS =
%i[wait_writable wait_readable].freeze
FILTERED_OUT_OPTIONS =
%i[username password].freeze

Instance Method Summary collapse

Instance Method Details

#append_to_buffer?(result) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (Timeout::Error)


39
40
41
42
43
44
# File 'lib/dalli/socket.rb', line 39

def append_to_buffer?(result)
  raise Timeout::Error, "IO timeout: #{logged_options.inspect}" if nonblock_timed_out?(result)
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}" unless result

  !WAIT_RCS.include?(result)
end

#logged_optionsObject



54
55
56
# File 'lib/dalli/socket.rb', line 54

def logged_options
  options.reject { |k, _| FILTERED_OUT_OPTIONS.include? k }
end

#nonblock_timed_out?(result) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/dalli/socket.rb', line 46

def nonblock_timed_out?(result)
  return true if result == :wait_readable && !wait_readable(options[:socket_timeout])

  # TODO: Do we actually need this?  Looks to be only used in read_nonblock
  result == :wait_writable && !wait_writable(options[:socket_timeout])
end

#read_availableObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dalli/socket.rb', line 25

def read_available
  value = +''
  loop do
    result = read_nonblock(8196, exception: false)
    break if WAIT_RCS.include?(result)
    raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}" unless result

    value << result
  end
  value
end

#readfull(count) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dalli/socket.rb', line 15

def readfull(count)
  value = String.new(capacity: count + 1)
  loop do
    result = read_nonblock(count - value.bytesize, exception: false)
    value << result if append_to_buffer?(result)
    break if value.bytesize == count
  end
  value
end