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

optimize: used lua table to cache the ngx variable. #31

Merged
merged 1 commit into from
May 28, 2019
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
9 changes: 5 additions & 4 deletions lua/apisix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions lua/apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
40 changes: 40 additions & 0 deletions lua/apisix/core/ctx.lua
Original file line number Diff line number Diff line change
@@ -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
38 changes: 5 additions & 33 deletions lua/apisix/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 10 additions & 12 deletions lua/apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/limit-count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/limit-req.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
Expand Down
4 changes: 2 additions & 2 deletions lua/apisix/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down