Gmae
Gmae

Reputation: 63

Set a timeout for Ruby Resolv.getaddress(ip)

I can't find in the Ruby documentation anything on how to set a timeout to retrieve the IP of a domain using the class method getaddress from the Resolv class—from the Ruby std library.

Upvotes: 2

Views: 1620

Answers (2)

Tiit
Tiit

Reputation: 520

The timeout option could be used like this:

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

Upvotes: 3

user2993456
user2993456

Reputation:

Looking at the source code for Resolv, at line 353, I can see that there is a method named timeouts defined in the DNS class. You should be able to use this to change the timeout.

    # 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

Upvotes: 2

Related Questions