Class: Dalli::Socket::TCP
- Inherits:
-
TCPSocket
- Object
- TCPSocket
- Dalli::Socket::TCP
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
InstanceMethods::FILTERED_OUT_OPTIONS, InstanceMethods::WAIT_RCS
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.configure_socket_buffers(sock, options) ⇒ Object
-
.configure_tcp_options(sock, options) ⇒ Object
-
.configure_timeout(sock, options) ⇒ Object
-
.create_socket_with_timeout(host, port, options) ⇒ Object
-
.init_socket_options(sock, options) ⇒ Object
-
.open(host, port, options = {}) ⇒ Object
-
.wrapping_ssl_socket(tcp_socket, host, ssl_context) ⇒ Object
#append_to_buffer?, #logged_options, #nonblock_timed_out?, #read_available, #readfull
Instance Attribute Details
#options ⇒ Object
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
130
131
132
133
|
# File 'lib/dalli/socket.rb', line 130
def self.configure_socket_buffers(sock, options)
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]
end
|
125
126
127
128
|
# File 'lib/dalli/socket.rb', line 125
def self.configure_tcp_options(sock, options)
sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if options[:keepalive]
end
|
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/dalli/socket.rb', line 135
def self.configure_timeout(sock, options)
return unless options[:socket_timeout]
if sock.respond_to?(:timeout=)
sock.timeout = options[:socket_timeout]
else
seconds, fractional = options[:socket_timeout].divmod(1)
timeval = [seconds, fractional * 1_000_000].pack('l_2')
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
end
end
|
.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)
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
|
# File 'lib/dalli/socket.rb', line 119
def self.init_socket_options(sock, options)
configure_tcp_options(sock, options)
configure_socket_buffers(sock, options)
configure_timeout(sock, options)
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
149
150
151
152
153
154
155
|
# File 'lib/dalli/socket.rb', line 149
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
|