You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/04/11 05:43:33 UTC

[apisix] branch master updated: feat: new way to cache ctx.var (#3915)

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 022d752  feat: new way to cache ctx.var (#3915)
022d752 is described below

commit 022d7527c27fd0cc8a59b38394116e5cf188952f
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Sun Apr 11 13:43:25 2021 +0800

    feat: new way to cache ctx.var (#3915)
---
 apisix/core/ctx.lua | 17 +++++++---
 t/core/ctx2.t       | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index d87342b..3d88800 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -26,7 +26,6 @@ local ck           = require "resty.cookie"
 local gq_parse     = require("graphql").parse
 local setmetatable = setmetatable
 local sub_str      = string.sub
-local rawset       = rawset
 local ngx          = ngx
 local ngx_var      = ngx.var
 local re_gsub      = ngx.re.gsub
@@ -133,6 +132,11 @@ do
 
     local mt = {
         __index = function(t, key)
+            local cached = t._cache[key]
+            if cached ~= nil then
+                return cached
+            end
+
             if type(key) ~= "string" then
                 error("invalid argument, expect string value", 2)
             end
@@ -183,7 +187,7 @@ do
             end
 
             if val ~= nil then
-                rawset(t, key, val)
+                t._cache[key] = val
             end
 
             return val
@@ -195,12 +199,16 @@ do
             end
 
             -- log.info("key: ", key, " new val: ", val)
-            rawset(t, key, val)
+            t._cache[key] = val
         end,
     }
 
 function _M.set_vars_meta(ctx)
     local var = tablepool.fetch("ctx_var", 0, 32)
+    if not var._cache then
+        var._cache = {}
+    end
+
     var._request = get_request()
     setmetatable(var, mt)
     ctx.var = var
@@ -211,7 +219,8 @@ function _M.release_vars(ctx)
         return
     end
 
-    tablepool.release("ctx_var", ctx.var)
+    core_tab.clear(ctx.var._cache)
+    tablepool.release("ctx_var", ctx.var, true)
     ctx.var = nil
 end
 
diff --git a/t/core/ctx2.t b/t/core/ctx2.t
new file mode 100644
index 0000000..d4aabf5
--- /dev/null
+++ b/t/core/ctx2.t
@@ -0,0 +1,89 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->response_body) {
+        $block->set_value("response_body", "passed\n");
+    }
+
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: should update cached ctx.var
+--- 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,
+                 [[{
+                    "plugins": {
+                        "serverless-post-function": {
+                            "functions" : ["return function(conf, ctx)
+                                        ngx.log(ngx.WARN, 'pre uri: ', ctx.var.upstream_uri);
+                                        ctx.var.upstream_uri = '/server_port';
+                                        ngx.log(ngx.WARN, 'post uri: ', ctx.var.upstream_uri);
+                                        end"]
+                        },
+                        "proxy-rewrite": {
+                            "uri": "/hello"
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/xxx"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+
+
+
+=== TEST 2: check
+--- request
+GET /xxx
+--- response_body chomp
+1980
+--- error_log
+pre uri: /hello
+post uri: /server_port