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

change: set default reject code for some plugins #1696

Merged
merged 5 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions apisix/plugins/limit-conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ local schema = {
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for"},
},
rejected_code = {type = "integer", minimum = 200},
rejected_code = {type = "integer", minimum = 200, default = 503},
},
required = {"conn", "burst", "default_conn_delay", "key", "rejected_code"}
required = {"conn", "burst", "default_conn_delay", "key"}
}


Expand Down
5 changes: 3 additions & 2 deletions apisix/plugins/limit-count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ local schema = {
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for"},
},
rejected_code = {type = "integer", minimum = 200, maximum = 600},
rejected_code = {type = "integer", minimum = 200, maximum = 600,
default = 503},
policy = {
type = "string",
enum = {"local", "redis"},
Expand All @@ -53,7 +54,7 @@ local schema = {
},
},
additionalProperties = false,
required = {"count", "time_window", "key", "rejected_code"},
required = {"count", "time_window", "key"},
}


Expand Down
4 changes: 2 additions & 2 deletions apisix/plugins/limit-req.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ local schema = {
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for"},
},
rejected_code = {type = "integer", minimum = 200},
rejected_code = {type = "integer", minimum = 200, default = 503},
},
required = {"rate", "burst", "key", "rejected_code"}
required = {"rate", "burst", "key"}
}


Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/limit-count-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
|count |必选 |指定时间窗口内的请求数量阈值|
|time_window |必选 |时间窗口的大小(以秒为单位),超过这个时间就会重置|
|key |必选 |是用来做请求计数的依据,当前接受的 key 有: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"。|
|rejected_code |必选 |T当请求超过阈值被拒绝时,返回的 HTTP 状态码。|
|rejected_code |可选 |T当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认 503。|
|policy |可选 |用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis 服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速).|
|redis_host |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。|
|redis_port |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口,默认端口 6379。|
Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/limit-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Limit request rate by a fixed number of requests in a given time window.
|count |required|the specified number of requests threshold.|
|time_window |required|the time window in seconds before the request count is reset.|
|key |required|the user specified key to limit the rate. Here is fully key list: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for".|
|rejected_code |required|The HTTP status code returned when the request exceeds the threshold is rejected.|
|rejected_code |optional|The HTTP status code returned when the request exceeds the threshold is rejected, default 503.|
|policy |optional|The rate-limiting policies to use for retrieving and incrementing the limits. Available values are `local`(the counters will be stored locally in-memory on the node, default value) and `redis`(counters are stored on a Redis server and will be shared across the nodes, usually used it to do the global speed limit).|
|redis_host |optional|When using the `redis` policy, this property specifies the address of the Redis server.|
|redis_port |optional|When using the `redis` policy, this property specifies the port of the Redis server. The default port is 6379.|
Expand Down
10 changes: 6 additions & 4 deletions doc/plugins/limit-req-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

## 参数

* `rate`:指定的请求速率(以秒为单位),请求速率超过 `rate` 但没有超过 (`rate` + `brust`)的请求会被加上延时
* `burst`:请求速率超过 (`rate` + `brust`)的请求会被直接拒绝
* `rejected_code`:当请求超过阈值被拒绝时,返回的 HTTP 状态码
* `key`:是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。
|名称 |可选项 |描述|
|--------- |--------|-----------|
|rate |必选|指定的请求速率(以秒为单位),请求速率超过 `rate` 但没有超过 (`rate` + `brust`)的请求会被加上延时。|
|burst |必选|请求速率超过 (`rate` + `brust`)的请求会被直接拒绝。|
| key |必选|是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。|
|rejected_code |可选|当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认 503。|

## 示例

Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/limit-req.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ limit request rate using the "leaky bucket" method.
|--------- |--------|-----------|
|rate |required|is the specified request rate (number per second) threshold. Requests exceeding this rate (and below `burst`) will get delayed to conform to the rate.|
|burst |required|is the number of excessive requests per second allowed to be delayed. Requests exceeding this hard limit will get rejected immediately.|
|rejected_code |required|The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.|
| key |required|is the user specified key to limit the rate, now accept those as key: "remote_addr"(client's IP), "server_addr"(server's IP), "X-Forwarded-For/X-Real-IP" in request header.|
|rejected_code |optional|The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.|

## How To Enable

Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/response-rewrite-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

|名字 |可选|说明|
|------- |-----|------|
|status_code |可选| 修改上游返回状态码|
|status_code |可选| 修改上游返回状态码,默认保留原始响应代码。|
|body |可选| 修改上游返回的 `body` 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉|
|body_base64 |可选| 布尔类型,描述 `body` 字段是否需要 base64 解码之后再返回给客户端,用在某些图片和 Protobuffer 场景|
|headers |可选| 返回给客户端的 `headers`,这里可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可|
Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/response-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ response rewrite plugin, rewrite the content from upstream.

|Name |Requirement|Description|
|------- |-----|------|
|status_code |optional| New `status code` to client|
|status_code |optional| New `status code` to client, keep the original response code by default.|
|body |optional| New `body` to client, and the content-length will be reset too.|
|body_base64 |optional| This is a boolean value,identify if `body` in configuration need base64 decoded before rewrite to client.|
|headers |optional| Set the new `headers` for client, can set up multiple. If it exists already from upstream, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. |
Expand Down
2 changes: 1 addition & 1 deletion t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GET /apisix/admin/plugins
--- request
GET /apisix/admin/plugins/limit-req
--- response_body
{"properties":{"rate":{"minimum":0,"type":"number"},"burst":{"minimum":0,"type":"number"},"key":{"enum":["remote_addr","server_addr","http_x_real_ip","http_x_forwarded_for"],"type":"string"},"rejected_code":{"minimum":200,"type":"integer"}},"required":["rate","burst","key","rejected_code"],"type":"object"}
{"properties":{"rate":{"minimum":0,"type":"number"},"burst":{"minimum":0,"type":"number"},"key":{"enum":["remote_addr","server_addr","http_x_real_ip","http_x_forwarded_for"],"type":"string"},"rejected_code":{"type":"integer","default":503,"minimum":200}},"required":["rate","burst","key"],"type":"object"}
--- no_error_log
[error]

Expand Down
2 changes: 1 addition & 1 deletion t/admin/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ passed
--- request
GET /apisix/admin/schema/plugins/limit-count
--- response_body eval
qr/"required":\["count","time_window","key","rejected_code"]/
qr/"required":\["count","time_window","key"\]/
--- no_error_log
[error]

Expand Down
61 changes: 61 additions & 0 deletions t/plugin/limit-conn.t
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,64 @@ GET /test_concurrency
503
--- error_log
limit key: 10.10.10.2route



=== TEST 20: default rejected_code
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-conn": {
"conn": 100,
"burst": 50,
"default_conn_delay": 0.1,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/limit_conn"
}]],
[[{
"node": {
"value": {
"plugins": {
"limit-conn": {
"rejected_code": 503
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/limit_conn"
},
"key": "/apisix/routes/1"
},
"action": "set"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
52 changes: 52 additions & 0 deletions t/plugin/limit-count.t
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,55 @@ passed
[200, 200, 503, 503]
--- no_error_log
[error]



=== TEST 20: default rejected_code
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]],
[[{
"node": {
"value": {
"plugins": {
"limit-count": {
"rejected_code": 503
}
}
}
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
53 changes: 53 additions & 0 deletions t/plugin/limit-req.t
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,56 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 11: default rejected_code
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-req": {
"rate": 4,
"burst": 2,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"desc": "上游节点",
"uri": "/hello"
}]],
[[{
"node": {
"value": {
"plugins": {
"limit-req": {
"rejected_code": 503,
"key": "remote_addr"
}
}
}
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]