From e1f4f9b1a31361315ffff5ec78fa016f35053ca4 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Thu, 2 Apr 2020 20:52:50 +0200 Subject: [PATCH 1/3] Refactor the way to run global rule in log/header filter/body filter phases. --- apisix/init.lua | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 272911a4bb59..f2f91522a933 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -436,54 +436,31 @@ function _M.grpc_access_phase() run_plugin("access", plugins, api_ctx) end - function _M.http_header_filter_phase() local api_ctx = ngx.ctx.api_ctx if not api_ctx then return end - - if router.global_rules and router.global_rules.values - and #router.global_rules.values > 0 - then - local plugins = core.tablepool.fetch("plugins", 32, 0) - for _, global_rule in ipairs(router.global_rules.values) do - core.table.clear(plugins) - plugins = plugin.filter(global_rule, plugins) - run_plugin("header_filter", plugins, api_ctx) - end - core.tablepool.release("plugins", plugins) - end - run_plugin("header_filter", nil, api_ctx) + common_phase("header_filter", api_ctx) end - function _M.http_body_filter_phase() local api_ctx = ngx.ctx.api_ctx if not api_ctx then return end - - if router.global_rules and router.global_rules.values - and #router.global_rules.values > 0 - then - local plugins = core.tablepool.fetch("plugins", 32, 0) - for _, global_rule in ipairs(router.global_rules.values) do - core.table.clear(plugins) - plugins = plugin.filter(global_rule, plugins) - run_plugin("body_filter", plugins, api_ctx) - end - core.tablepool.release("plugins", plugins) - end - run_plugin("body_filter", nil, api_ctx) + common_phase("body_filter", api_ctx) end - function _M.http_log_phase() local api_ctx = ngx.ctx.api_ctx if not api_ctx then return end + common_phase("log", api_ctx) +end + +local function common_phase(plugin_name,api_ctx) if router.global_rules and router.global_rules.values and #router.global_rules.values > 0 @@ -492,12 +469,11 @@ function _M.http_log_phase() for _, global_rule in ipairs(router.global_rules.values) do core.table.clear(plugins) plugins = plugin.filter(global_rule, plugins) - run_plugin("log", plugins, api_ctx) + run_plugin(plugin_name, plugins, api_ctx) end core.tablepool.release("plugins", plugins) end - - run_plugin("log", nil, api_ctx) + run_plugin(plugin_name, nil, api_ctx) if api_ctx.uri_parse_param then core.tablepool.release("uri_parse_param", api_ctx.uri_parse_param) @@ -511,7 +487,6 @@ function _M.http_log_phase() core.tablepool.release("api_ctx", api_ctx) end - function _M.http_balancer_phase() local api_ctx = ngx.ctx.api_ctx if not api_ctx then From 4046586a4dec45261f66956661be89519c09e1a6 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Thu, 2 Apr 2020 20:55:41 +0200 Subject: [PATCH 2/3] fix. --- apisix/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/init.lua b/apisix/init.lua index f2f91522a933..0331e42254f3 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -461,7 +461,6 @@ function _M.http_log_phase() end local function common_phase(plugin_name,api_ctx) - if router.global_rules and router.global_rules.values and #router.global_rules.values > 0 then From f2d49855965df034ee34abaa5e52427bb769493f Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Thu, 2 Apr 2020 21:12:39 +0200 Subject: [PATCH 3/3] fix. --- apisix/init.lua | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 0331e42254f3..d9590bb77ace 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -436,30 +436,6 @@ function _M.grpc_access_phase() run_plugin("access", plugins, api_ctx) end -function _M.http_header_filter_phase() - local api_ctx = ngx.ctx.api_ctx - if not api_ctx then - return - end - common_phase("header_filter", api_ctx) -end - -function _M.http_body_filter_phase() - local api_ctx = ngx.ctx.api_ctx - if not api_ctx then - return - end - common_phase("body_filter", api_ctx) -end - -function _M.http_log_phase() - local api_ctx = ngx.ctx.api_ctx - if not api_ctx then - return - end - common_phase("log", api_ctx) -end - local function common_phase(plugin_name,api_ctx) if router.global_rules and router.global_rules.values and #router.global_rules.values > 0 @@ -486,6 +462,30 @@ local function common_phase(plugin_name,api_ctx) core.tablepool.release("api_ctx", api_ctx) end +function _M.http_header_filter_phase() + local api_ctx = ngx.ctx.api_ctx + if not api_ctx then + return + end + common_phase("header_filter", api_ctx) +end + +function _M.http_body_filter_phase() + local api_ctx = ngx.ctx.api_ctx + if not api_ctx then + return + end + common_phase("body_filter", api_ctx) +end + +function _M.http_log_phase() + local api_ctx = ngx.ctx.api_ctx + if not api_ctx then + return + end + common_phase("log", api_ctx) +end + function _M.http_balancer_phase() local api_ctx = ngx.ctx.api_ctx if not api_ctx then