summaryrefslogtreecommitdiff
path: root/ext/socket/lib/socket.rb
diff options
context:
space:
mode:
authorMasaki Matsushita <glass.saga@gmail.com>2019-09-10 09:57:31 +0900
committerMasaki Matsushita <glass.saga@gmail.com>2019-09-10 10:10:59 +0900
commit0e9d56f5e73ed2fd8e7c858fdea7b7d5b905bb64 (patch)
tree6f6133010c44d90c82e0f3b5a53edf649e4352a5 /ext/socket/lib/socket.rb
parent3678c371199d9a048589bf3da37f06223912dea5 (diff)
Support timeout for Addrinfo
Addrinfo.getaddrinfo and .foreach now accepts :timeout in seconds as a keyword argument. If getaddrinfo_a(3) is available, the timeout will be applied for name resolution. Otherwise, it will be ignored. Socket.tcp accepts :resolv_timeout to use this feature. This commit is retry of 6382f5cc91ac9e36776bc854632d9a1237250da7. Test was failed on Solaris machines which don't have "http" in /etc/services. In this commit, use "ssh" instead.
Diffstat (limited to 'ext/socket/lib/socket.rb')
-rw-r--r--ext/socket/lib/socket.rb9
1 files changed, 5 insertions, 4 deletions
diff --git a/ext/socket/lib/socket.rb b/ext/socket/lib/socket.rb
index 4ed2df23e6..d756a32a5a 100644
--- a/ext/socket/lib/socket.rb
+++ b/ext/socket/lib/socket.rb
@@ -223,8 +223,8 @@ class Addrinfo
# # #<Addrinfo: [::1]:80 TCP (:80)>
# # #<Addrinfo: [::1]:80 UDP (:80)>
#
- def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, &block)
- Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags).each(&block)
+ def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, timeout: nil, &block)
+ Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags, timeout: timeout).each(&block)
end
end
@@ -606,6 +606,7 @@ class Socket < BasicSocket
# _opts_ may have following options:
#
# [:connect_timeout] specify the timeout in seconds.
+ # [:resolv_timeout] specify the name resolution timeout in seconds.
#
# If a block is given, the block is called with the socket.
# The value of the block is returned.
@@ -619,7 +620,7 @@ class Socket < BasicSocket
# puts sock.read
# }
#
- def self.tcp(host, port, local_host = nil, local_port = nil, connect_timeout: nil) # :yield: socket
+ def self.tcp(host, port, local_host = nil, local_port = nil, connect_timeout: nil, resolv_timeout: nil) # :yield: socket
last_error = nil
ret = nil
@@ -628,7 +629,7 @@ class Socket < BasicSocket
local_addr_list = Addrinfo.getaddrinfo(local_host, local_port, nil, :STREAM, nil)
end
- Addrinfo.foreach(host, port, nil, :STREAM) {|ai|
+ Addrinfo.foreach(host, port, nil, :STREAM, timeout: resolv_timeout) {|ai|
if local_addr_list
local_addr = local_addr_list.find {|local_ai| local_ai.afamily == ai.afamily }
next unless local_addr