diff --git a/lib/dalli/socket.rb b/lib/dalli/socket.rb index d175d11d..6e0f4111 100644 --- a/lib/dalli/socket.rb +++ b/lib/dalli/socket.rb @@ -88,14 +88,24 @@ class TCP < TCPSocket # options - supports enhanced logging in the case of a timeout attr_accessor :options - def self.open(host, port, options = {}) - Timeout.timeout(options[:socket_timeout]) do - sock = new(host, port) + if RUBY_VERSION >= "3.0" + def self.open(host, port, options = {}) + sock = new(host, port, connect_timeout: options[:socket_timeout]) 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 + else + def self.open(host, port, options = {}) + Timeout.timeout(options[:socket_timeout]) do + sock = new(host, port) + 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 end def self.init_socket_options(sock, options) @@ -103,6 +113,8 @@ def self.init_socket_options(sock, options) 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] + sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, options[:socket_timeout]) if options[:socket_timeout] + sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, options[:socket_timeout]) if options[:socket_timeout] end def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)