Skip to content

Commit

Permalink
Added lrucache to cache expression result
Browse files Browse the repository at this point in the history
  • Loading branch information
sreeram-venkitesh committed Nov 1, 2024
1 parent 45679ae commit f18e753
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions apisix/plugins/chaitin-waf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ local ipairs = ipairs

local plugin_name = "chaitin-waf"

local lrucache = core.lrucache.new({
ttl = 300, count = 1024
})

local vars_schema = {
type = "array",
}
Expand Down Expand Up @@ -223,18 +227,28 @@ local function get_chaitin_server(metadata, ctx)
end


local function fetch_expression_result(match)
local exp, err = expr.new(match.vars)
if err then
local msg = "failed to create match expression for " ..
tostring(match.vars) .. ", err: " .. tostring(err)
core.log.error(msg)
return false, msg
end

return exp, err
end


local function check_match(conf, ctx)
local match_passed = true

if conf.match then
for _, match in ipairs(conf.match) do
-- todo: use lrucache to cache the result
local exp, err = expr.new(match.vars)
if err then
local msg = "failed to create match expression for " ..
tostring(match.vars) .. ", err: " .. tostring(err)
core.log.error(msg)
return false, msg
local exp, err = lrucache(match, nil, fetch_expression_result, match)
if not exp then
return false, err
end

match_passed = exp:eval(ctx.var)
Expand Down

0 comments on commit f18e753

Please sign in to comment.