Skip to content

Commit

Permalink
fix(utils): yield to sleep only when the ngx.sleep is available (#11392)
Browse files Browse the repository at this point in the history
### Summary

Previously the sleeping was skipped only on phases `init` and `init_worker`,
and on CLI, but there are a lot more phases in Nginx where the ngx.sleep
is not allowed. This changes the `utils.yield` to skip `ngx.sleep`ing on
such faces. I checked that there was as many phases where it was allowed
as there was phases where it was disallowed, so I ended up with a list of
where it is allowed as that is easier to update by just looking:
https://github.com/openresty/lua-nginx-module#ngxsleep (+ the preread phase)

Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
  • Loading branch information
bungle authored Aug 11, 2023
1 parent 3d33efe commit 44ad22c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1652,15 +1652,22 @@ do
local get_phase = ngx.get_phase
local ngx_sleep = _G.native_ngx_sleep or ngx.sleep

local SLEEP_PHASES = {
rewrite = true,
access = true,
content = true,
timer = true,
ssl_certificate = true,
ssl_session_fetch = true,
ssl_client_hello = true,
preread = true,
}

local YIELD_ITERATIONS = 1000
local counter = YIELD_ITERATIONS

function _M.yield(in_loop, phase)
if ngx.IS_CLI then
return
end
phase = phase or get_phase()
if phase == "init" or phase == "init_worker" then
if ngx.IS_CLI or SLEEP_PHASES[phase or get_phase()] == nil then
return
end
if in_loop then
Expand Down

1 comment on commit 44ad22c

@khcp-gha-bot
Copy link

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:44ad22c3c5bbf152b451ee764921b47c9df6c3a2
Artifacts available https://github.com/Kong/kong/actions/runs/5832414103

Please sign in to comment.