From 98699818225868441364666da7f4ff0807786718 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Fri, 9 Aug 2024 14:50:10 +0800 Subject: [PATCH 01/11] remove unnecessary dns initiazion --- kong/init.lua | 2 +- kong/runloop/balancer/targets.lua | 2 +- kong/timing/hooks/dns.lua | 6 +----- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/kong/init.lua b/kong/init.lua index 2a68f2acadf4..a9ae45d4482e 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -644,6 +644,7 @@ function Kong.init() -- retrieve kong_config local conf_path = pl_path.join(ngx.config.prefix(), ".kong_env") local config = assert(conf_loader(conf_path, nil, { from_kong_env = true })) + kong.dns = assert(package.loaded["kong.resty.dns.client"]) reset_kong_shm(config) @@ -683,7 +684,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() diff --git a/kong/runloop/balancer/targets.lua b/kong/runloop/balancer/targets.lua index dea4febb36eb..070ff79939c5 100644 --- a/kong/runloop/balancer/targets.lua +++ b/kong/runloop/balancer/targets.lua @@ -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"]) if renewal_heap:size() > 0 then renewal_heap = require("binaryheap").minUnique() renewal_weak_cache = setmetatable({}, { __mode = "v" }) diff --git a/kong/timing/hooks/dns.lua b/kong/timing/hooks/dns.lua index e9eb0c17c3b3..fa8791c1a6b3 100644 --- a/kong/timing/hooks/dns.lua +++ b/kong/timing/hooks/dns.lua @@ -2,11 +2,7 @@ local _M = {} local timing -local client = package.loaded["kong.resty.dns.client"] -if not client then - client = require("kong.tools.dns")() -end - +local client = assert(package.loaded["kong.resty.dns.client"]) local function before_toip(qname, _port, _dnsCacheOnly, _try_list) timing.enter_context("dns") From a397dc2c5ded40089e1ca1e2f8ca41cc301558d8 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Fri, 9 Aug 2024 15:13:08 +0800 Subject: [PATCH 02/11] remove unused variable --- kong/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/kong/init.lua b/kong/init.lua index a9ae45d4482e..8d7ca719fd40 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -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" From 850c9afa268fa6f1f014da01a256251c2bc2288f Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 10:11:39 +0800 Subject: [PATCH 03/11] refine due to PR comments --- kong/init.lua | 3 +++ kong/runloop/balancer/targets.lua | 2 +- kong/timing/hooks/dns.lua | 3 +-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kong/init.lua b/kong/init.lua index 8d7ca719fd40..1cabfa0c2054 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -643,6 +643,9 @@ function Kong.init() -- retrieve kong_config 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"]) reset_kong_shm(config) diff --git a/kong/runloop/balancer/targets.lua b/kong/runloop/balancer/targets.lua index 070ff79939c5..abe107133438 100644 --- a/kong/runloop/balancer/targets.lua +++ b/kong/runloop/balancer/targets.lua @@ -48,7 +48,7 @@ local resolve_timer_running local queryDns function targets_M.init() - dns_client = assert(package.loaded["kong.resty.dns.client"]) + dns_client = assert(kong.dns) if renewal_heap:size() > 0 then renewal_heap = require("binaryheap").minUnique() renewal_weak_cache = setmetatable({}, { __mode = "v" }) diff --git a/kong/timing/hooks/dns.lua b/kong/timing/hooks/dns.lua index fa8791c1a6b3..40d522b9135a 100644 --- a/kong/timing/hooks/dns.lua +++ b/kong/timing/hooks/dns.lua @@ -2,8 +2,6 @@ local _M = {} local timing -local client = assert(package.loaded["kong.resty.dns.client"]) - local function before_toip(qname, _port, _dnsCacheOnly, _try_list) timing.enter_context("dns") timing.enter_context(qname) @@ -26,6 +24,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(kong.dns) req_dyn_hook.hook_function("timing", client, "toip", 4, { befores = { before_toip }, afters = { after_toip }, From dfd02bbcbc8eee5aabb662f8654062ee0c8d2b86 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 10:51:58 +0800 Subject: [PATCH 04/11] change dns client init in timing hooks to avoid error --- kong/timing/hooks/dns.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kong/timing/hooks/dns.lua b/kong/timing/hooks/dns.lua index 40d522b9135a..9c7fd20f0cfe 100644 --- a/kong/timing/hooks/dns.lua +++ b/kong/timing/hooks/dns.lua @@ -1,5 +1,4 @@ local _M = {} - local timing local function before_toip(qname, _port, _dnsCacheOnly, _try_list) @@ -24,7 +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(kong.dns) + local client = assert(package.loaded["kong.resty.dns.client"]) req_dyn_hook.hook_function("timing", client, "toip", 4, { befores = { before_toip }, afters = { after_toip }, From e02c80af9d885ec80043978ae4a3d8ba94c261c6 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 11:34:36 +0800 Subject: [PATCH 05/11] change dns client init in balancer module to avoid UT failure --- kong/runloop/balancer/targets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/runloop/balancer/targets.lua b/kong/runloop/balancer/targets.lua index abe107133438..070ff79939c5 100644 --- a/kong/runloop/balancer/targets.lua +++ b/kong/runloop/balancer/targets.lua @@ -48,7 +48,7 @@ local resolve_timer_running local queryDns function targets_M.init() - dns_client = assert(kong.dns) + dns_client = assert(package.loaded["kong.resty.dns.client"]) if renewal_heap:size() > 0 then renewal_heap = require("binaryheap").minUnique() renewal_weak_cache = setmetatable({}, { __mode = "v" }) From 45dd36b6a4bbee69e2abd11bd28cc741fca7b8b6 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 15:21:19 +0800 Subject: [PATCH 06/11] remove extra dns init code in globalpatch --- kong/globalpatches.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 8d2a318568e3..561eea8a74b1 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -521,7 +521,8 @@ return function(options) local client = package.loaded["kong.resty.dns.client"] if not client then - client = require("kong.tools.dns")() + -- just require without dns client init, because we only want `toip` function + client = require("kong.resty.dns.client") end --- Patch the TCP connect and UDP setpeername methods such that all From 741e2780e26089fb0df6bab80bd9ff24600f075c Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 15:47:35 +0800 Subject: [PATCH 07/11] Revert "remove extra dns init code in globalpatch" This reverts commit 20bfcea0bd2c4a5ac13bdf1120af06e33f5541ff. --- kong/globalpatches.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 561eea8a74b1..8d2a318568e3 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -521,8 +521,7 @@ return function(options) local client = package.loaded["kong.resty.dns.client"] if not client then - -- just require without dns client init, because we only want `toip` function - client = require("kong.resty.dns.client") + client = require("kong.tools.dns")() end --- Patch the TCP connect and UDP setpeername methods such that all From 9239e134bccf64e375f8ed76dd0d6f421a8a8a68 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Tue, 13 Aug 2024 15:54:29 +0800 Subject: [PATCH 08/11] revert dns init elimination due to test dependency --- kong/globalpatches.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 8d2a318568e3..86ceacd0f845 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -521,6 +521,7 @@ return function(options) local client = package.loaded["kong.resty.dns.client"] if not client then + -- dns initialized here, can't be eliminated due to test dependencies client = require("kong.tools.dns")() end From 1bd9468291867e1625ed03141f54ea78ac49dcd1 Mon Sep 17 00:00:00 2001 From: ProBrian Date: Wed, 14 Aug 2024 11:47:16 +0800 Subject: [PATCH 09/11] refine annotations --- kong/globalpatches.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 86ceacd0f845..61ca747fd4d2 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -521,7 +521,7 @@ return function(options) local client = package.loaded["kong.resty.dns.client"] if not client then - -- dns initialized here, can't be eliminated due to test dependencies + -- dns initialization here is essential for busted tests. client = require("kong.tools.dns")() end From b54b92ab63c5805396df629c653902ab85786575 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Mon, 19 Aug 2024 11:45:31 +0800 Subject: [PATCH 10/11] added changelog --- changelog/unreleased/kong/fix-dns-initialization.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/unreleased/kong/fix-dns-initialization.yml diff --git a/changelog/unreleased/kong/fix-dns-initialization.yml b/changelog/unreleased/kong/fix-dns-initialization.yml new file mode 100644 index 000000000000..bd6c586d3cb8 --- /dev/null +++ b/changelog/unreleased/kong/fix-dns-initialization.yml @@ -0,0 +1,3 @@ +message: Removed unnecessary dns initialization +type: performance +scope: Core From 476df5551ee680d3b7dff19e9b6d83130771a4b5 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Mon, 19 Aug 2024 11:47:38 +0800 Subject: [PATCH 11/11] refine changelog --- changelog/unreleased/kong/fix-dns-initialization.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-dns-initialization.yml b/changelog/unreleased/kong/fix-dns-initialization.yml index bd6c586d3cb8..0ee27c14e272 100644 --- a/changelog/unreleased/kong/fix-dns-initialization.yml +++ b/changelog/unreleased/kong/fix-dns-initialization.yml @@ -1,3 +1,3 @@ -message: Removed unnecessary dns initialization +message: Removed unnecessary DNS client initialization type: performance scope: Core