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/06/01 11:46:39 UTC

[apisix] branch master updated: fix(ext-plugin): when token is stale, refresh token and try again (#4345)

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 619ac84  fix(ext-plugin): when token is stale, refresh token and try again (#4345)
619ac84 is described below

commit 619ac84c8892b0592fa8c1b625b54d8087fa3483
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Tue Jun 1 19:46:26 2021 +0800

    fix(ext-plugin): when token is stale, refresh token and try again (#4345)
---
 apisix/plugins/ext-plugin/init.lua | 42 +++++++++++++++++++++++++-------------
 t/lib/ext-plugin.lua               | 11 ++++++++--
 t/plugin/ext-plugin/sanity.t       | 33 +++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua
index c227be0..7be360f 100644
--- a/apisix/plugins/ext-plugin/init.lua
+++ b/apisix/plugins/ext-plugin/init.lua
@@ -521,20 +521,6 @@ rpc_call = function (ty, conf, ctx)
 end
 
 
-function _M.communicate(conf, ctx)
-    local ok, err, code, body = rpc_call(constants.RPC_HTTP_REQ_CALL, conf, ctx)
-    if not ok then
-        core.log.error(err)
-        return 503
-    end
-
-    if code then
-        return code, body
-    end
-    return
-end
-
-
 local function create_lrucache()
     if lrucache then
         core.log.warn("flush conf token lrucache")
@@ -547,6 +533,34 @@ local function create_lrucache()
 end
 
 
+function _M.communicate(conf, ctx)
+    local ok, err, code, body
+    local tries = 0
+    while tries < 3 do
+        tries = tries + 1
+        ok, err, code, body = rpc_call(constants.RPC_HTTP_REQ_CALL, conf, ctx)
+        if ok then
+            if code then
+                return code, body
+            end
+
+            return
+        end
+
+        if not core.string.find(err, "conf token not found") then
+            core.log.error(err)
+            return 503
+        end
+
+        core.log.warn("refresh cache and try again")
+        create_lrucache()
+    end
+
+    core.log.error(err)
+    return 503
+end
+
+
 local function must_set(env, value)
     local ok, err = core.os.setenv(env, value)
     if not ok then
diff --git a/t/lib/ext-plugin.lua b/t/lib/ext-plugin.lua
index 5ef8eb9..3f0e616 100644
--- a/t/lib/ext-plugin.lua
+++ b/t/lib/ext-plugin.lua
@@ -81,9 +81,16 @@ function _M.go(case)
             builder:Finish(req)
             data = builder:Output()
         end
-    end
 
-    if ty == constants.RPC_HTTP_REQ_CALL then
+    elseif case.no_token then
+        ty = constants.RPC_ERROR
+        err_resp.Start(builder)
+        err_resp.AddCode(builder, err_code.CONF_TOKEN_NOT_FOUND)
+        local req = prepare_conf_req.End(builder)
+        builder:Finish(req)
+        data = builder:Output()
+
+    elseif ty == constants.RPC_HTTP_REQ_CALL then
         local buf = flatbuffers.binaryArray.New(data)
         local call_req = http_req_call_req.GetRootAsReq(buf, 0)
         if case.check_input then
diff --git a/t/plugin/ext-plugin/sanity.t b/t/plugin/ext-plugin/sanity.t
index ac21eeb..bde3fcc 100644
--- a/t/plugin/ext-plugin/sanity.t
+++ b/t/plugin/ext-plugin/sanity.t
@@ -340,7 +340,38 @@ failed to receive RPC_PREPARE_CONF: bad request
 
 
 
-=== TEST 12: runner can access the environment variable
+=== TEST 12: refresh token
+--- request
+GET /hello
+--- response_body
+hello world
+--- extra_stream_config
+    server {
+        listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
+
+        content_by_lua_block {
+            local ext = require("lib.ext-plugin")
+            if not package.loaded.count then
+                package.loaded.count = 1
+            else
+                package.loaded.count = package.loaded.count + 1
+            end
+
+            if package.loaded.count == 1 then
+                ext.go({no_token = true})
+            else
+                ext.go({with_conf = true})
+            end
+        }
+    }
+--- error_log
+refresh cache and try again
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: runner can access the environment variable
 --- main_config
 env MY_ENV_VAR=foo;
 --- ext_plugin_cmd