Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: allow skip body filter #8149

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions apisix/plugins/ai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ local apisix = require("apisix")
local core = require("apisix.core")
local router = require("apisix.router")
local event = require("apisix.core.event")
local load_balancer = require("apisix.balancer")
local balancer = require("ngx.balancer")
local ngx = ngx
local is_http = ngx.config.subsystem == "http"
local enable_keepalive = balancer.enable_keepalive and is_http
local is_apisix_or, response = pcall(require, "resty.apisix.response")
Copy link
Member

@membphis membphis Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a core API to get the version of APISIX runtime

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you thing? @spacewander

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean move pcall(require, "resty.apisix.response") on apisix core level?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible but not valuable to do this, as we don't want to support multiple versions of APISIX-Base, only support the latest APISIX-Base. Maybe a simple API to detect if apisix-base / openresty is used is enough.

local ipairs = ipairs
local pcall = pcall
local loadstring = loadstring
Expand Down Expand Up @@ -65,7 +66,7 @@ local _M = {

local orig_router_match
local orig_handle_upstream = apisix.handle_upstream
local orig_balancer_run = load_balancer.run
local orig_http_balancer_phase = apisix.http_balancer_phase

local default_keepalive_pool = {}

Expand Down Expand Up @@ -116,7 +117,21 @@ end


local pool_opt
local function ai_balancer_run(route)
local function ai_http_balancer_phase()
local api_ctx = ngx.ctx.api_ctx
if not api_ctx then
core.log.error("invalid api_ctx")
return core.response.exit(500)
end

if is_apisix_or then
local ok, err = response.skip_body_filter_by_lua()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No skip_header_filter_by_lua?

Copy link
Member Author

@tzssangglass tzssangglass Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cancel skip_header_filter_by_lua, because we add APISIX/2.99in the header_filter phase, and enable_server_tokens = false cannot be used as a flag to skip header_filter_by_lua because it only removes the version in server token, not delete the server token unless we add another switch for removing the server token completely.

if not ok then
core.log.error("failed to skip body filter by lua: ", err)
end
end

local route = api_ctx.matched_route
local server = route.value.upstream.nodes[1]
if enable_keepalive then
local ok, err = balancer.set_current_peer(server.host, server.port or 80, pool_opt)
Expand All @@ -132,6 +147,7 @@ local function ai_balancer_run(route)
end
end


local function routes_analyze(routes)
local route_flags = core.table.new(0, 16)
local route_up_flags = core.table.new(0, 12)
Expand Down Expand Up @@ -161,6 +177,8 @@ local function routes_analyze(routes)
route_flags["service_id"] = true
elseif key == "plugin_config_id" then
route_flags["plugin_config_id"] = true
elseif key == "script" then
route_flags["script"] = true
end

-- collect upstream flags
Expand Down Expand Up @@ -198,10 +216,14 @@ local function routes_analyze(routes)
end
end

local global_rules_flag = router.global_rules and router.global_rules.values
and #router.global_rules.values ~= 0

if route_flags["vars"] or route_flags["filter_fun"]
or route_flags["remote_addr"]
or route_flags["service_id"]
or route_flags["plugin_config_id"] then
or route_flags["plugin_config_id"]
or global_rules_flag then
router.router_http.match = orig_router_match
else
core.log.info("use ai plane to match route")
Expand All @@ -215,10 +237,12 @@ local function routes_analyze(routes)
end

if route_flags["service"]
or route_flags["script"]
or route_flags["service_id"]
or route_flags["upstream_id"]
or route_flags["enable_websocket"]
or route_flags["plugins"]
or route_flags["plugin_config_id"]
or route_up_flags["has_domain"]
or route_up_flags["pass_host"]
or route_up_flags["scheme"]
Expand All @@ -228,13 +252,14 @@ local function routes_analyze(routes)
or route_up_flags["tls"]
or route_up_flags["keepalive"]
or route_up_flags["service_name"]
or route_up_flags["more_nodes"] then
or route_up_flags["more_nodes"]
or global_rules_flag then
apisix.handle_upstream = orig_handle_upstream
load_balancer.run = orig_balancer_run
apisix.http_balancer_phase = orig_http_balancer_phase
else
-- replace the upstream module
-- replace the upstream and balancer module
apisix.handle_upstream = ai_upstream
load_balancer.run = ai_balancer_run
apisix.http_balancer_phase = ai_http_balancer_phase
end
end

Expand Down
10 changes: 10 additions & 0 deletions t/debug/dynamic-hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ run_tests();
__DATA__

=== TEST 1: dynamic enable
# ai module would conflict with the debug module
--- extra_yaml_config
plugins:
#- ai
- example-plugin
--- debug_config eval: $::debug_config
--- config
location /t {
Expand Down Expand Up @@ -86,6 +91,11 @@ call require("apisix").http_access_phase() args:{}


=== TEST 2: dynamic enable by per request and disable after handle request
# ai module would conflict with the debug module
--- extra_yaml_config
plugins:
#- ai
- example-plugin
--- debug_config eval: $::debug_config
--- config
location /t {
Expand Down
Loading