Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resty.resolver] do not allocate dns socket unless used #251

Merged
merged 2 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Changed
- Lazy load DNS resolver to improve performance [PR #251](https://github.com/3scale/apicast/pull/251)

## [3.0.0-alpha2] - 2017-02-06
### Added
Expand Down Expand Up @@ -41,4 +43,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[Unreleased]: https://github.com/3scale/apicast/compare/v2.0.0...HEAD
[2.0.0]: https://github.com/3scale/apicast/compare/v0.2...v2.0.0
[3.0.0-alpha1]: https://github.com/3scale/apicast/compare/v2.0.0...v3.0.0-alpha1
[3.0.0-alpha1]: https://github.com/3scale/apicast/compare/v3.0.0-alpha1...v3.0.0-alpha2
[3.0.0-alpha2]: https://github.com/3scale/apicast/compare/v3.0.0-alpha1...v3.0.0-alpha2
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