From 23b2ae112ebfc12a279fb5625d12570f029e71d8 Mon Sep 17 00:00:00 2001 From: Chrono Date: Wed, 27 Nov 2024 10:12:49 +0800 Subject: [PATCH] fix(clustering/rpc): try sync after rpc is ready (#13901) KAG-5569, KAG-5724 --- kong/clustering/services/sync/init.lua | 3 +-- kong/clustering/services/sync/rpc.lua | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/kong/clustering/services/sync/init.lua b/kong/clustering/services/sync/init.lua index 40f1b836241b..06802f6088db 100644 --- a/kong/clustering/services/sync/init.lua +++ b/kong/clustering/services/sync/init.lua @@ -7,8 +7,7 @@ local strategy = require("kong.clustering.services.sync.strategies.postgres") local rpc = require("kong.clustering.services.sync.rpc") --- TODO: what is the proper value? -local FIRST_SYNC_DELAY = 0.5 -- seconds +local FIRST_SYNC_DELAY = 0.2 -- seconds local EACH_SYNC_DELAY = 30 -- seconds diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 1e80906b4649..5783107f0caf 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -181,7 +181,27 @@ function _M:init(manager, is_cp) end +-- check if rpc connection is ready +local function is_rpc_ready() + for i = 1, 5 do + local res = kong.rpc:get_peers() + + -- control_plane is ready + if res["control_plane"] then + return true + end + + -- retry later + ngx.sleep(0.1 * i) + end +end + + local function do_sync() + if not is_rpc_ready() then + return nil, "rpc is not ready" + end + local ns_deltas, err = kong.rpc:call("control_plane", "kong.sync.v2.get_delta", { default = { version = @@ -394,8 +414,6 @@ end function _M:sync_once(delay) - --- XXX TODO: check rpc connection is ready - return start_sync_timer(ngx.timer.at, delay or 0) end