Skip to content

Commit

Permalink
feat(api-breaker): allow configuring response (#6949)
Browse files Browse the repository at this point in the history
Co-authored-by: qihaiyan <qihaiyan@hisense.com>
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: Alex Zhang <tokers@apache.org>
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
  • Loading branch information
5 people committed Jun 30, 2022
1 parent b533590 commit d30f77e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
30 changes: 30 additions & 0 deletions apisix/plugins/api-breaker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ local plugin_name = "api-breaker"
local ngx = ngx
local math = math
local error = error
local ipairs = ipairs


local shared_buffer = ngx.shared["plugin-".. plugin_name]
if not shared_buffer then
Expand All @@ -35,6 +37,25 @@ local schema = {
minimum = 200,
maximum = 599,
},
break_response_body = {
type = "string"
},
break_response_headers = {
type = "array",
items = {
type = "object",
properties = {
key = {
type = "string",
minLength = 1
},
value = {
type = "string",
minLength = 1
}
}
}
},
max_breaker_sec = {
type = "integer",
minimum = 3,
Expand Down Expand Up @@ -158,6 +179,15 @@ function _M.access(conf, ctx)

-- breaker
if lasttime + breaker_time >= ngx.time() then
if conf.break_response_body then
if conf.break_response_headers then
for _, value in ipairs(conf.break_response_headers) do
local val = core.utils.resolve_var(value.value, ctx.var)
core.response.add_header(value.key, val)
end
end
return conf.break_response_code, conf.break_response_body
end
return conf.break_response_code
end

Expand Down
2 changes: 2 additions & 0 deletions docs/en/latest/plugins/api-breaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ In an unhealthy state, when a request is forwarded to an upstream service and th
| Name | Type | Requirement | Default | Valid | Description |
| ----------------------- | ------------- | ----------- | -------- | --------------- | --------------------------------------------------------------------------- |
| break_response_code | integer | required | | [200, ..., 599] | Return error code when unhealthy |
| break_response_body | string | optional | | | Return response body when unhealthy |
| break_response_headers | array[object] | optional | | | New headers for the response. The values in the header can contain Nginx variables like `$remote_addr` and `$balancer_ip`. This field is in effective only if `break_response_body` is configured. |
| max_breaker_sec | integer | optional | 300 | >=3 | Maximum breaker time(seconds) |
| unhealthy.http_statuses | array[integer] | optional | {500} | [500, ..., 599] | Status codes when unhealthy |
| unhealthy.failures | integer | optional | 3 | >=1 | Number of consecutive error requests that triggered an unhealthy state |
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/latest/plugins/api-breaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ title: api-breaker
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| ----------------------- | -------------- | ------ | ---------- | --------------- | -------------------------------- |
| break_response_code | integer | 必须 || [200, ..., 599] | 不健康返回错误码 |
| break_response_body | string | 可选 || | 不健康返回报文 |
| break_response_headers | array[object] | 可选 || | 不健康返回报文头,这里可以设置多个。这个值能够以 `$var` 的格式包含 Nginx 变量,比如 `$remote_addr $balancer_ip`。该字段仅在 `break_response_body` 被配置时生效 |
| max_breaker_sec | integer | 可选 | 300 | >=3 | 最大熔断持续时间 |
| unhealthy.http_statuses | array[integer] | 可选 | {500} | [500, ..., 599] | 不健康时候的状态码 |
| unhealthy.failures | integer | 可选 | 3 | >=1 | 触发不健康状态的连续错误请求次数 |
Expand Down
6 changes: 6 additions & 0 deletions t/plugin/api-breaker.t
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ breaker_time: 2
"plugins": {
"api-breaker": {
"break_response_code": 502,
"break_response_body": "{\"message\":\"breaker opened.\"}",
"break_response_headers": [{"key":"Content-Type","value":"application/json"},{"key":"Content-Type","value":"application/json+v1"}],
"unhealthy": {
"failures": 3
},
Expand Down Expand Up @@ -509,6 +511,10 @@ GET /api_breaker?code=500
]
--- error_code eval
[200, 500, 503, 500, 500, 502]
--- response_headers eval
["Content-Type: text/plain", "Content-Type: text/html", "Content-Type: text/html", "Content-Type: text/html", "Content-Type: text/html", "Content-Type: application/json+v1"]
--- response_body_like eval
[".*", ".*", ".*", ".*", ".*", "{\"message\":\"breaker opened.\"}"]
--- no_error_log
[error]

Expand Down

0 comments on commit d30f77e

Please sign in to comment.