Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext-plugin): when token is stale, refresh token and try again #4345

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions apisix/plugins/ext-plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there a blank line here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the return easy to find.

return
end

if not core.string.find(err, "conf token not found") then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use code to determine this, I'm not sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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,
Expand Down
11 changes: 9 additions & 2 deletions t/lib/ext-plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions t/plugin/ext-plugin/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]