Skip to content

Commit

Permalink
feat: add no_random option (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske authored Feb 22, 2021
1 parent 6d96cfb commit ad4a51c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ server {
nameservers = {"8.8.8.8", {"8.8.4.4", 53} },
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
no_random = true, -- always start with first nameserver
}
if not r then
Expand Down Expand Up @@ -134,6 +135,9 @@ It accepts a `opts` table argument. The following options are supported:
* `no_recurse`

a boolean flag controls whether to disable the "recursion desired" (RD) flag in the UDP request. Defaults to `false`.
* `no_random`

a boolean flag controls whether to randomly pick the nameserver to query first, if `true` will always start with the first nameserver listed. Defaults to `false`.

[Back to TOC](#table-of-contents)

Expand Down
3 changes: 2 additions & 1 deletion lib/resty/dns/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ function _M.new(class, opts)
tcp_sock:settimeout(timeout)

return setmetatable(
{ cur = rand(1, n), socks = socks,
{ cur = opts.no_random and 1 or rand(1, n),
socks = socks,
tcp_sock = tcp_sock,
servers = servers,
retrans = opts.retrans or 5,
Expand Down
113 changes: 112 additions & 1 deletion t/mock.t
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,118 @@ Connection refused
local ans, err, lst = r:query("www.google.com", { qtype = r.TYPE_A }, {})
if not ans then
ngx.say("failed to query:")
table.sort(lst)
table.sort(lst) -- must sort because we have a random start
for i, err in ipairs(lst) do
ngx.say(i, ": ", err)
end
return
end
-- should not reach here
}
}
--- request
GET /t
--- udp_query eval
"\x{00}}\x{01}\x{00}\x{00}\x{01}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{03}www\x{06}google\x{03}com\x{00}\x{00}\x{01}\x{00}\x{01}"
--- response_body
failed to query:
1: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
2: failed to receive reply from UDP server 127.0.0.1:20001: connection refused
3: failed to receive reply from UDP server 127.0.0.1:20002: connection refused
--- error_log
Connection refused



=== TEST 38: no_random always starts at first server
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local resolver = require "resty.dns.resolver"

local ans, err, lst
local tries = {}
for i = 1, 10 do
local r, err = resolver:new{
nameservers = { -- note: using bad ports
{"127.0.0.1", 20000},
{"127.0.0.1", 20001},
{"127.0.0.1", 20002},
},
retrans = 1,
no_random = true,
}
if not r then
ngx.say("failed to instantiate resolver: ", err)
return
end

--r._id = 125

local ans, err, lst = r:query("www.google.com", { qtype = r.TYPE_A }, {})
if ans then
error("query wasn't supposed to succeed")
end
for _, try in ipairs(lst) do
tries[#tries+1] = try
end
end
ngx.say("tries:")
--table.sort(lst)
for i, err in ipairs(tries) do
ngx.say(i, ": ", err)
end
return
}
}
--- request
GET /t
--- udp_query eval
"\x{00}}\x{01}\x{00}\x{00}\x{01}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{03}www\x{06}google\x{03}com\x{00}\x{00}\x{01}\x{00}\x{01}"
--- response_body
tries:
1: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
2: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
3: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
4: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
5: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
6: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
7: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
8: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
9: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
10: failed to receive reply from UDP server 127.0.0.1:20000: connection refused
--- error_log
Connection refused



=== TEST 39: no_random tries servers in defined order
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local resolver = require "resty.dns.resolver"

local r, err = resolver:new{
nameservers = { -- note: using bad ports
{"127.0.0.1", 20000},
{"127.0.0.1", 20001},
{"127.0.0.1", 20002},
},
retrans = 3,
no_random = true,
}
if not r then
ngx.say("failed to instantiate resolver: ", err)
return
end

r._id = 125

local ans, err, lst = r:query("www.google.com", { qtype = r.TYPE_A }, {})
if not ans then
ngx.say("failed to query:")
for i, err in ipairs(lst) do
ngx.say(i, ": ", err)
end
Expand Down

0 comments on commit ad4a51c

Please sign in to comment.