From 3636949548631c33e6ac9d6e07e144d078fc64b9 Mon Sep 17 00:00:00 2001 From: Chrono Date: Wed, 8 Jan 2025 12:03:27 +0800 Subject: [PATCH] fix(clustering/rpc): dont start too many timers for sync (#14089) KAG-6114 --- kong/clustering/services/sync/rpc.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 108c54d7fdd..5af99ca68c6 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -369,7 +369,7 @@ local sync_once_impl local function start_sync_once_timer(retry_count) - local ok, err = ngx.timer.at(0, sync_once_impl, retry_count or 0) + local ok, err = kong.timer:at(0, sync_once_impl, retry_count or 0) if not ok then return nil, err end @@ -385,6 +385,8 @@ function sync_once_impl(premature, retry_count) sync_handler() + -- check if "kong.sync.v2.notify_new_version" updates the latest version + local latest_notified_version = ngx.shared.kong:get(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY) if not latest_notified_version then ngx_log(ngx_DEBUG, "no version notified yet") @@ -409,7 +411,15 @@ end function _M:sync_once(delay) - return ngx.timer.at(delay or 0, sync_once_impl, 0) + local name = "rpc_sync_v2_once" + local is_managed = kong.timer:is_managed(name) + + -- we are running a sync handler + if is_managed then + return true + end + + return kong.timer:named_at(name, delay or 0, sync_once_impl, 0) end