Skip to content

Commit

Permalink
refactor sync_once
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Nov 30, 2024
1 parent 35aa605 commit d7f3df9
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions kong/clustering/services/sync/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ local function do_sync()
end


local function sync_handler(premature)
local sync_handler

sync_handler = function(premature, try_counter)
if premature then
return
end
Expand All @@ -397,52 +399,40 @@ local function sync_handler(premature)
if not res and err ~= "timeout" then
ngx_log(ngx_ERR, "unable to create worker mutex and sync: ", err)
end
end


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)
if not ok then
return nil, err
end

return true
end


function sync_once_impl(premature, retry_count)
if premature then
if not try_counter then
return
end

sync_handler()

local latest_notified_version = ngx.shared.kong:get(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY)
local current_version = tonumber(declarative.get_current_hash()) or 0

if not latest_notified_version then
ngx_log(ngx_DEBUG, "no version notified yet")
return
end

local current_version = tonumber(declarative.get_current_hash()) or 0
if current_version >= latest_notified_version then
return
end

-- retry if the version is not updated
if current_version < latest_notified_version then
retry_count = retry_count or 0
if retry_count > MAX_RETRY then
ngx_log(ngx_ERR, "sync_once retry count exceeded. retry_count: ", retry_count)
return
end

return start_sync_once_timer(retry_count + 1)
if try_counter > MAX_RETRY then
ngx_log(ngx_ERR, "sync_once try count exceeded. try_counter: ", try_counter)
return
end

local ok, err = ngx.timer.at(0, sync_handler, try_counter + 1)
if not ok then
return nil, err
end

return true
end


function _M:sync_once(delay)
return ngx.timer.at(delay or 0, sync_once_impl, 0)
return ngx.timer.at(delay or 0, sync_handler, 0)
end


Expand Down

0 comments on commit d7f3df9

Please sign in to comment.