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

refactor(dns): remove unnecessary DNS client initialization #13479

Merged
merged 11 commits into from
Aug 19, 2024
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-dns-initialization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Removed unnecessary dns initialization
type: performance
scope: Core
1 change: 1 addition & 0 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ return function(options)

local client = package.loaded["kong.resty.dns.client"]
if not client then
-- dns initialization here is essential for busted tests.
client = require("kong.tools.dns")()
end

Expand Down
6 changes: 4 additions & 2 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ _G.kong = kong_global.new() -- no versioned PDK for plugins for now


local DB = require "kong.db"
local dns = require "kong.tools.dns"
local meta = require "kong.meta"
local lapis = require "lapis"
local runloop = require "kong.runloop.handler"
Expand Down Expand Up @@ -645,6 +644,10 @@ function Kong.init()
local conf_path = pl_path.join(ngx.config.prefix(), ".kong_env")
local config = assert(conf_loader(conf_path, nil, { from_kong_env = true }))

-- The dns client has been initialized in conf_loader, so we set it directly.
-- Other modules should use 'kong.dns' to avoid reinitialization.
kong.dns = assert(package.loaded["kong.resty.dns.client"])
ProBrian marked this conversation as resolved.
Show resolved Hide resolved

reset_kong_shm(config)

-- special math.randomseed from kong.globalpatches not taking any argument.
Expand Down Expand Up @@ -683,7 +686,6 @@ function Kong.init()
assert(db:connect())

kong.db = db
kong.dns = dns(config)

if config.proxy_ssl_enabled or config.stream_ssl_enabled then
certificate.init()
Expand Down
2 changes: 1 addition & 1 deletion kong/runloop/balancer/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local resolve_timer_running
local queryDns

function targets_M.init()
dns_client = require("kong.tools.dns")(kong.configuration) -- configure DNS client
dns_client = assert(package.loaded["kong.resty.dns.client"])
ProBrian marked this conversation as resolved.
Show resolved Hide resolved
if renewal_heap:size() > 0 then
renewal_heap = require("binaryheap").minUnique()
renewal_weak_cache = setmetatable({}, { __mode = "v" })
Expand Down
8 changes: 1 addition & 7 deletions kong/timing/hooks/dns.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
local _M = {}

local timing

local client = package.loaded["kong.resty.dns.client"]
if not client then
client = require("kong.tools.dns")()
end


local function before_toip(qname, _port, _dnsCacheOnly, _try_list)
timing.enter_context("dns")
timing.enter_context(qname)
Expand All @@ -30,6 +23,7 @@ function _M.register_hooks(timing_module)
Here is the signature of the `toip()` function:
function toip(self, qname, port, dnsCacheOnly, try_list)
--]]
local client = assert(package.loaded["kong.resty.dns.client"])
chobits marked this conversation as resolved.
Show resolved Hide resolved
req_dyn_hook.hook_function("timing", client, "toip", 4, {
befores = { before_toip },
afters = { after_toip },
Expand Down
Loading