This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new e97a05fcd feat(core): add no_cacheable option to custom variable
(#8103)
e97a05fcd is described below
commit e97a05fcdd07fdd579ea5c43d383d7472317c688
Author: 罗泽轩 <[email protected]>
AuthorDate: Tue Oct 18 13:33:05 2022 +0800
feat(core): add no_cacheable option to custom variable (#8103)
---
apisix/core/ctx.lua | 12 ++++++++++-
t/core/ctx2.t | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 1 deletion(-)
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index 9b589b6b3..b98df9186 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -318,6 +318,7 @@ do
-- @function core.ctx.register_var
-- @tparam string name custom variable name
-- @tparam function getter The fetch function for custom variables.
+-- @tparam table opts An optional options table which controls the behavior
about the variable
-- @usage
-- local core = require "apisix.core"
--
@@ -328,12 +329,21 @@ do
-- end
-- return nil
-- end)
-function _M.register_var(name, getter)
+--
+-- We support the options below in the `opts`:
+-- * no_cacheable: if the result of getter is cacheable or not. Default to
`false`.
+function _M.register_var(name, getter, opts)
if type(getter) ~= "function" then
error("the getter of registered var should be a function")
end
apisix_var_names[name] = getter
+
+ if opts then
+ if opts.no_cacheable then
+ no_cacheable_var_names[name] = true
+ end
+ end
end
function _M.set_vars_meta(ctx)
diff --git a/t/core/ctx2.t b/t/core/ctx2.t
index 47c0a4743..a99844ffd 100644
--- a/t/core/ctx2.t
+++ b/t/core/ctx2.t
@@ -372,3 +372,65 @@ Content-Type: application/x-www-form-urlencoded
}
--- response_body
find ctx.var.a6_labels_zone: Singapore
+
+
+
+=== TEST 17: register custom variable with no cacheable
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [=[{
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "serverless-pre-function": {
+ "phase": "rewrite",
+ "functions" : ["return function(conf, ctx)
ngx.say('find ctx.var.a6_count: ', ctx.var.a6_count) end"]
+ },
+ "serverless-post-function": {
+ "phase": "rewrite",
+ "functions" : ["return function(conf, ctx)
ngx.say('find ctx.var.a6_count: ', ctx.var.a6_count) end"]
+ }
+ },
+ "uri": "/hello"
+ }]=]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+
+
+
+=== TEST 18: hit
+--- config
+ location /t {
+ content_by_lua_block {
+ local http = require "resty.http"
+ local core = require "apisix.core"
+ core.ctx.register_var("a6_count", function(ctx)
+ if not ctx.a6_count then
+ ctx.a6_count = 0
+ end
+ ctx.a6_count = ctx.a6_count + 1
+ return ctx.a6_count
+ end, {no_cacheable = true})
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+ local httpc = http.new()
+ local res = assert(httpc:request_uri(uri))
+ ngx.print(res.body)
+ }
+ }
+--- response_body
+find ctx.var.a6_count: 1
+find ctx.var.a6_count: 2