optimize: reused temporary Lua table by tablepool.

This commit is contained in:
Yuansheng Wang 2019-05-29 10:29:40 +08:00
parent 9c5bb8759a
commit 46a5e7ac19
3 changed files with 41 additions and 16 deletions

View File

@ -4,8 +4,7 @@ local require = require
local core = require("apisix.core")
local router = require("apisix.route").get
local plugin = require("apisix.plugin")
local new_tab = require("table.new")
local load_balancer = require("apisix.balancer") .run
local load_balancer = require("apisix.balancer").run
local service_fetch = require("apisix.service").get
local ngx = ngx
@ -38,18 +37,30 @@ local function run_plugin(phase, filter_plugins, api_ctx)
filter_plugins = filter_plugins or api_ctx.filter_plugins
if not filter_plugins then
return
return api_ctx
end
if phase ~= "log" then
for i = 1, #filter_plugins, 2 do
local phase_fun = filter_plugins[i][phase]
if phase_fun then
local code, body = phase_fun(filter_plugins[i + 1], api_ctx)
if code or body then
core.response.exit(code, body)
end
end
end
return api_ctx
end
for i = 1, #filter_plugins, 2 do
local phase_fun = filter_plugins[i][phase]
if phase_fun then
local code, body = phase_fun(filter_plugins[i + 1], api_ctx)
if phase ~= "log" and type(code) == "number" or body then
core.response.exit(code, body)
end
phase_fun(filter_plugins[i + 1], api_ctx)
end
end
return api_ctx
end
@ -58,8 +69,7 @@ function _M.rewrite_phase()
local api_ctx = ngx_ctx.api_ctx
if api_ctx == nil then
-- todo: reuse this table
api_ctx = new_tab(0, 32)
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
end
core.ctx.set_vars_meta(api_ctx)
@ -127,7 +137,11 @@ function _M.header_filter_phase()
end
function _M.log_phase()
run_plugin("log")
local api_ctx = run_plugin("log")
if api_ctx then
core.ctx.release_vars(api_ctx)
core.tablepool.release("api_ctx", api_ctx)
end
end
function _M.balancer_phase()

View File

@ -34,10 +34,10 @@ end
return {
version = 0.1,
log = require("apisix.core.log"),
config = require("apisix.core.config_etcd"),
json = {
version = 0.1,
log = require("apisix.core.log"),
config = require("apisix.core.config_etcd"),
json = {
encode = function(data, force)
if force then
data = tab_clone_with_serialise(data)
@ -47,11 +47,12 @@ return {
end,
decode = require("cjson.safe").decode,
},
table = require("apisix.core.table"),
table = require("apisix.core.table"),
request = require("apisix.core.request"),
response = require("apisix.core.response"),
typeof = require("apisix.core.typeof"),
lrucache = require("apisix.core.lrucache"),
schema = require("apisix.core.schema"),
ctx = require("apisix.core.ctx"),
tablepool= require("tablepool"),
}

View File

@ -1,3 +1,4 @@
local tablepool = require "tablepool"
local new_tab = require("table.new")
local ngx_var = ngx.var
local setmetatable = setmetatable
@ -31,10 +32,19 @@ do
}
function _M.set_vars_meta(ctx)
ctx.var = new_tab(0, 32)
ctx.var = tablepool.fetch("ctx_var", 0, 32)
setmetatable(ctx.var, mt)
end
function _M.release_vars(ctx)
if ctx.var == nil then
return
end
tablepool.release("ctx_var", ctx.var)
ctx.var = nil
end
end -- do