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

feat: use checker.name to format periodic lock to distribute active check task to multiple workers #44

Closed
wants to merge 3 commits into from

Conversation

nic-6443
Copy link

@nic-6443 nic-6443 commented Dec 26, 2023

Currently, using a constant as self.PERIODIC_LOCK causes all active health tasks to be processed by only one worker. Even if other workers receive a request associated with another checker, they cannot start the health check actually. ref:

local function get_periodic_lock(shm, key)
local my_pid = ngx_worker_pid()
local checker_pid = shm:get(key)
if checker_pid == nil then
-- no worker is checking, try to acquire the lock
local ok, err = shm:add(key, my_pid, LOCK_PERIOD)
if not ok then
if err == "exists" then
-- another worker got the lock before
return false
end
ngx_log(ERR, "failed to add key '", key, "': ", err)
return nil, err
end
elseif checker_pid ~= my_pid then
-- another worker is checking
return false
end
return true
end

local shm, key = self.shm, self.PERIODIC_LOCK
last_cleanup_check = ngx_now()
active_check_timer, err = resty_timer({
recurring = true,
interval = CHECK_INTERVAL,
jitter = CHECK_JITTER,
detached = false,
expire = function()
if get_periodic_lock(shm, key) then
active_check_timer.interval = CHECK_INTERVAL
renew_periodic_lock(shm, key)
else
active_check_timer.interval = CHECK_INTERVAL * 10
return
end

self.PERIODIC_LOCK = SHM_PREFIX .. ":period_lock:"

…heck task to multiple workers

Signed-off-by: Nic <qianyong@api7.ai>
f
Signed-off-by: Nic <qianyong@api7.ai>
@nic-6443 nic-6443 requested review from monkeyDluffy6017 and AlinsRan and removed request for monkeyDluffy6017 and AlinsRan December 26, 2023 12:13
@nic-6443 nic-6443 marked this pull request as draft December 27, 2023 01:36
f
Signed-off-by: Nic <qianyong@api7.ai>
@nic-6443 nic-6443 closed this Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant