From cc4d42f281725a15672e78ed59d4a69c7ac90851 Mon Sep 17 00:00:00 2001 From: windmgc Date: Wed, 20 Sep 2023 16:15:30 +0800 Subject: [PATCH 1/4] tests(vault): add test to start kong with vault referenced postgres config --- .../02-integration/13-vaults/03-mock_spec.lua | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/02-integration/13-vaults/03-mock_spec.lua b/spec/02-integration/13-vaults/03-mock_spec.lua index d8e2c896bfa6..77508422afea 100644 --- a/spec/02-integration/13-vaults/03-mock_spec.lua +++ b/spec/02-integration/13-vaults/03-mock_spec.lua @@ -145,4 +145,26 @@ for _, strategy in helpers.each_strategy() do end) end) end) + + if strategy == "postgres" then + describe("ENV Vault #" .. strategy, function () + describe("Kong Start", function () + it("can resolve reference in init_phase", function () + helpers.setenv("TEST_ENV_VAULT_LOGLEVEL", "debug") + + assert(helpers.start_kong { + database = strategy, + prefix = helpers.test_conf.prefix, + nginx_conf = "spec/fixtures/custom_nginx.template", + vaults = "env", + log_level = "{vault://env/TEST_ENV_VAULT_LOGLEVEL}" + }) + + finally(function () + assert(helpers.stop_kong()) + end) + end) + end) + end) + end end From 34ef3669a1ff8a3be3b22bdff633cd4be207148f Mon Sep 17 00:00:00 2001 From: windmgc Date: Thu, 21 Sep 2023 16:39:10 +0800 Subject: [PATCH 2/4] feat(*): add check_phase_yieldable --- kong/tools/utils.lua | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/kong/tools/utils.lua b/kong/tools/utils.lua index 8b9c3bb9347d..88fc340cb743 100644 --- a/kong/tools/utils.lua +++ b/kong/tools/utils.lua @@ -1648,26 +1648,45 @@ function _M.sort_by_handler_priority(a, b) return prio_a > prio_b end -do +--- +-- 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 check_phase_yieldable do local get_phase = ngx.get_phase - local ngx_sleep = _G.native_ngx_sleep or ngx.sleep - local SLEEP_PHASES = { + -- 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, - ssl_client_hello = true, preread = true, } + check_phase_yieldable = function(phase) + if LUA_CONTEXT_YIELDABLE_PHASE[phase or get_phase()] == nil then + return false + end + return true + end +end + +_M.check_phase_yieldable = check_phase_yieldable + +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 SLEEP_PHASES[phase or get_phase()] == nil then + if ngx.IS_CLI or not check_phase_yieldable(phase) then return end if in_loop then From c7396af6e2b56f651514462ba6ed59f2bf4c8af6 Mon Sep 17 00:00:00 2001 From: windmgc Date: Thu, 21 Sep 2023 16:39:33 +0800 Subject: [PATCH 3/4] fix(*): with_coroutine_mutex bypass any non-yieldable phase --- kong/concurrency.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kong/concurrency.lua b/kong/concurrency.lua index 82bc5ad75fb4..6a7e9e63341c 100644 --- a/kong/concurrency.lua +++ b/kong/concurrency.lua @@ -1,5 +1,6 @@ local resty_lock = require "resty.lock" local ngx_semaphore = require "ngx.semaphore" +local check_phase_yieldable = require("kong.tools.utils").check_phase_yieldable local type = type @@ -7,9 +8,6 @@ local error = error local pcall = pcall -local get_phase = ngx.get_phase - - local concurrency = {} @@ -91,7 +89,7 @@ function concurrency.with_coroutine_mutex(opts, fn) error("invalid value for opts.on_timeout", 2) end - if get_phase() == "init_worker" then + if not check_phase_yieldable() then return fn() end From a29b22812892c0dd2e0a5e3d71401a24928742c0 Mon Sep 17 00:00:00 2001 From: windmgc Date: Mon, 25 Sep 2023 16:14:33 +0800 Subject: [PATCH 4/4] fix(*): rename function to in_yieldable_phase --- kong/concurrency.lua | 4 ++-- kong/tools/utils.lua | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kong/concurrency.lua b/kong/concurrency.lua index 6a7e9e63341c..58077d0aeed5 100644 --- a/kong/concurrency.lua +++ b/kong/concurrency.lua @@ -1,6 +1,6 @@ local resty_lock = require "resty.lock" local ngx_semaphore = require "ngx.semaphore" -local check_phase_yieldable = require("kong.tools.utils").check_phase_yieldable +local in_yieldable_phase = require("kong.tools.utils").in_yieldable_phase local type = type @@ -89,7 +89,7 @@ function concurrency.with_coroutine_mutex(opts, fn) error("invalid value for opts.on_timeout", 2) end - if not check_phase_yieldable() then + if not in_yieldable_phase() then return fn() end diff --git a/kong/tools/utils.lua b/kong/tools/utils.lua index 88fc340cb743..d95a5adcbc85 100644 --- a/kong/tools/utils.lua +++ b/kong/tools/utils.lua @@ -1653,7 +1653,7 @@ end -- @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 check_phase_yieldable do +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 @@ -1669,7 +1669,7 @@ local check_phase_yieldable do preread = true, } - check_phase_yieldable = function(phase) + in_yieldable_phase = function(phase) if LUA_CONTEXT_YIELDABLE_PHASE[phase or get_phase()] == nil then return false end @@ -1677,7 +1677,7 @@ local check_phase_yieldable do end end -_M.check_phase_yieldable = check_phase_yieldable +_M.in_yieldable_phase = in_yieldable_phase do local ngx_sleep = _G.native_ngx_sleep or ngx.sleep @@ -1686,7 +1686,7 @@ do local counter = YIELD_ITERATIONS function _M.yield(in_loop, phase) - if ngx.IS_CLI or not check_phase_yieldable(phase) then + if ngx.IS_CLI or not in_yieldable_phase(phase) then return end if in_loop then