feat(plugin): add degradation switch for ext-plugin (#5897)

Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Co-authored-by: liushan03 <liushan03@meicai.cn>
This commit is contained in:
arabot777 2021-12-28 09:39:20 +08:00 committed by GitHub
parent f7c791dcd1
commit 9106401521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 133 additions and 0 deletions

View File

@ -99,6 +99,7 @@ local schema = {
},
minItems = 1,
},
allow_degradation = {type = "boolean", default = false}
},
}
@ -700,6 +701,10 @@ function _M.communicate(conf, ctx, plugin_name)
if not core.string.find(err, "conf token not found") then
core.log.error(err)
if conf.allow_degradation then
core.log.warn("Plugin Runner is wrong, allow degradation")
return
end
return 503
end
@ -708,6 +713,10 @@ function _M.communicate(conf, ctx, plugin_name)
end
core.log.error(err)
if conf.allow_degradation then
core.log.warn("Plugin Runner is wrong after " .. tries .. " times retry, allow degradation")
return
end
return 503
end

View File

@ -43,6 +43,7 @@ The result of external plugins execution will affect the behavior of the current
| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| conf | array | optional | | [{"name": "ext-plugin-A", "value": "{\"enable\":\"feature\"}"}] | The plugins list which will be executed at the plugin runner with their configuration |
| allow_degradation | boolean | optional | false | | Whether to enable plugin degradation when the plugin runner is temporarily unavailable. Allow requests to continue when the value is set to true, default false. |
## How To Enable

View File

@ -42,6 +42,7 @@ External Plugins 执行的结果会影响当前请求的行为。
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| conf | array | 可选 | | [{"name": "ext-plugin-A", "value": "{\"enable\":\"feature\"}"}] | 在 Plugin Runner 内执行的插件列表的配置 |
| allow_degradation | boolean | 可选 | false | | 当 Plugin Runner 临时不可用时是否允许请求继续。当值设置为 true 时则自动允许请求继续,默认值是 false。|
## 如何启用

View File

@ -571,3 +571,125 @@ qr/get conf token: 233 conf: \[(\{"value":"bar","name":"foo"\}|\{"name":"foo","v
end
}
}
=== TEST 19: default allow_degradation
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local code, message, res = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"plugins": {
"ext-plugin-post-req": {
"conf": [
{"name":"foo", "value":"bar"},
{"name":"cat", "value":"dog"}
]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
ngx.say(message)
}
}
--- response_body
passed
=== TEST 20: ext-plugin wrong, req reject
--- request
GET /hello
--- extra_stream_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock1;
content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({})
}
}
--- error_code: 503
--- error_log eval
qr/failed to connect to the unix socket/
=== TEST 21: open allow_degradation
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local code, message, res = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"plugins": {
"ext-plugin-post-req": {
"conf": [
{"name":"foo", "value":"bar"},
{"name":"cat", "value":"dog"}
],
"allow_degradation": true
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
ngx.say(message)
}
}
--- response_body
passed
=== TEST 22: ext-plugin wrong, req access
--- request
GET /hello
--- extra_stream_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock1;
content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({})
}
}
--- response_body
hello world
--- error_log eval
qr/Plugin Runner.*allow degradation/