From b393d4927232e8fb17f2059a77ff39a8ccb777b6 Mon Sep 17 00:00:00 2001 From: Yuansheng Wang Date: Tue, 28 May 2019 10:47:53 +0800 Subject: [PATCH] optimize: used lua table to cache the ngx variable. --- lua/apisix.lua | 9 ++--- lua/apisix/core.lua | 1 + lua/apisix/core/ctx.lua | 40 ++++++++++++++++++++++ lua/apisix/core/request.lua | 38 +++----------------- lua/apisix/plugin.lua | 22 ++++++------ lua/apisix/plugins/limit-count.lua | 2 +- lua/apisix/plugins/limit-req.lua | 2 +- lua/apisix/plugins/prometheus/exporter.lua | 4 +-- 8 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 lua/apisix/core/ctx.lua diff --git a/lua/apisix.lua b/lua/apisix.lua index 716219d1d4e2..f0b5ba05a6b9 100644 --- a/lua/apisix.lua +++ b/lua/apisix.lua @@ -43,7 +43,7 @@ local function run_plugin(phase, filter_plugins, api_ctx) for i = 1, #filter_plugins, 2 do local phase_fun = filter_plugins[i][phase] - if phase_fun then + 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) @@ -62,9 +62,10 @@ function _M.rewrite_phase() api_ctx = new_tab(0, 32) end - local method = core.request.var(api_ctx, "method") - local uri = core.request.var(api_ctx, "uri") - -- local host = core.request.var(api_ctx, "host") -- todo: support host + core.ctx.set_vars_meta(api_ctx) + local method = api_ctx.var["method"] + local uri = api_ctx.var["uri"] + -- local host = api_ctx.var["host"] -- todo: support host -- run the api router local api_router = plugin.api_router() diff --git a/lua/apisix/core.lua b/lua/apisix/core.lua index 3daa2bedea89..1ac01b77ec88 100644 --- a/lua/apisix/core.lua +++ b/lua/apisix/core.lua @@ -57,4 +57,5 @@ return { typeof = require("apisix.core.typeof"), lrucache = require("apisix.core.lrucache"), schema = require("apisix.core.schema"), + ctx = require("apisix.core.ctx"), } diff --git a/lua/apisix/core/ctx.lua b/lua/apisix/core/ctx.lua new file mode 100644 index 000000000000..42d352de35a7 --- /dev/null +++ b/lua/apisix/core/ctx.lua @@ -0,0 +1,40 @@ +local new_tab = require("table.new") +local ngx_var = ngx.var + + +local _M = {version = 0.1} + + +do + local var_methods = { + ["method"] = ngx.req.get_method + } + + local mt = { + __index = function(t, name) + local val + local method = var_methods[name] + if method then + val = method() + + else + val = ngx_var[name] + end + + if val then + t[name] = val + end + + return val + end + } + +function _M.set_vars_meta(ctx) + ctx.var = new_tab(0, 32) + setmetatable(ctx.var, mt) +end + +end -- do + + +return _M diff --git a/lua/apisix/core/request.lua b/lua/apisix/core/request.lua index 6468ae0b0c27..f687130e85a8 100644 --- a/lua/apisix/core/request.lua +++ b/lua/apisix/core/request.lua @@ -2,53 +2,25 @@ local ngx = ngx local get_headers = ngx.req.get_headers -local ngx_var = ngx.var -local new_tab = require("table.new") -local var_methods = { - -- todo: support more type - method = ngx.req.get_method -} local _M = {version = 0.1} -function _M.header(ctx, name) +local function _headers(ctx) local headers = ctx.headers if not headers then headers = get_headers() ctx.headers = headers end - return ctx.headers[name] + return headers end +_M.headers = _headers -function _M.var(ctx, name) - local vars = ctx.vars - if not vars then - vars = new_tab(0, 8) - ctx.vars = vars - end - - local val = vars[name] - if val then - return val - end - - -- todo: support more data type - local method = var_methods[name] - if method then - val = method() - else - val = ngx_var[name] - end - - if val then - vars[name] = val - end - - return val +function _M.header(ctx, name) + return _headers(ctx)[name] end diff --git a/lua/apisix/plugin.lua b/lua/apisix/plugin.lua index a57e7e246a8d..a13baeb42bad 100644 --- a/lua/apisix/plugin.lua +++ b/lua/apisix/plugin.lua @@ -120,25 +120,23 @@ end function _M.merge_service_route(service_conf, route_conf) - -- core.log.warn("base conf: ", core.json.encode(base_conf)) - -- core.log.warn("new conf: ", core.json.encode(new_conf)) - local new_conf = false - if service_conf.plugin_config and - core.table.nkeys(service_conf.plugin_config) then - for name, conf in pairs(service_conf.plugin_config) do - route_conf.plugin_config[name] = conf + local changed = false + if route_conf.value.plugin_config and + core.table.nkeys(route_conf.value.plugin_config) then + for name, conf in pairs(route_conf.value.plugin_config) do + service_conf.value.plugin_config[name] = conf end - new_conf = true + changed = true end - if service_conf.upstream and core.table.nkeys(service_conf.upstream) then - route_conf.upstream = service_conf.upstream - new_conf = true + if route_conf.upstream and core.table.nkeys(route_conf.upstream) then + service_conf.upstream = route_conf.upstream + changed = true end route_conf.service = service_conf.value - return service_conf, new_conf + return service_conf, changed end diff --git a/lua/apisix/plugins/limit-count.lua b/lua/apisix/plugins/limit-count.lua index d50c30dcede7..ef219555e5e0 100644 --- a/lua/apisix/plugins/limit-count.lua +++ b/lua/apisix/plugins/limit-count.lua @@ -27,7 +27,7 @@ function _M.access(conf, ctx) local limit = core.lrucache.plugin_ctx(plugin_name, ctx, create_limit_obj, conf) - local key = core.request.var(ctx, conf.key) + local key = ctx.var[conf.key] if not key or key == "" then key = "" core.log.warn("fetched empty string value as key to limit the request ", diff --git a/lua/apisix/plugins/limit-req.lua b/lua/apisix/plugins/limit-req.lua index a96de060bf43..7369197303fb 100644 --- a/lua/apisix/plugins/limit-req.lua +++ b/lua/apisix/plugins/limit-req.lua @@ -25,7 +25,7 @@ function _M.access(conf, ctx) local limit_ins = core.lrucache.plugin_ctx(plugin_name, ctx, create_limit_obj, conf) - local key = core.request.var(ctx, conf.key) + local key = ctx.var[conf.key] if not key or key == "" then key = "" core.log.warn("fetched empty string value as key to limit the request ", diff --git a/lua/apisix/plugins/prometheus/exporter.lua b/lua/apisix/plugins/prometheus/exporter.lua index f837cb71f2ed..ccf8c3a0a6f6 100644 --- a/lua/apisix/plugins/prometheus/exporter.lua +++ b/lua/apisix/plugins/prometheus/exporter.lua @@ -23,8 +23,8 @@ do function _M.log(conf, ctx) core.table.clear(tmp_tab) - tmp_tab[1] = ngx.status - tmp_tab[2] = ngx.var.host + tmp_tab[1] = ctx.var.status + tmp_tab[2] = ctx.var.host metrics.status:inc(1, tmp_tab) core.log.warn("hit prometheuse plugin")