From d484d442e60a9427938652151064455d0e7bd5ca Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 31 May 2021 17:56:50 +0800 Subject: [PATCH] fix(ext-plugin): when token is stale, refresh token and try again Signed-off-by: spacewander --- apisix/plugins/ext-plugin/init.lua | 42 ++++++++++++++++++++---------- t/lib/ext-plugin.lua | 11 ++++++-- t/plugin/ext-plugin/sanity.t | 31 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index 9f83bf4e4f3c..661966dc76e9 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 spawn_proc(cmd) local opt = { merge_stderr = true, diff --git a/t/lib/ext-plugin.lua b/t/lib/ext-plugin.lua index 5ef8eb96a8ee..3f0e616c286a 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 905215e93221..4a08792a74c4 100644 --- a/t/plugin/ext-plugin/sanity.t +++ b/t/plugin/ext-plugin/sanity.t @@ -337,3 +337,34 @@ GET /hello --- error_code: 503 --- error_log failed to receive RPC_PREPARE_CONF: bad request + + + +=== 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]