【问题标题】:Set a timeout for Ruby Resolv.getaddress(ip)为 Ruby Resolv.getaddress(ip) 设置超时
【发布时间】:2016-12-20 14:21:29
【问题描述】:

我找不到in the Ruby documentation 任何关于如何使用 Resolv 类(来自 Ruby 标准库)的类方法 getaddress 来设置超时以检索域 IP 的信息。

【问题讨论】:

    标签: ruby timeout resolv


    【解决方案1】:

    查看 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
    

    【讨论】:

      【解决方案2】:

      超时选项可以这样使用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-10
        • 2014-08-14
        • 2011-11-11
        • 2014-07-30
        • 1970-01-01
        • 2010-10-10
        • 2010-09-27
        相关资源
        最近更新 更多