Skip to content

Commit

Permalink
[resty.resolver] do not allocate dns socket unless used
Browse files Browse the repository at this point in the history
allocating dns resolver will cause opening a socket

flamegraphs: https://gist.github.com/mikz/22c650100409fb172dc543596c49a309
  • Loading branch information
mikz committed Feb 7, 2017
1 parent b78b01f commit 5f73f52
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions apicast/src/resty/resolver/dns.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local resty_resolver = require 'resty.dns.resolver'

local setmetatable = setmetatable
local ipairs = ipairs
local concat = table.concat
local pairs = pairs

local _M = {
_VERSION = '0.1'
Expand All @@ -14,23 +13,34 @@ function _M.new(_, options)
local opts = options or {}
local nameservers = opts.nameservers or {}

for _,nameserver in ipairs(nameservers) do
resolvers[nameserver] = resty_resolver:new({ nameservers = { nameserver }})
end

return setmetatable({
resolvers = resolvers
resolvers = resolvers,
nameservers = nameservers
}, mt)
end

function _M:init_resolvers()
local resolvers = self.resolvers
local nameservers = self.nameservers

for i=1,#nameservers do
resolvers[nameservers[i]] = resty_resolver:new({ nameservers = { nameservers[i] }})
end
return resolvers
end

function _M.query(self, qname, opts)
local resolvers = self.resolvers
local answers, err

if #resolvers == 0 then
resolvers = self:init_resolvers()
end

for nameserver, resolver in pairs(resolvers) do
answers, err = resolver:query(qname, opts)

ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' nameserver: ', concat(nameserver,':'))
ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' nameserver: ', nameserver[1],':', nameserver[2])

if answers and not answers.errcode and not err then
break
Expand Down

0 comments on commit 5f73f52

Please sign in to comment.