Set timeout to Resolv to get ip address from a domain

Viewed 63

BTW: I already take a look at others answers on SO, but none of them works as expected.

Take a look at the following code:

require 'resolv'

t = Time.now
  Resolv::DNS.open do |dns|
    dns.getaddress('thisisaninvaliddomain.com')
  end
p Time.now - t

This piece of code takes something between 1.5 ~ 4.5 seconds to run.

If I add a timeout as other SO responses like (Set a timeout for Ruby Resolv.getaddress(ip)) suggest

require 'resolv'

Resolv::DNS.open do |dns|
  dns.timeouts = 1
  dns.getaddress('thisisaninvaliddomain.com')
end

it just finishes without issues, but, if I put a lower timeout like 0.0001 it fails with the following message:

resolv.rb:379:in 'getaddress': DNS result has no information for thisdomaindoesnotexists.com (Resolv::ResolvError)

If I try with a valid domain (like google.com) it will return the same error instead of a ResolvTimeout, so, I have no way to know if the domain doesn't exist, or it throws a timeout.

Any ideas what's going on here?

0 Answers
Related