You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/10/19 01:35:34 UTC

[GitHub] [apisix] spacewander commented on a diff in pull request #8113: fix(ai): add dynamic route cache key

spacewander commented on code in PR #8113:
URL: https://github.com/apache/apisix/pull/8113#discussion_r998858316


##########
apisix/core/ai.lua:
##########
@@ -17,6 +17,24 @@
 local require         = require
 local core            = require("apisix.core")
 local ipairs          = ipairs
+local pcall           = pcall
+local loadstring      = loadstring
+local encode_base64   = ngx.encode_base64
+
+local get_cache_key_func
+local get_cache_key_func_def_render
+
+local get_cache_key_func_def = [[
+return function(ctx)
+    return ctx.var.uri

Review Comment:
   We can make it a little faster, by using `local var = ctx.var` to reduce table lookup.



##########
t/core/ai.t:
##########
@@ -474,3 +474,177 @@ qr/use ai plane to match route/
 --- grep_error_log_out
 use ai plane to match route
 use ai plane to match route
+
+
+
+=== TEST 6: route key: uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+            ngx.sleep(1)
+
+            -- enable
+            do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri)
+                assert(res.status == 200)
+                if not res then
+                    ngx.log(ngx.ERR, err)
+                    return
+                end
+            end
+
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri)
+            assert(res.status == 200)
+            if not res then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+
+            ngx.say("done")
+        }
+    }
+--- response_body
+done
+--- error_log
+route cache key: L2hlbGxv
+
+
+
+=== TEST 7: route key: uri + method
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "methods": ["GET"],
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+            ngx.sleep(1)
+
+            -- enable
+            do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri)
+                assert(res.status == 200)
+                if not res then
+                    ngx.log(ngx.ERR, err)
+                    return
+                end
+            end
+
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri)
+            assert(res.status == 200)
+            if not res then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+
+            ngx.say("done")
+        }
+    }
+--- response_body
+done
+--- error_log
+route cache key: L2hlbGxvAEdFVA==
+
+
+
+=== TEST 8: route key: uri + method + host
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "host": "127.0.0.1",
+                    "methods": ["GET"],
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+            ngx.sleep(1)
+
+            -- enable
+            do

Review Comment:
   Let's exact this common part into a function, instead of repeating it three times.



##########
apisix/core/ai.lua:
##########
@@ -48,10 +66,41 @@ local function ai_match(ctx)
 end
 
 
+local function gen_get_cache_key_func(route_flags)
+    if get_cache_key_func_def_render == nil then
+        local template = require("resty.template")
+        get_cache_key_func_def_render = template.compile(get_cache_key_func_def)
+    end
+
+    local str = get_cache_key_func_def_render({route_flags = route_flags})
+    local func, err = loadstring(str)
+    if func == nil then
+        return false, err
+    else
+        local ok
+        ok, get_cache_key_func = pcall(func)

Review Comment:
   Better to use a temp var instead of hijacking a module var to hold the potential err message



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org