Skip to content

Commit

Permalink
refactor(tools): separate yield functions from utils (#11747)
Browse files Browse the repository at this point in the history
Clean the huge utils.lua
  • Loading branch information
chronolaw authored Oct 27, 2023
1 parent 310a50b commit ed798ec
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
1 change: 1 addition & 0 deletions kong-3.6.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ build = {
["kong.tools.request_aware_table"] = "kong/tools/request_aware_table.lua",
["kong.tools.string"] = "kong/tools/string.lua",
["kong.tools.table"] = "kong/tools/table.lua",
["kong.tools.yield"] = "kong/tools/yield.lua",

["kong.runloop.handler"] = "kong/runloop/handler.lua",
["kong.runloop.events"] = "kong/runloop/events.lua",
Expand Down
64 changes: 10 additions & 54 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1346,57 +1346,6 @@ function _M.sort_by_handler_priority(a, b)
return prio_a > prio_b
end

---
-- 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)
if LUA_CONTEXT_YIELDABLE_PHASE[phase or get_phase()] == nil then
return false
end
return true
end
end

_M.in_yieldable_phase = in_yieldable_phase

do
local ngx_sleep = _G.native_ngx_sleep or ngx.sleep

local YIELD_ITERATIONS = 1000
local counter = YIELD_ITERATIONS

function _M.yield(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)
end
end

local time_ns
do
Expand Down Expand Up @@ -1545,9 +1494,16 @@ _M.get_updated_monotonic_ms = get_updated_monotonic_ms


do
local tbl = require "kong.tools.table"
for name, func in pairs(tbl) do
_M[name] = func
local modules = {
"kong.tools.table",
"kong.tools.yield",
}

for _, str in ipairs(modules) do
local mod = require(str)
for name, func in pairs(mod) do
_M[name] = func
end
end
end

Expand Down
59 changes: 59 additions & 0 deletions kong/tools/yield.lua
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

1 comment on commit ed798ec

@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:ed798ec4bba611603d465395d21c5065a33d8287
Artifacts available https://github.com/Kong/kong/actions/runs/6664564296

Please sign in to comment.