Skip to content

Commit

Permalink
fix: avoid corrupting the default healthcheck conf (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Sep 2, 2022
1 parent 8fd933b commit 173b534
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,48 @@ local function key_for(key_prefix, ip, port, hostname)
end


local deepcopy
do
local function _deepcopy(orig, copied)
-- prevent infinite loop when a field refers its parent
copied[orig] = true
-- If the array-like table contains nil in the middle,
-- the len might be smaller than the expected.
-- But it doesn't affect the correctness.
local len = #orig
local copy = table.new(len, table.nkeys(orig) - len)
for orig_key, orig_value in pairs(orig) do
if type(orig_value) == "table" and not copied[orig_value] then
copy[orig_key] = _deepcopy(orig_value, copied)
else
copy[orig_key] = orig_value
end
end

local mt = getmetatable(orig)
if mt ~= nil then
setmetatable(copy, mt)
end

return copy
end


local copied_recorder = {}

function deepcopy(orig)
local orig_type = type(orig)
if orig_type ~= 'table' then
return orig
end

local res = _deepcopy(orig, copied_recorder)
table.clear(copied_recorder)
return res
end
end


local checker = {}


Expand Down Expand Up @@ -1203,7 +1245,7 @@ local function fill_in_settings(opts, defaults, ctx)
obj[k] = v
end
elseif default ~= NO_DEFAULT then
obj[k] = default
obj[k] = deepcopy(default)
end

end
Expand Down

0 comments on commit 173b534

Please sign in to comment.