Skip to content

Commit

Permalink
fix(clustering/rpc): sync retry timeout due to block (#14195)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlightIbuki authored Jan 21, 2025
1 parent b728232 commit dfc955e
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions kong/clustering/services/sync/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -374,28 +374,17 @@ 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 = kong.timer:at(0, sync_once_impl, retry_count or 0)
if not ok then
return nil, err
end

return true
return res, err
end


function sync_once_impl(premature, retry_count)
local function sync_once_impl(premature, retry_count)
if premature then
return
end

sync_handler()
local _, err = sync_handler()

-- check if "kong.sync.v2.notify_new_version" updates the latest version

Expand All @@ -413,12 +402,27 @@ function sync_once_impl(premature, retry_count)

-- retry if the version is not updated
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)
-- we do not count a timed out sync. just retry
if err ~= "timeout" then
retry_count = retry_count + 1
end

-- in some cases, the new spawned timer will be switched to immediately,
-- preventing the coroutine who possesses the mutex to run
-- to let other coroutines has a chance to run
local ok, err = kong.timer:at(0.1, sync_once_impl, retry_count or 0)
-- this is a workaround for a timerng bug, where tail recursion causes failure
-- ok could be a string so let's convert it to boolean
if not ok then
return nil, err
end
return true
end


Expand Down

1 comment on commit dfc955e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong-dev:dfc955edb725730af77e31682b185ab87a20bf65
Artifacts available https://github.com/Kong/kong/actions/runs/12882318884

Please sign in to comment.