Class: Dalli::Socket::TCP
- Inherits:
-
TCPSocket
- Object
- TCPSocket
- Dalli::Socket::TCP
- 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
-
#options ⇒ Object
options - supports enhanced logging in the case of a timeout.
Class Method Summary collapse
- .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
Methods included from InstanceMethods
#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 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, ) # 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: [:socket_timeout]) yield(sock) else Timeout.timeout([: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.(sock, ) sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true) sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if [:keepalive] sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, [:rcvbuf]) if [:rcvbuf] sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, [:sndbuf]) if [:sndbuf] return unless [:socket_timeout] seconds, fractional = [: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, = {}) create_socket_with_timeout(host, port, ) do |sock| sock. = { host: host, port: port }.merge() (sock, ) [:ssl_context] ? wrapping_ssl_socket(sock, host, [: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 |