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

[bugfix]add wait_add_target_list to store the target #42

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 30 additions & 2 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ end

local checker = {}

--use the wait_add_target_list to store the target before added to the target_list actually.
checker.wait_add_target_list = {}

------------------------------------------------------------------------------
-- Node management.
Expand Down Expand Up @@ -243,8 +245,15 @@ end

--- Get a target
local function get_target(self, ip, port, hostname)

hostname = hostname or ip
return ((self.targets[ip] or EMPTY)[port] or EMPTY)[hostname]

if not self.targets then
return ((self.wait_add_target_list[ip] or EMPTY)[port] or EMPTY)[hostname]
end

return ((self.targets[ip] or EMPTY)[port] or EMPTY)[hostname] or
((self.wait_add_target_list[ip] or EMPTY)[port] or EMPTY)[hostname]
end

--- Add a target to the healthchecker.
Expand All @@ -267,6 +276,18 @@ function checker:add_target(ip, port, hostname, is_healthy, hostheader)
end

local internal_health = is_healthy and "healthy" or "unhealthy"

--use the wait_add_target_list to store the target before added to the target_list actually.
self.wait_add_target_list[ip] = self.wait_add_target_list[ip] or {}
self.wait_add_target_list[ip][port] = self.wait_add_target_list[ip][port] or {}
local id = hostname or ip
self.wait_add_target_list[id][port][id] = {
ip = ip,
port = port,
hostname = hostname,
hostheader = hostheader,
internal_health = internal_health
}

local ok, err = locking_target_list(self, function(target_list)

Expand All @@ -287,7 +308,14 @@ function checker:add_target(ip, port, hostname, is_healthy, hostheader)
if not ok then
self:log(ERR, "failed to set initial health status in shm: ", err)
end

-- to clear wait_add_target_list
self.wait_add_target_list[ip][port][id] = nil
if #self.wait_add_target_list[ip][port] == 0 then
self.wait_add_target_list[ip][port] = nil
end
if #self.wait_add_target_list[ip] == 0 then
self.wait_add_target_list[ip] = nil
end
-- target does not exist, go add it
target_list[#target_list + 1] = {
ip = ip,
Expand Down