perf: run the balancer phase in an earlier phase first to avoid alway… (#4629)

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
This commit is contained in:
罗泽轩 2021-07-21 06:35:47 +08:00 committed by GitHub
parent a015dc378f
commit 4c4033fdba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 24 deletions

View File

@ -288,7 +288,6 @@ end
function _M.run(route, ctx, plugin_funcs) function _M.run(route, ctx, plugin_funcs)
local server, err local server, err
local header_changed
if ctx.picked_server then if ctx.picked_server then
-- use the server picked in the access phase -- use the server picked in the access phase
@ -311,6 +310,7 @@ function _M.run(route, ctx, plugin_funcs)
return core.response.exit(502) return core.response.exit(502)
end end
local header_changed
local pass_host = ctx.pass_host local pass_host = ctx.pass_host
if pass_host == "node" and balancer.recreate_request then if pass_host == "node" and balancer.recreate_request then
local host = server.domain or server.host local host = server.domain or server.host
@ -321,13 +321,12 @@ function _M.run(route, ctx, plugin_funcs)
end end
end end
end
local _, run = plugin_funcs("balancer") local _, run = plugin_funcs("balancer")
-- always recreate request as the request may be changed by plugins -- always recreate request as the request may be changed by plugins
if (run or header_changed) and balancer.recreate_request then if (run or header_changed) and balancer.recreate_request then
balancer.recreate_request() balancer.recreate_request()
end end
end
core.log.info("proxy request to ", server.host, ":", server.port) core.log.info("proxy request to ", server.host, ":", server.port)

View File

@ -321,6 +321,23 @@ local function verify_tls_client(ctx)
end end
local function common_phase(phase_name)
local api_ctx = ngx.ctx.api_ctx
if not api_ctx then
return
end
plugin.run_global_rules(api_ctx, api_ctx.global_rules, phase_name)
if api_ctx.script_obj then
script.run(phase_name, api_ctx)
return api_ctx, true
end
return plugin.run_plugin(phase_name, nil, api_ctx)
end
function _M.http_access_phase() function _M.http_access_phase()
local ngx_ctx = ngx.ctx local ngx_ctx = ngx.ctx
@ -501,6 +518,9 @@ function _M.http_access_phase()
set_upstream_headers(api_ctx, server) set_upstream_headers(api_ctx, server)
-- run the balancer phase in access phase first to avoid always reinit request
common_phase("balancer")
local ref = ctxdump.stash_ngx_ctx() local ref = ctxdump.stash_ngx_ctx()
core.log.info("stash ngx ctx: ", ref) core.log.info("stash ngx ctx: ", ref)
ngx_var.ctx_ref = ref ngx_var.ctx_ref = ref
@ -546,23 +566,6 @@ function _M.grpc_access_phase()
end end
local function common_phase(phase_name)
local api_ctx = ngx.ctx.api_ctx
if not api_ctx then
return
end
plugin.run_global_rules(api_ctx, api_ctx.global_rules, phase_name)
if api_ctx.script_obj then
script.run(phase_name, api_ctx)
return api_ctx, true
end
return plugin.run_plugin(phase_name, nil, api_ctx)
end
local function set_resp_upstream_status(up_status) local function set_resp_upstream_status(up_status)
core.response.set_header("X-APISIX-Upstream-Status", up_status) core.response.set_header("X-APISIX-Upstream-Status", up_status)
core.log.info("X-APISIX-Upstream-Status: ", up_status) core.log.info("X-APISIX-Upstream-Status: ", up_status)
@ -909,6 +912,9 @@ function _M.stream_preread_phase()
end end
api_ctx.picked_server = server api_ctx.picked_server = server
-- run the balancer phase in preread phase first to avoid always reinit request
common_phase("balancer")
end end