mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-04 21:17:36 +08:00
feature: supported to use lrucache for plugin developer.
This commit is contained in:
parent
20e240bb96
commit
4570ba59c0
@ -51,6 +51,6 @@ return {
|
||||
},
|
||||
resp = require("apisix.core.resp"),
|
||||
typeof = require("apisix.core.typeof"),
|
||||
global_lru = require("apisix.core.global_lrucache"),
|
||||
lrucache = require("apisix.core.lrucache"),
|
||||
ctx = require("apisix.core.ctx"),
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
-- Copyright (C) Yuansheng Wang
|
||||
|
||||
local ngx_req = ngx.req
|
||||
local ngx_var = ngx.var
|
||||
local new_tab = require("table.new")
|
||||
|
||||
@ -8,6 +7,12 @@ local new_tab = require("table.new")
|
||||
local _M = {version = 0.1}
|
||||
|
||||
|
||||
local var_methods = {
|
||||
-- todo: support more type
|
||||
method = ngx.req.get_method
|
||||
}
|
||||
|
||||
|
||||
function _M.get(api_ctx, name)
|
||||
local vars = api_ctx.vars
|
||||
if not vars then
|
||||
@ -20,8 +25,10 @@ function _M.get(api_ctx, name)
|
||||
return val
|
||||
end
|
||||
|
||||
if name == "method" then
|
||||
val = ngx_req.get_method()
|
||||
-- todo: added more data type
|
||||
local method = var_methods[name]
|
||||
if method then
|
||||
val = method()
|
||||
else
|
||||
val = ngx_var[name]
|
||||
end
|
||||
|
@ -1,33 +0,0 @@
|
||||
-- Copyright (C) Yuansheng Wang
|
||||
|
||||
local lru_new = require("resty.lrucache").new
|
||||
local lru_items = lru_new(200) -- todo: support to config in yaml.
|
||||
|
||||
|
||||
local _M = {version = 0.1}
|
||||
|
||||
|
||||
function _M.fetch(name, count, version)
|
||||
local cache = lru_items:get(name)
|
||||
if cache and cache.version == version then
|
||||
return cache
|
||||
end
|
||||
|
||||
cache = lru_new(count)
|
||||
cache.version = version
|
||||
lru_items:set(name, cache)
|
||||
return cache
|
||||
end
|
||||
|
||||
|
||||
function _M.set(self, name, lru)
|
||||
return lru_items:set(name, lru)
|
||||
end
|
||||
|
||||
|
||||
function _M.delete(name)
|
||||
return lru_items:delete(name)
|
||||
end
|
||||
|
||||
|
||||
return _M
|
44
lua/apisix/core/lrucache.lua
Normal file
44
lua/apisix/core/lrucache.lua
Normal file
@ -0,0 +1,44 @@
|
||||
local lru_new = require("resty.lrucache").new
|
||||
local global_lrus = lru_new(256) -- todo: support to config in yaml.
|
||||
|
||||
local _M = {version = 0.1}
|
||||
|
||||
|
||||
local function _global_lrus(name, count)
|
||||
local lru = global_lrus:get(name)
|
||||
if lru then
|
||||
return lru
|
||||
end
|
||||
|
||||
lru = lru_new(count)
|
||||
global_lrus:set(name, lru)
|
||||
return lru
|
||||
end
|
||||
_M.global = _global_lrus
|
||||
|
||||
|
||||
local function _plugin_key(plugin_name, key, version, callback_fun, ...)
|
||||
local lru_plugin_conf = _global_lrus("/plugin/" .. plugin_name, 32)
|
||||
|
||||
-- todo: support `stale` object
|
||||
local obj = lru_plugin_conf:get(key)
|
||||
if not obj or obj.version ~= version then
|
||||
obj = callback_fun(...)
|
||||
obj.version = version
|
||||
lru_plugin_conf:set(key, obj)
|
||||
end
|
||||
|
||||
return obj
|
||||
end
|
||||
_M.plugin = _plugin_key
|
||||
|
||||
|
||||
function _M.plugin_ctx(plugin_name, api_ctx, callback_fun, ...)
|
||||
local key = api_ctx.conf_type .. "#" .. api_ctx.conf_id
|
||||
|
||||
return _plugin_key(plugin_name, key, api_ctx.conf_version,
|
||||
callback_fun, ...)
|
||||
end
|
||||
|
||||
|
||||
return _M
|
@ -32,14 +32,14 @@ end
|
||||
|
||||
|
||||
function _M.rewrite(conf, api_ctx)
|
||||
core.log.warn("plugin rewrite phase, conf: ", core.json.encode(conf),
|
||||
" ctx: ", core.json.encode(api_ctx, true))
|
||||
core.log.warn("plugin rewrite phase, conf: ", core.json.encode(conf))
|
||||
-- core.log.warn(" ctx: ", core.json.encode(api_ctx, true))
|
||||
end
|
||||
|
||||
|
||||
function _M.access(conf, api_ctx)
|
||||
core.log.warn("plugin access phase, conf: ", core.json.encode(conf),
|
||||
" ctx: ", core.json.encode(api_ctx, true))
|
||||
core.log.warn("plugin access phase, conf: ", core.json.encode(conf))
|
||||
-- core.log.warn(" ctx: ", core.json.encode(api_ctx, true))
|
||||
-- ngx.say("hit example plugin")
|
||||
end
|
||||
|
||||
|
@ -1,13 +1,12 @@
|
||||
local limit_req = require("resty.limit.req")
|
||||
local core = require("apisix.core")
|
||||
-- todo: support to config it in yaml
|
||||
local cache = core.global_lru.fetch("/plugin/limit-req", 200)
|
||||
local plugin_name = "limit-req"
|
||||
|
||||
|
||||
local _M = {
|
||||
version = 0.1,
|
||||
priority = 1001, -- TODO: add a type field, may be a good idea
|
||||
name = "limit-req",
|
||||
name = plugin_name,
|
||||
}
|
||||
|
||||
|
||||
@ -15,19 +14,25 @@ function _M.check_args(conf)
|
||||
return true
|
||||
end
|
||||
|
||||
local function create_limit_obj(conf)
|
||||
core.log.info("create new plugin ins")
|
||||
return limit_req.new("plugin-limit-req",
|
||||
conf.rate, conf.burst)
|
||||
end
|
||||
|
||||
|
||||
function _M.access(conf, api_ctx)
|
||||
local key = api_ctx.conf_type .. "#" .. api_ctx.conf_id
|
||||
-- core.log.warn("key: ", key, " conf: ", core.json.encode(conf))
|
||||
-- todo: support to config it in yaml
|
||||
local limit_ins = core.lrucache.plugin_ctx(plugin_name, api_ctx,
|
||||
create_limit_obj, conf)
|
||||
|
||||
local limit_ins = cache:get(key)
|
||||
if not limit_ins or limit_ins.version ~= api_ctx.conf_version then
|
||||
limit_ins = limit_req.new("plugin-limit-req", conf.rate, conf.burst)
|
||||
cache:set(key, limit_ins)
|
||||
local key = core.ctx.get(api_ctx, conf.key)
|
||||
if not key or key == "" then
|
||||
key = ""
|
||||
core.log.warn("fetched empty string value to limit the request maybe ",
|
||||
"wrong, please pay attention to this.")
|
||||
end
|
||||
|
||||
key = core.ctx.get(api_ctx, conf.key)
|
||||
|
||||
local delay, err = limit_ins:incoming(key, true)
|
||||
if not delay then
|
||||
if err == "rejected" then
|
||||
|
Loading…
Reference in New Issue
Block a user