Asynchronous DNS Resolution
RubyDNS can be used as an asynchronous DNS client, either passing requests to a specific server or using default system resolvers.
# There technically should be at least one nameserver:
resolver = RubyDNS::Resolver.new(RubyDNS::System::nameservers)
EventMachine::run do
resolver.query('google.com') do |response|
assert_equal RubyDNS::Message, response.class
assert_equal Resolv::DNS::RCode::NoError, response.rcode
EventMachine::stop
end
end
EventMachine
provides the basic asynchronous event processing loop, and thus any use of RubyDNS::Resolver
must happen within a valid EventMachine
context.
Timeout
As this is an asynchronous service, a timeout can be provided (the default is 5 seconds). If a response is not received within a given time, a RubyDNS::ResolutionFailure
error will be returned to the caller in the response
argument:
SERVERS = [[:udp, '127.0.0.1', 5330], [:tcp, '127.0.0.1', 5330]]
resolver = RubyDNS::Resolver.new(SERVERS, :timeout => 60)
For more examples, please check out rubydns/test/test_slow_server.rb.
Logging
Additional debugging output can be sent to a log file by providing the :logger
option:
require 'logger'
SERVERS = [[:udp, '127.0.0.1', 5330], [:tcp, '127.0.0.1', 5330]]
resolver = RubyDNS::Resolver.new(SERVERS, :logger => Logger.new($stderr))