diff options
author | Masaki Matsushita <glass.saga@gmail.com> | 2018-12-31 12:17:39 +0900 |
---|---|---|
committer | Masaki Matsushita <glass.saga@gmail.com> | 2019-09-09 14:34:05 +0900 |
commit | 6382f5cc91ac9e36776bc854632d9a1237250da7 (patch) | |
tree | 4276c3de900e0f4300b8e0165dfc94ead4e0920d /ext/socket/lib | |
parent | fa79219356715e28529b721e81056ec69a998c4e (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.
Diffstat (limited to 'ext/socket/lib')
-rw-r--r-- | ext/socket/lib/socket.rb | 9 |
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 |