-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tools): separate yield functions from utils (#11747)
Clean the huge utils.lua
- Loading branch information
Showing
3 changed files
with
70 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local _M = {} | ||
|
||
|
||
--- | ||
-- Check if the phase is yieldable. | ||
-- @tparam string phase the phase to check, if not specified then | ||
-- the default value will be the current phase | ||
-- @treturn boolean true if the phase is yieldable, false otherwise | ||
local in_yieldable_phase | ||
do | ||
local get_phase = ngx.get_phase | ||
|
||
-- https://github.com/openresty/lua-nginx-module/blob/c89469e920713d17d703a5f3736c9335edac22bf/src/ngx_http_lua_util.h#L35C10-L35C10 | ||
local LUA_CONTEXT_YIELDABLE_PHASE = { | ||
rewrite = true, | ||
server_rewrite = true, | ||
access = true, | ||
content = true, | ||
timer = true, | ||
ssl_client_hello = true, | ||
ssl_certificate = true, | ||
ssl_session_fetch = true, | ||
preread = true, | ||
} | ||
|
||
in_yieldable_phase = function(phase) | ||
return LUA_CONTEXT_YIELDABLE_PHASE[phase or get_phase()] | ||
end | ||
end | ||
_M.in_yieldable_phase = in_yieldable_phase | ||
|
||
|
||
local yield | ||
do | ||
local ngx_sleep = _G.native_ngx_sleep or ngx.sleep | ||
|
||
local YIELD_ITERATIONS = 1000 | ||
local counter = YIELD_ITERATIONS | ||
|
||
yield = function(in_loop, phase) | ||
if ngx.IS_CLI or not in_yieldable_phase(phase) then | ||
return | ||
end | ||
|
||
if in_loop then | ||
counter = counter - 1 | ||
if counter > 0 then | ||
return | ||
end | ||
counter = YIELD_ITERATIONS | ||
end | ||
|
||
ngx_sleep(0) -- yield | ||
end | ||
end | ||
_M.yield = yield | ||
|
||
|
||
return _M |
ed798ec
There was a problem hiding this comment.
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:ed798ec4bba611603d465395d21c5065a33d8287
Artifacts available https://github.com/Kong/kong/actions/runs/6664564296