为Ruby Resolv.getaddress(ip)设置超时

时间:2016-08-13 21:21:54

标签: ruby timeout resolv

我无法找到in the Ruby documentation关于如何使用Resolv类中的类方法getaddress从Ruby std库设置超时来检索域IP的任何内容。

2 个答案:

答案 0 :(得分:2)

查看Resolv的source code,在第353行,我可以看到在DNS类中定义了一个名为timeouts的方法。您应该可以使用它来更改超时。

    # Sets the resolver timeouts.  This may be a single positive number
    # or an array of positive numbers representing timeouts in seconds.
    # If an array is specified, a DNS request will retry and wait for
    # each successive interval in the array until a successful response
    # is received.  Specifying +nil+ reverts to the default timeouts:
    # [ 5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second ]
    #
    # Example:
    #
    #   dns.timeouts = 3
    #
    def timeouts=(values)
      @config.timeouts = values
    end

答案 1 :(得分:0)

超时选项可以这样使用:

Resolv::DNS.open do |dns|
  dns.timeouts = 1
  host = dns.getname "172.28.0.1"
  puts "hostname: #{host}"
end