From e74171791a2674d8583164a6c0f903aac5ac55d9 Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:09:55 +0800 Subject: [PATCH 1/9] style(dynamic_hook): remove trailing spaces --- kong/dynamic_hook/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index d5cd940b0f13..5fa6a2acd09a 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -37,7 +37,7 @@ local function should_execute_original_func(group_name) if ALWAYS_ENABLED_GROUPS[group_name] then return end - + local phase = ngx_get_phase() if phase == "init" or phase == "init_worker" then return true @@ -58,7 +58,7 @@ end local function execute_hook_vararg(hook, hook_type, group_name, ...) if not hook then return - end + end local ok, err = pcall(hook, ...) if not ok then ngx_log(ngx_WARN, "failed to run ", hook_type, " hook of ", group_name, ": ", err) @@ -132,7 +132,7 @@ local function execute_original_func(max_args, original_func, a1, a2, a3, a4, a5 return original_func(a1, a2, a3, a4, a5, a6) elseif max_args == 7 then return original_func(a1, a2, a3, a4, a5, a6, a7) - else + else return original_func(a1, a2, a3, a4, a5, a6, a7, a8) end end @@ -142,7 +142,7 @@ local function wrap_function(max_args, group_name, original_func, handlers) return function(a1, a2, a3, a4, a5, a6, a7, a8) if should_execute_original_func(group_name) then a1, a2, a3, a4, a5, a6, a7, a8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) - + else execute_hook(handlers.before_mut, "before_mut", group_name, a1, a2, a3, a4, a5, a6, a7, a8) execute_hooks(handlers.befores, "before", group_name, a1, a2, a3, a4, a5, a6, a7, a8) From 4234b2be54a10575b32e5571b545e895fe4748fb Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:11:43 +0800 Subject: [PATCH 2/9] style(dynamic_hook): align assignment statements --- kong/dynamic_hook/init.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index 5fa6a2acd09a..d013c78e7849 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -1,20 +1,20 @@ -local ngx = ngx -local type = type -local pcall = pcall -local select = select -local ipairs = ipairs -local assert = assert -local ngx_log = ngx.log -local ngx_WARN = ngx.WARN +local ngx = ngx +local type = type +local pcall = pcall +local select = select +local ipairs = ipairs +local assert = assert +local ngx_log = ngx.log +local ngx_WARN = ngx.WARN local ngx_get_phase = ngx.get_phase local _M = { TYPE = { - BEFORE = 1, - AFTER = 2, - BEFORE_MUT = 3, - AFTER_MUT = 4, + BEFORE = 1, + AFTER = 2, + BEFORE_MUT = 3, + AFTER_MUT = 4, }, } From f4ad0f88f93beeb520f8297ec143a7d545e70846 Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:22:05 +0800 Subject: [PATCH 3/9] style(dynamic_hook): add blank lines --- kong/dynamic_hook/init.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index d013c78e7849..24ca843c8089 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -59,6 +59,7 @@ local function execute_hook_vararg(hook, hook_type, group_name, ...) if not hook then return end + local ok, err = pcall(hook, ...) if not ok then ngx_log(ngx_WARN, "failed to run ", hook_type, " hook of ", group_name, ": ", err) @@ -70,6 +71,7 @@ local function execute_hooks_vararg(hooks, hook_type, group_name, ...) if not hooks then return end + for _, hook in ipairs(hooks) do execute_hook_vararg(hook, hook_type, group_name, ...) end @@ -88,6 +90,7 @@ local function wrap_function_vararg(group_name, original_func, handlers) if should_execute_original_func(group_name) then return original_func(...) end + execute_hooks_vararg(handlers.befores, "before", group_name, ...) return execute_after_hooks_vararg(handlers, group_name, original_func(...)) end @@ -98,6 +101,7 @@ local function execute_hook(hook, hook_type, group_name, a1, a2, a3, a4, a5, a6, if not hook then return end + local ok, err = pcall(hook, a1, a2, a3, a4, a5, a6, a7, a8) if not ok then ngx_log(ngx_WARN, "failed to run ", hook_type, " hook of ", group_name, ": ", err) @@ -109,6 +113,7 @@ local function execute_hooks(hooks, hook_type, group_name, a1, a2, a3, a4, a5, a if not hooks then return end + for _, hook in ipairs(hooks) do execute_hook(hook, hook_type, group_name, a1, a2, a3, a4, a5, a6, a7, a8) end @@ -150,6 +155,7 @@ local function wrap_function(max_args, group_name, original_func, handlers) execute_hook(handlers.after_mut, "after_mut", group_name, a1, a2, a3, a4, a5, a6, a7, a8) execute_hooks(handlers.afters, "after", group_name, a1, a2, a3, a4, a5, a6, a7, a8) end + return a1, a2, a3, a4, a5, a6, a7, a8 end end @@ -234,6 +240,7 @@ function _M.run_hooks(group_name, hook_name, a1, a2, a3, a4, a5, a6, a7, a8, ... else ok, err = pcall(handler, a1, a2, a3, a4, a5, a6, a7, a8, ...) end + if not ok then ngx_log(ngx_WARN, "failed to run dynamic hook ", group_name, ".", hook_name, ": ", err) end From e3f1a7a5ca339349141a09d51d4217f230b6989e Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:26:29 +0800 Subject: [PATCH 4/9] feat(dynamic_hook): remove hooks types `before_mut` and `after_mut` We haven't used this feature until now, so I removed them to simplify the code logic. --- kong/dynamic_hook/init.lua | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index 24ca843c8089..83ae7faf1469 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -13,8 +13,6 @@ local _M = { TYPE = { BEFORE = 1, AFTER = 2, - BEFORE_MUT = 3, - AFTER_MUT = 4, }, } @@ -79,7 +77,6 @@ end local function execute_after_hooks_vararg(handlers, group_name, ...) - execute_hook_vararg(handlers.after_mut, "after_mut", group_name, ...) execute_hooks_vararg(handlers.afters, "after", group_name, ...) return ... end @@ -149,10 +146,8 @@ local function wrap_function(max_args, group_name, original_func, handlers) a1, a2, a3, a4, a5, a6, a7, a8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) else - execute_hook(handlers.before_mut, "before_mut", group_name, a1, a2, a3, a4, a5, a6, a7, a8) execute_hooks(handlers.befores, "before", group_name, a1, a2, a3, a4, a5, a6, a7, a8) a1, a2, a3, a4, a5, a6, a7, a8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) - execute_hook(handlers.after_mut, "after_mut", group_name, a1, a2, a3, a4, a5, a6, a7, a8) execute_hooks(handlers.afters, "after", group_name, a1, a2, a3, a4, a5, a6, a7, a8) end @@ -166,9 +161,7 @@ function _M.hook_function(group_name, parent, child_key, max_args, handlers) assert(type(child_key) == "string", "child_key must be a string") local is_varargs = max_args == "varargs" - if is_varargs then - assert(handlers.before_mut == nil, "before_mut is not supported for varargs functions") - else + if not is_varargs then assert(type(max_args) == "number", 'max_args must be a number or "varargs"') assert(max_args >= 0 and max_args <= 8, 'max_args must be >= 0 and <= 8, or "varargs"') end From 42ad4138e78f795b8ce934db08c2f9186249921b Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:30:15 +0800 Subject: [PATCH 5/9] fix(dynamic_hook): fix incorrect return values of hooked functions --- kong/dynamic_hook/init.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index 83ae7faf1469..e9b7081da74a 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -142,16 +142,18 @@ end local function wrap_function(max_args, group_name, original_func, handlers) return function(a1, a2, a3, a4, a5, a6, a7, a8) + local r1, r2, r3, r4, r5, r6, r7, r8 + if should_execute_original_func(group_name) then - a1, a2, a3, a4, a5, a6, a7, a8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) + r1, r2, r3, r4, r5, r6, r7, r8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) else execute_hooks(handlers.befores, "before", group_name, a1, a2, a3, a4, a5, a6, a7, a8) - a1, a2, a3, a4, a5, a6, a7, a8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) - execute_hooks(handlers.afters, "after", group_name, a1, a2, a3, a4, a5, a6, a7, a8) + r1, r2, r3, r4, r5, r6, r7, r8 = execute_original_func(max_args, original_func, a1, a2, a3, a4, a5, a6, a7, a8) + execute_hooks(handlers.afters, "after", group_name, r1, r2, r3, r4, r5, r6, r7, r8) end - return a1, a2, a3, a4, a5, a6, a7, a8 + return r1, r2, r3, r4, r5, r6, r7, r8 end end From 31e1ba4775ca52b81641207cb6b175b6e7c73d2b Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:35:17 +0800 Subject: [PATCH 6/9] refactor(dynamic_hook): introduce more assertions for robustness --- kong/dynamic_hook/init.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index e9b7081da74a..d8041e3def8c 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -159,6 +159,7 @@ end function _M.hook_function(group_name, parent, child_key, max_args, handlers) + assert(type(group_name) == "string", "group_name must be a string") assert(type(parent) == "table", "parent must be a table") assert(type(child_key) == "string", "child_key must be a string") @@ -195,6 +196,8 @@ end function _M.is_group_enabled(group_name) + assert(type(group_name) == "string", "group_name must be a string") + if ALWAYS_ENABLED_GROUPS[group_name] then return true end @@ -214,6 +217,9 @@ end function _M.run_hooks(group_name, hook_name, a1, a2, a3, a4, a5, a6, a7, a8, ...) + assert(type(group_name) == "string", "group_name must be a string") + assert(type(hook_name) == "string", "hook_name must be a string") + if not _M.is_group_enabled(group_name) then return end @@ -243,6 +249,8 @@ end function _M.enable_on_this_request(group_name, ngx_ctx) + assert(type(group_name) == "string", "group_name must be a string") + ngx_ctx = ngx_ctx or ngx.ctx if ngx_ctx.dynamic_hook then ngx_ctx.dynamic_hook.enabled_groups[group_name] = true @@ -257,6 +265,8 @@ end function _M.always_enable(group_name) + assert(type(group_name) == "string", "group_name must be a string") + ALWAYS_ENABLED_GROUPS[group_name] = true end From abc0c0d1de1fb03eda0da249a1edbe38267dbbb8 Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 11:48:13 +0800 Subject: [PATCH 7/9] style(dynamic_code): remove unused code --- kong/dynamic_hook/init.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index d8041e3def8c..cc5ab4683c9d 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -9,12 +9,7 @@ local ngx_WARN = ngx.WARN local ngx_get_phase = ngx.get_phase -local _M = { - TYPE = { - BEFORE = 1, - AFTER = 2, - }, -} +local _M = {} local NON_FUNCTION_HOOKS = { From 36d9b6a426e01c5164c94d2456d326b388b6a9e2 Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 14:54:42 +0800 Subject: [PATCH 8/9] style(dynamic_hook): rename `run_hooks` to `run_hook` as it only run a single hook --- kong/dynamic_hook/init.lua | 2 +- kong/init.lua | 72 +++++++++++++++++++------------------- kong/resty/dns/client.lua | 4 +-- kong/runloop/handler.lua | 8 ++--- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index cc5ab4683c9d..76826226eb76 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -211,7 +211,7 @@ function _M.is_group_enabled(group_name) end -function _M.run_hooks(group_name, hook_name, a1, a2, a3, a4, a5, a6, a7, a8, ...) +function _M.run_hook(group_name, hook_name, a1, a2, a3, a4, a5, a6, a7, a8, ...) assert(type(group_name) == "string", "group_name must be a string") assert(type(hook_name) == "string", "hook_name must be a string") diff --git a/kong/init.lua b/kong/init.lua index 0a16660a1962..225d6683174c 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -135,7 +135,7 @@ local get_start_time_ms = utils.get_start_time_ms local get_updated_now_ms = utils.get_updated_now_ms -local req_dyn_hook_run_hooks = req_dyn_hook.run_hooks +local req_dyn_hook_run_hook = req_dyn_hook.run_hook local req_dyn_hook_is_group_enabled = req_dyn_hook.is_group_enabled @@ -326,7 +326,7 @@ local function execute_global_plugins_iterator(plugins_iterator, phase, ctx) local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:plugin_iterator") + req_dyn_hook_run_hook("timing", "before:plugin_iterator") end for _, plugin, configuration in iterator, plugins, 0 do @@ -338,13 +338,13 @@ local function execute_global_plugins_iterator(plugins_iterator, phase, ctx) setup_plugin_context(ctx, plugin, configuration) if has_timing then - req_dyn_hook_run_hooks("timing", "before:plugin", plugin.name, ctx.plugin_id) + req_dyn_hook_run_hook("timing", "before:plugin", plugin.name, ctx.plugin_id) end plugin.handler[phase](plugin.handler, configuration) if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin") + req_dyn_hook_run_hook("timing", "after:plugin") end reset_plugin_context(ctx, old_ws) @@ -355,7 +355,7 @@ local function execute_global_plugins_iterator(plugins_iterator, phase, ctx) end if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin_iterator") + req_dyn_hook_run_hook("timing", "after:plugin_iterator") end end @@ -376,7 +376,7 @@ local function execute_collecting_plugins_iterator(plugins_iterator, phase, ctx) local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:plugin_iterator") + req_dyn_hook_run_hook("timing", "before:plugin_iterator") end for _, plugin, configuration in iterator, plugins, 0 do @@ -389,14 +389,14 @@ local function execute_collecting_plugins_iterator(plugins_iterator, phase, ctx) setup_plugin_context(ctx, plugin, configuration) if has_timing then - req_dyn_hook_run_hooks( "timing", "before:plugin", plugin.name, ctx.plugin_id) + req_dyn_hook_run_hook( "timing", "before:plugin", plugin.name, ctx.plugin_id) end local co = coroutine.create(plugin.handler[phase]) local cok, cerr = coroutine.resume(co, plugin.handler, configuration) if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin") + req_dyn_hook_run_hook("timing", "after:plugin") end if not cok then @@ -426,7 +426,7 @@ local function execute_collecting_plugins_iterator(plugins_iterator, phase, ctx) end if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin_iterator") + req_dyn_hook_run_hook("timing", "after:plugin_iterator") end ctx.delay_response = nil @@ -447,7 +447,7 @@ local function execute_collected_plugins_iterator(plugins_iterator, phase, ctx) local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:plugin_iterator") + req_dyn_hook_run_hook("timing", "before:plugin_iterator") end for _, plugin, configuration in iterator, plugins, 0 do @@ -459,13 +459,13 @@ local function execute_collected_plugins_iterator(plugins_iterator, phase, ctx) setup_plugin_context(ctx, plugin, configuration) if has_timing then - req_dyn_hook_run_hooks("timing", "before:plugin", plugin.name, ctx.plugin_id) + req_dyn_hook_run_hook("timing", "before:plugin", plugin.name, ctx.plugin_id) end plugin.handler[phase](plugin.handler, configuration) if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin") + req_dyn_hook_run_hook("timing", "after:plugin") end reset_plugin_context(ctx, old_ws) @@ -476,7 +476,7 @@ local function execute_collected_plugins_iterator(plugins_iterator, phase, ctx) end if has_timing then - req_dyn_hook_run_hooks("timing", "after:plugin_iterator") + req_dyn_hook_run_hook("timing", "after:plugin_iterator") end end @@ -1119,7 +1119,7 @@ function Kong.rewrite() ctx.KONG_PHASE = PHASES.rewrite local has_timing - req_dyn_hook_run_hooks("timing:auth", "auth") + req_dyn_hook_run_hook("timing:auth", "auth") if req_dyn_hook_is_group_enabled("timing") then ctx.has_timing = true @@ -1127,7 +1127,7 @@ function Kong.rewrite() end if has_timing then - req_dyn_hook_run_hooks("timing", "before:rewrite") + req_dyn_hook_run_hook("timing", "before:rewrite") end kong_resty_ctx.stash_ref(ctx) @@ -1156,7 +1156,7 @@ function Kong.rewrite() ctx.KONG_REWRITE_TIME = ctx.KONG_REWRITE_ENDED_AT - ctx.KONG_REWRITE_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:rewrite") + req_dyn_hook_run_hook("timing", "after:rewrite") end end @@ -1166,7 +1166,7 @@ function Kong.access() local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:access") + req_dyn_hook_run_hook("timing", "before:access") end if not ctx.KONG_ACCESS_START then @@ -1192,7 +1192,7 @@ function Kong.access() ctx.KONG_RESPONSE_LATENCY = ctx.KONG_ACCESS_ENDED_AT - ctx.KONG_PROCESSING_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:access") + req_dyn_hook_run_hook("timing", "after:access") end return flush_delayed_response(ctx) @@ -1208,7 +1208,7 @@ function Kong.access() ctx.buffered_proxying = nil if has_timing then - req_dyn_hook_run_hooks("timing", "after:access") + req_dyn_hook_run_hook("timing", "after:access") end return kong.response.error(503, "no Service found with those values") @@ -1229,7 +1229,7 @@ function Kong.access() local upgrade = var.upstream_upgrade or "" if version < 2 and upgrade == "" then if has_timing then - req_dyn_hook_run_hooks("timing", "after:access") + req_dyn_hook_run_hook("timing", "after:access") end return Kong.response() @@ -1245,7 +1245,7 @@ function Kong.access() end if has_timing then - req_dyn_hook_run_hooks("timing", "after:access") + req_dyn_hook_run_hook("timing", "after:access") end end @@ -1255,7 +1255,7 @@ function Kong.balancer() local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:balancer") + req_dyn_hook_run_hook("timing", "before:balancer") end -- This may be called multiple times, and no yielding here! @@ -1337,7 +1337,7 @@ function Kong.balancer() ctx.KONG_PROXY_LATENCY = ctx.KONG_BALANCER_ENDED_AT - ctx.KONG_PROCESSING_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:balancer") + req_dyn_hook_run_hook("timing", "after:balancer") end return ngx.exit(errcode) @@ -1349,7 +1349,7 @@ function Kong.balancer() ngx_log(ngx_ERR, "failed to set balancer Host header: ", err) if has_timing then - req_dyn_hook_run_hooks("timing", "after:balancer") + req_dyn_hook_run_hook("timing", "after:balancer") end return ngx.exit(500) @@ -1404,7 +1404,7 @@ function Kong.balancer() ctx.KONG_PROXY_LATENCY = ctx.KONG_BALANCER_ENDED_AT - ctx.KONG_PROCESSING_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:balancer") + req_dyn_hook_run_hook("timing", "after:balancer") end return ngx.exit(500) @@ -1444,7 +1444,7 @@ function Kong.balancer() ctx.KONG_PROXY_LATENCY = ctx.KONG_BALANCER_ENDED_AT - ctx.KONG_PROCESSING_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:balancer") + req_dyn_hook_run_hook("timing", "after:balancer") end end @@ -1473,7 +1473,7 @@ do local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:response") + req_dyn_hook_run_hook("timing", "before:response") end local plugins_iterator = runloop.get_plugins_iterator() @@ -1494,7 +1494,7 @@ do ngx.status = res.status or 502 if has_timing then - req_dyn_hook_run_hooks("timing", "after:response") + req_dyn_hook_run_hook("timing", "after:response") end return kong_error_handlers(ctx) @@ -1548,7 +1548,7 @@ do ngx.print(body) if has_timing then - req_dyn_hook_run_hooks("timing", "after:response") + req_dyn_hook_run_hook("timing", "after:response") end -- jump over the balancer to header_filter @@ -1562,7 +1562,7 @@ function Kong.header_filter() local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:header_filter") + req_dyn_hook_run_hook("timing", "before:header_filter") end if not ctx.KONG_PROCESSING_START then @@ -1634,7 +1634,7 @@ function Kong.header_filter() ctx.KONG_HEADER_FILTER_TIME = ctx.KONG_HEADER_FILTER_ENDED_AT - ctx.KONG_HEADER_FILTER_START if has_timing then - req_dyn_hook_run_hooks("timing", "after:header_filter") + req_dyn_hook_run_hook("timing", "after:header_filter") end end @@ -1644,7 +1644,7 @@ function Kong.body_filter() local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:body_filter") + req_dyn_hook_run_hook("timing", "before:body_filter") end if not ctx.KONG_BODY_FILTER_START then @@ -1703,7 +1703,7 @@ function Kong.body_filter() if not arg[2] then if has_timing then - req_dyn_hook_run_hooks("timing", "after:body_filter") + req_dyn_hook_run_hook("timing", "after:body_filter") end return @@ -1725,7 +1725,7 @@ function Kong.body_filter() end if has_timing then - req_dyn_hook_run_hooks("timing", "after:body_filter") + req_dyn_hook_run_hook("timing", "after:body_filter") end end @@ -1735,7 +1735,7 @@ function Kong.log() local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:log") + req_dyn_hook_run_hook("timing", "before:log") end if not ctx.KONG_LOG_START then @@ -1830,7 +1830,7 @@ function Kong.log() runloop.log.after(ctx) if has_timing then - req_dyn_hook_run_hooks("timing", "after:log") + req_dyn_hook_run_hook("timing", "after:log") end release_table(CTX_NS, ctx) diff --git a/kong/resty/dns/client.lua b/kong/resty/dns/client.lua index f7e23049ab68..874515badeb1 100644 --- a/kong/resty/dns/client.lua +++ b/kong/resty/dns/client.lua @@ -49,7 +49,7 @@ local table_concat = table.concat local string_lower = string.lower local string_byte = string.byte -local req_dyn_hook_run_hooks = req_dyn_hook.run_hooks +local req_dyn_hook_run_hook = req_dyn_hook.run_hook local DOT = string_byte(".") @@ -145,7 +145,7 @@ local cachelookup = function(qname, qtype) local ctx = ngx.ctx if ctx and ctx.has_timing then - req_dyn_hook_run_hooks("timing", "dns:cache_lookup", cached ~= nil) + req_dyn_hook_run_hook("timing", "dns:cache_lookup", cached ~= nil) end if cached then diff --git a/kong/runloop/handler.lua b/kong/runloop/handler.lua index 84fb8493c89b..b743e3241717 100644 --- a/kong/runloop/handler.lua +++ b/kong/runloop/handler.lua @@ -51,7 +51,7 @@ local request_id_get = request_id.get local escape = require("kong.tools.uri").escape local encode = require("string.buffer").encode -local req_dyn_hook_run_hooks = req_dyn_hook.run_hooks +local req_dyn_hook_run_hook = req_dyn_hook.run_hook local is_http_module = subsystem == "http" local is_stream_module = subsystem == "stream" @@ -1112,7 +1112,7 @@ return { local has_timing = ctx.has_timing if has_timing then - req_dyn_hook_run_hooks("timing", "before:router") + req_dyn_hook_run_hook("timing", "before:router") end -- routing request @@ -1120,7 +1120,7 @@ return { local match_t = router:exec(ctx) if has_timing then - req_dyn_hook_run_hooks("timing", "after:router") + req_dyn_hook_run_hook("timing", "after:router") end if not match_t then @@ -1141,7 +1141,7 @@ return { ctx.workspace = match_t.route and match_t.route.ws_id if has_timing then - req_dyn_hook_run_hooks("timing", "workspace_id:got", ctx.workspace) + req_dyn_hook_run_hook("timing", "workspace_id:got", ctx.workspace) end local host = var.host From 3d83e4713194db9576586ae3391b14b756ec9b0b Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 27 May 2024 15:58:46 +0800 Subject: [PATCH 9/9] style(dynamic_hook): rename the third argument of function `hook_function` from `child_key` to `function_key` for readability Co-authored-by: samugi --- kong/dynamic_hook/init.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index 76826226eb76..1e1ec18ffb19 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -153,10 +153,10 @@ local function wrap_function(max_args, group_name, original_func, handlers) end -function _M.hook_function(group_name, parent, child_key, max_args, handlers) +function _M.hook_function(group_name, parent, function_key, max_args, handlers) assert(type(group_name) == "string", "group_name must be a string") assert(type(parent) == "table", "parent must be a table") - assert(type(child_key) == "string", "child_key must be a string") + assert(type(function_key) == "string", "function_key must be a string") local is_varargs = max_args == "varargs" if not is_varargs then @@ -164,13 +164,13 @@ function _M.hook_function(group_name, parent, child_key, max_args, handlers) assert(max_args >= 0 and max_args <= 8, 'max_args must be >= 0 and <= 8, or "varargs"') end - local original_func = parent[child_key] - assert(type(original_func) == "function", "parent[" .. child_key .. "] must be a function") + local original_func = parent[function_key] + assert(type(original_func) == "function", "parent[" .. function_key .. "] must be a function") if is_varargs then - parent[child_key] = wrap_function_vararg(group_name, original_func, handlers) + parent[function_key] = wrap_function_vararg(group_name, original_func, handlers) else - parent[child_key] = wrap_function(max_args, group_name, original_func, handlers) + parent[function_key] = wrap_function(max_args, group_name, original_func, handlers) end end