1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/resolv.rb: make timeout configurable for DNS query.

patch by Eric Wong.  [ruby-core:38533] [Feature #5100]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2011-10-22 08:46:12 +00:00
parent f600a558e0
commit 16f4ecbc8c
3 changed files with 59 additions and 1 deletions

View file

@ -1,3 +1,8 @@
Sat Oct 22 17:43:33 2011 Tanaka Akira <akr@fsij.org>
* lib/resolv.rb: make timeout configurable for DNS query.
patch by Eric Wong. [ruby-core:38533] [Feature #5100]
Sat Oct 22 02:07:48 2011 Naohisa Goto <ngotogenome@gmail.com>
* numeric.c (rb_infinity, rb_nan): use union to prevent bus error

View file

@ -336,6 +336,21 @@ class Resolv
@initialized = nil
end
# 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
def lazy_initialize # :nodoc:
@mutex.synchronize {
unless @initialized
@ -851,6 +866,20 @@ class Resolv
@mutex = Mutex.new
@config_info = config_info
@initialized = nil
@timeouts = nil
end
def timeouts=(values)
if values
values = Array(values)
values.each do |t|
Numeric === t or raise ArgumentError, "#{t.inspect} is not numeric"
t > 0.0 or raise Argument, "timeout=#{t} must be postive"
end
@timeouts = values
else
@timeouts = nil
end
end
def Config.parse_resolv_conf(filename)
@ -1013,7 +1042,7 @@ class Resolv
def resolv(name)
candidates = generate_candidates(name)
timeouts = generate_timeouts
timeouts = @timeouts || generate_timeouts
begin
candidates.each {|candidate|
begin

View file

@ -105,6 +105,30 @@ class TestResolvDNS < Test::Unit::TestCase
}
end
def test_query_ipv4_address_timeout
with_udp('127.0.0.1', 0) {|u|
_, port , _, host = u.addr
start = nil
rv = Resolv::DNS.open(:nameserver_port => [[host, port]]) {|dns|
dns.timeouts = 0.1
start = Time.now
dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A)
}
diff = Time.now - start
assert rv.empty?, "unexpected: #{rv.inspect} (expected empty)"
assert_in_delta 0.1, diff, 0.05
rv = Resolv::DNS.open(:nameserver_port => [[host, port]]) {|dns|
dns.timeouts = [ 0.1, 0.2 ]
start = Time.now
dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A)
}
diff = Time.now - start
assert rv.empty?, "unexpected: #{rv.inspect} (expected empty)"
assert_in_delta 0.3, diff, 0.05
}
end
def test_no_server
u = UDPSocket.new
u.bind("127.0.0.1", 0)