Skip to content

Commit

Permalink
perf: convert some variables local constants
Browse files Browse the repository at this point in the history
  • Loading branch information
chobits committed Mar 28, 2024
1 parent d2738be commit a983e81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
48 changes: 26 additions & 22 deletions kong/resty/dns_client/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local cjson = require("cjson.safe")
local utils = require("kong.resty.dns_client.utils")
local tablex = require("pl.tablex")
local mlcache = require("kong.resty.mlcache")
local resolver = require("resty.dns.resolver")

Expand All @@ -12,12 +11,14 @@ local DEBUG = ngx.DEBUG
local ALERT = ngx.ALERT
local timer_at = ngx.timer.at

local type = type
local pairs = pairs
local ipairs = ipairs
local math_min = math.min
local string_lower = string.lower
local table_insert = table.insert
local type = type
local pairs = pairs
local ipairs = ipairs
local math_min = math.min
local string_lower = string.lower
local table_insert = table.insert
local table_isempty = require("table.isempty")
local tablex_readonly = require("pl.tablex").readonly

local parse_hosts = utils.parse_hosts
local ipv6_bracket = utils.ipv6_bracket
Expand All @@ -36,6 +37,8 @@ local DEFAULT_EMPTY_TTL = 30
-- long-lasting TTL of 10 years for hosts or static IP addresses in cache settings
local LONG_LASTING_TTL = 10 * 365 * 24 * 60 * 60

local PERSISTENT_CACHE_TTL = { ttl = 0 } -- used for mlcache:set

local DEFAULT_ORDER = { "LAST", "SRV", "A", "AAAA", "CNAME" }

local TYPE_SRV = resolver.TYPE_SRV
Expand All @@ -57,6 +60,7 @@ local TYPE_TO_NAME = {
[TYPE_A] = "A",
[TYPE_AAAA] = "AAAA",
[TYPE_CNAME] = "CNAME",
[TYPE_LAST] = "LAST",
}

local HIT_L3 = 3 -- L1 lru, L2 shm, L3 callback, L4 stale
Expand All @@ -73,7 +77,7 @@ local NAME_ERROR_CODE = 3 -- response code 3 as "Name Error" or "NXD
-- client specific error
local CACHE_ONLY_ERROR_CODE = 100
local CACHE_ONLY_ERROR_MESSAGE = "cache only lookup failed"
local CACHE_ONLY_ANSWERS = tablex.readonly({ errcode = CACHE_ONLY_ERROR_CODE, errstr = CACHE_ONLY_ERROR_MESSAGE })
local CACHE_ONLY_ANSWERS = tablex_readonly({ errcode = CACHE_ONLY_ERROR_CODE, errstr = CACHE_ONLY_ERROR_MESSAGE })
local EMPTY_RECORD_ERROR_CODE = 101
local EMPTY_RECORD_ERROR_MESSAGE = "empty record received"

Expand Down Expand Up @@ -116,7 +120,7 @@ end
local function insert_last_type(cache, name, qtype)
local key = "last:" .. name
if TYPE_TO_NAME[qtype] and cache:get(key) ~= qtype then
cache:set(key, { ttl = 0 }, qtype)
cache:set(key, PERSISTENT_CACHE_TTL, qtype)
end
end

Expand Down Expand Up @@ -185,9 +189,9 @@ function _M.new(opts)
end

-- init the resolver options for lua-resty-dns
local nameservers = (opts.nameservers and #opts.nameservers > 0) and
local nameservers = (opts.nameservers and not table_isempty(opts.nameservers)) and
opts.nameservers or resolv.nameservers
if not nameservers or #nameservers == 0 then
if not nameservers or table_isempty(nameservers) then
log(WARN, "Invalid configuration, no nameservers specified")
end

Expand Down Expand Up @@ -259,7 +263,7 @@ function _M.new(opts)
end

-- parse order
if opts.order and #opts.order == 0 then
if opts.order and table_isempty(opts.order) then
return nil, "Invalid order array: empty record types"
end

Expand Down Expand Up @@ -297,11 +301,11 @@ function _M.new(opts)
empty_ttl = opts.empty_ttl or DEFAULT_EMPTY_TTL,
search_types = search_types,
-- quickly accessible constant empty answers
empty_answers = {
EMPTY_ANSWERS = tablex_readonly({
errcode = EMPTY_RECORD_ERROR_CODE,
errstr = EMPTY_RECORD_ERROR_MESSAGE,
ttl = opts.empty_ttl or DEFAULT_EMPTY_TTL,
},
}),
}, mt)
end

Expand All @@ -321,6 +325,12 @@ local function process_answers(self, qname, qtype, answers)
for _, answer in ipairs(answers) do
answer.name = string_lower(answer.name)

if self.valid_ttl then
answer.ttl = self.valid_ttl
else
ttl = math_min(ttl, answer.ttl)
end

if answer.type == TYPE_CNAME then
cname_answer = answer -- use the last one as the real cname

Expand All @@ -339,17 +349,11 @@ local function process_answers(self, qname, qtype, answers)
table_insert(processed_answers, answer)
end
end

if self.valid_ttl then
answer.ttl = self.valid_ttl
else
ttl = math_min(ttl, answer.ttl)
end
end

if #processed_answers == 0 then
if table_isempty(processed_answers) then
if not cname_answer then
return self.empty_answers
return self.EMPTY_ANSWERS
end

processed_answers[1] = cname_answer
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/30-new-dns-client/02-old_client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

local _writefile = require("pl.utils").writefile
local tmpname = require("pl.path").tmpname
local cycle_aware_deep_copy = require("kong.tools.utils").cycle_aware_deep_copy
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy

-- hosted in Route53 in the AWS sandbox
local TEST_DOMAIN = "kong-gateway-testing.link"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
local utils = require("kong.tools.utils")
local _writefile = require("pl.utils").writefile
local tmpname = require("pl.path").tmpname
local cycle_aware_deep_copy = require("kong.tools.utils").cycle_aware_deep_copy
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy

-- hosted in Route53 in the AWS sandbox
local TEST_NS = "198.51.100.0"
Expand Down

0 comments on commit a983e81

Please sign in to comment.