Class: Dalli::Socket::TCP

Inherits:
TCPSocket
  • Object
show all
Includes:
InstanceMethods
Defined in:
lib/dalli/socket.rb

Overview

A standard TCP socket between the Dalli client and the Memcached server.

Constant Summary

Constants included from InstanceMethods

InstanceMethods::FILTERED_OUT_OPTIONS, InstanceMethods::WAIT_RCS

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from InstanceMethods

#append_to_buffer?, #logged_options, #nonblock_timed_out?, #read_available, #readfull

Instance Attribute Details

#optionsObject

options - supports enhanced logging in the case of a timeout



89
90
91
# File 'lib/dalli/socket.rb', line 89

def options
  @options
end

Class Method Details

.create_socket_with_timeout(host, port, options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dalli/socket.rb', line 100

def self.create_socket_with_timeout(host, port, options)
  # Check that TCPSocket#initialize was not overwritten by resolv-replace gem
  # (part of ruby standard library since 3.0.0, should be removed in 3.4.0),
  # as it does not handle keyword arguments correctly.
  # To check this we are using the fact that resolv-replace
  # aliases TCPSocket#initialize method to #original_resolv_initialize.
  # https://github.com/ruby/resolv-replace/blob/v0.1.1/lib/resolv-replace.rb#L21
  if RUBY_VERSION >= '3.0' &&
     !::TCPSocket.private_instance_methods.include?(:original_resolv_initialize)
    sock = new(host, port, connect_timeout: options[:socket_timeout])
    yield(sock)
  else
    Timeout.timeout(options[:socket_timeout]) do
      sock = new(host, port)
      yield(sock)
    end
  end
end

.init_socket_options(sock, options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dalli/socket.rb', line 119

def self.init_socket_options(sock, options)
  sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if options[:keepalive]
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options[:rcvbuf]) if options[:rcvbuf]
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]

  return unless options[:socket_timeout]

  seconds, fractional = options[:socket_timeout].divmod(1)
  microseconds = fractional * 1_000_000
  timeval = [seconds, microseconds].pack('l_2')

  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
end

.open(host, port, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/dalli/socket.rb', line 91

def self.open(host, port, options = {})
  create_socket_with_timeout(host, port, options) do |sock|
    sock.options = { host: host, port: port }.merge(options)
    init_socket_options(sock, options)

    options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock
  end
end

.wrapping_ssl_socket(tcp_socket, host, ssl_context) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/dalli/socket.rb', line 135

def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
  ssl_socket.hostname = host
  ssl_socket.sync_close = true
  ssl_socket.connect
  ssl_socket
end