mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 13:47:41 +03:00
net.dns: Add jitter to spread queries and reduce failures due to congestion
This commit is contained in:
parent
3ff48b8386
commit
d080fee323
1 changed files with 20 additions and 2 deletions
22
net/dns.lua
22
net/dns.lua
|
@ -72,6 +72,8 @@ local ztact = { -- public domain 20080404 lua@ztact.com
|
||||||
local get, set = ztact.get, ztact.set;
|
local get, set = ztact.get, ztact.set;
|
||||||
|
|
||||||
local default_timeout = 15;
|
local default_timeout = 15;
|
||||||
|
local default_jitter = 1;
|
||||||
|
local default_retry_jitter = 2;
|
||||||
|
|
||||||
-------------------------------------------------- module dns
|
-------------------------------------------------- module dns
|
||||||
local _ENV = nil;
|
local _ENV = nil;
|
||||||
|
@ -668,6 +670,8 @@ end
|
||||||
|
|
||||||
resolver.delays = { 1, 3 };
|
resolver.delays = { 1, 3 };
|
||||||
|
|
||||||
|
resolver.jitter = have_timer and default_jitter or nil;
|
||||||
|
resolver.retry_jitter = have_timer and default_retry_jitter or nil;
|
||||||
|
|
||||||
function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
|
function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
|
||||||
self.server = self.server or {};
|
self.server = self.server or {};
|
||||||
|
@ -855,7 +859,7 @@ function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
|
||||||
packet = header..question,
|
packet = header..question,
|
||||||
server = self.best_server,
|
server = self.best_server,
|
||||||
delay = 1,
|
delay = 1,
|
||||||
retry = socket.gettime() + self.delays[1]
|
retry = socket.gettime() + self.delays[1];
|
||||||
qclass = qclass;
|
qclass = qclass;
|
||||||
qtype = qtype;
|
qtype = qtype;
|
||||||
qname = qname;
|
qname = qname;
|
||||||
|
@ -869,7 +873,13 @@ function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
|
||||||
if not conn then
|
if not conn then
|
||||||
return nil, err;
|
return nil, err;
|
||||||
end
|
end
|
||||||
conn:send (o.packet)
|
if self.jitter then
|
||||||
|
timer.add_task(math.random()*self.jitter, function ()
|
||||||
|
conn:send(o.packet);
|
||||||
|
end);
|
||||||
|
else
|
||||||
|
conn:send(o.packet);
|
||||||
|
end
|
||||||
|
|
||||||
-- remember which coroutine wants the answer
|
-- remember which coroutine wants the answer
|
||||||
if co then
|
if co then
|
||||||
|
@ -920,8 +930,16 @@ function resolver:servfail(sock, err)
|
||||||
sock, err = self:getsocket(o.server);
|
sock, err = self:getsocket(o.server);
|
||||||
if sock then
|
if sock then
|
||||||
retried = true;
|
retried = true;
|
||||||
|
if self.retry_jitter then
|
||||||
|
local delay = self.delays[((o.retries-1)%#self.delays)+1] + (math.random()*self.retry_jitter);
|
||||||
|
log("debug", "retry %d in %0.2fs", o.retries, delay);
|
||||||
|
timer.add_task(delay, function ()
|
||||||
|
sock:send(o.packet);
|
||||||
|
end);
|
||||||
|
else
|
||||||
log("debug", "retry %d (immediate)", o.retries);
|
log("debug", "retry %d (immediate)", o.retries);
|
||||||
sock:send(o.packet);
|
sock:send(o.packet);
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not retried then
|
if not retried then
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue