-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
local ipairs = ipairs | ||
local pcall = pcall | ||
local loadstring = loadstring | ||
|
@@ -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 = {} | ||
|
||
|
@@ -116,7 +117,26 @@ 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_header_filter_by_lua() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only the current request skips the ***_filter, all right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, it effects global |
||
if not ok then | ||
core.log.error("failed to skip header filter by lua: ", err) | ||
end | ||
|
||
ok, err = response.skip_body_filter_by_lua() | ||
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) | ||
|
@@ -132,6 +152,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) | ||
|
@@ -219,6 +240,7 @@ local function routes_analyze(routes) | |
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"] | ||
|
@@ -230,11 +252,11 @@ local function routes_analyze(routes) | |
or route_up_flags["service_name"] | ||
or route_up_flags["more_nodes"] 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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
use t::APISIX 'no_plan'; | ||
|
||
repeat_each(1); | ||
log_level('info'); | ||
worker_connections(256); | ||
no_root_location(); | ||
no_shuffle(); | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if ((!defined $block->error_log) && (!defined $block->no_error_log)) { | ||
$block->set_value("no_error_log", "[error]"); | ||
} | ||
|
||
if (!defined $block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
|
||
}); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: enable skip header and body filter | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"methods": ["GET"], | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
local http = require "resty.http" | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
local httpc = http.new() | ||
local res, err = httpc:request_uri(uri) | ||
assert(res.status == 200) | ||
if not res then | ||
ngx.log(ngx.ERR, err) | ||
return | ||
end | ||
local headers = res.headers | ||
ngx.say(headers["Server"]) | ||
} | ||
} | ||
--- response_body eval | ||
qr/openresty\/\d+/ | ||
|
||
|
||
|
||
=== TEST 2: route with plugin_config_id, disable skip header and body filter | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/plugin_configs/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"serverless-pre-function": { | ||
"phase": "before_proxy", | ||
"functions" : ["return function(conf, ctx) ngx.log(ngx.WARN, \"run before_proxy phase balancer_ip : \", ctx.balancer_ip) end"] | ||
} | ||
} | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugin_config_id": "1", | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local http = require "resty.http" | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
local httpc = http.new() | ||
local res, err = httpc:request_uri(uri) | ||
assert(res.status == 200) | ||
if not res then | ||
ngx.log(ngx.ERR, err) | ||
return | ||
end | ||
local headers = res.headers | ||
ngx.say(headers["Server"]) | ||
} | ||
} | ||
--- response_body eval | ||
qr/APISIX\/\d+/ | ||
--- error_log | ||
run before_proxy phase balancer_ip : 127.0.0.1 | ||
--- no_error_log | ||
enable sample upstream | ||
|
||
|
||
|
||
=== TEST 3: route with plugins, disable skip header and body filter | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/consumers', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"username": "new_consumer", | ||
"plugins": { | ||
"key-auth": { | ||
"key": "auth-jack" | ||
}, | ||
"serverless-pre-function": { | ||
"phase": "before_proxy", | ||
"functions" : ["return function(conf, ctx) ngx.log(ngx.WARN, \"run before_proxy phase balancer_ip : \", ctx.balancer_ip) end"] | ||
} | ||
} | ||
}]] | ||
) | ||
ngx.sleep(0.5) | ||
|
||
local code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"key-auth": {} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local http = require "resty.http" | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
local httpc = http.new() | ||
local headers = { | ||
["apikey"] = "auth-jack" | ||
} | ||
local res, err = httpc:request_uri(uri, {headers = headers}) | ||
assert(res.status == 200) | ||
if not res then | ||
ngx.log(ngx.ERR, err) | ||
return | ||
end | ||
local headers = res.headers | ||
ngx.say(headers["Server"]) | ||
} | ||
} | ||
--- response_body eval | ||
qr/APISIX\/\d+/ | ||
--- error_log | ||
run before_proxy phase balancer_ip : 127.0.0.1 | ||
--- no_error_log | ||
enable sample upstream | ||
|
||
|
||
|
||
=== TEST 4: one of route has plugins, disable skip header and body filter | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"serverless-pre-function": { | ||
"phase": "before_proxy", | ||
"functions" : ["return function(conf, ctx) ngx.log(ngx.WARN, \"run before_proxy phase balancer_ip : \", ctx.balancer_ip) end"] | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local code, body = t('/apisix/admin/routes/2', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello1" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
ngx.say(body) | ||
return | ||
end | ||
ngx.sleep(0.5) | ||
|
||
local http = require "resty.http" | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1" | ||
local httpc = http.new() | ||
local headers = { | ||
["apikey"] = "auth-jack" | ||
} | ||
local res, err = httpc:request_uri(uri, {headers = headers}) | ||
assert(res.status == 200) | ||
if not res then | ||
ngx.log(ngx.ERR, err) | ||
return | ||
end | ||
local headers = res.headers | ||
ngx.say(headers["Server"]) | ||
} | ||
} | ||
--- response_body eval | ||
qr/APISIX\/\d+/ | ||
--- no_error_log | ||
enable sample upstream |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.