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

feat: add additional option to control the default_conn_delay option of the limit-conn plugin #4604

Merged
merged 22 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
344701b
修复多个request-id产生的bug
zuiyangqingzhou Jun 24, 2021
18e2ded
Merge branch 'fix-request-id'
zuiyangqingzhou Jun 24, 2021
fe7df38
Merge branch 'apache:master' into master
zuiyangqingzhou Jun 27, 2021
ffe870e
1、request-id add prefix
zuiyangqingzhou Jun 27, 2021
9e02e21
Merge branch 'fix-request-id'
zuiyangqingzhou Jun 27, 2021
6f1efed
remove unnecessary logic
zuiyangqingzhou Jun 28, 2021
cdb3798
Merge branch 'fix-request-id'
zuiyangqingzhou Jun 28, 2021
2489318
fix the linter
zuiyangqingzhou Jun 30, 2021
83ef3f3
Merge branch 'apache:master' into master
zuiyangqingzhou Jun 30, 2021
08fef26
Merge branch 'fix-request-id'
zuiyangqingzhou Jun 30, 2021
c48ac1b
Merge branch 'apache:master' into master
zuiyangqingzhou Jul 4, 2021
e6ea846
Merge branch 'apache:master' into master
zuiyangqingzhou Jul 11, 2021
4a58f9c
Merge branch 'apache:master' into master
zuiyangqingzhou Jul 14, 2021
753a956
add additional option to control the default_conn_delay parameter
zuiyangqingzhou Jul 14, 2021
abfa56c
adjustment format
zuiyangqingzhou Jul 14, 2021
c54d2bc
adjustment format
zuiyangqingzhou Jul 14, 2021
3d3f9d5
Merge branch 'apache:master' into master
zuiyangqingzhou Jul 18, 2021
ba19219
Merge branch 'master' into optimize_conn_delay
zuiyangqingzhou Jul 18, 2021
0ee8e03
add test cases
zuiyangqingzhou Jul 18, 2021
00d716c
fix code style error
zuiyangqingzhou Jul 18, 2021
47dc329
fix code style error
zuiyangqingzhou Jul 19, 2021
bdedb6c
fix log phase conf param error
zuiyangqingzhou Jul 22, 2021
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
2 changes: 1 addition & 1 deletion apisix/plugins/limit-conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local schema = {
conn = {type = "integer", exclusiveMinimum = 0},
burst = {type = "integer", minimum = 0},
default_conn_delay = {type = "number", exclusiveMinimum = 0},
delay_strict_mode = {type = "boolean", default = false},
only_use_default_delay = {type = "boolean", default = false},
key = {type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
Expand Down
4 changes: 2 additions & 2 deletions apisix/plugins/limit-conn/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ function _M.decrease(conf, ctx)
local delay = limit_conn[i + 2]

local latency
if not conf.delay_strict_mode then
if not conf.only_use_default_delay then
if ctx.proxy_passed then
latency = ctx.var.upstream_response_time
else
latency = ctx.var.request_time - delay
end
core.log.debug("request latency is ", latency) -- for test
end
core.log.debug("request latency is ", latency) -- for test

local conn, err = lim:leaving(key, latency)
if not conn then
Expand Down
2 changes: 1 addition & 1 deletion apisix/stream/plugins/limit-conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local schema = {
conn = {type = "integer", exclusiveMinimum = 0},
burst = {type = "integer", minimum = 0},
default_conn_delay = {type = "number", exclusiveMinimum = 0},
delay_strict_mode = {type = "boolean", default = false},
only_use_default_delay = {type = "boolean", default = false},
key = {
type = "string",
enum = {"remote_addr", "server_addr"}
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/limit-conn.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Limiting request concurrency plugin.
| conn | integer | required | | conn > 0 | the maximum number of concurrent requests allowed. Requests exceeding this ratio (and below `conn` + `burst`) will get delayed(the latency seconds is configured by `default_conn_delay`) to conform to this threshold. |
| burst | integer | required | | burst >= 0 | the number of excessive concurrent requests (or connections) allowed to be delayed. |
| default_conn_delay | number | required | | default_conn_delay > 0 | the latency seconds of request when concurrent requests exceeding `conn` but below (`conn` + `burst`). |
| delay_strict_mode | boolean | optional | false | [true,false] | enable the strict mode of the latency seconds. If you set this option to `true`, it will run strictly according to the latency seconds you set without additional calculation logic. |
| only_use_default_delay | boolean | optional | false | [true,false] | enable the strict mode of the latency seconds. If you set this option to `true`, it will run strictly according to the latency seconds you set without additional calculation logic. |
| key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | to limit the concurrency level. <br />For example, one can use the host name (or server zone) as the key so that we limit concurrency per host name. Otherwise, we can also use the client address as the key so that we can avoid a single client from flooding our service with too many parallel connections or requests. <br /> Now accept those as key: "remote_addr"(client's IP), "server_addr"(server's IP), "X-Forwarded-For/X-Real-IP" in request header, "consumer_name"(consumer's username). |
| rejected_code | string | optional | 503 | [200,...,599] | returned when the request exceeds `conn` + `burst` will be rejected. |

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/limit-conn.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ title: limit-conn
| conn | integer | required | | conn > 0 | 允许的最大并发请求数。超过 `conn` 的限制、但是低于 `conn` + `burst` 的请求,将被延迟处理。 |
| burst | integer | required | | burst >= 0 | 允许被延迟处理的并发请求数。 |
| default_conn_delay | number | required | | default_conn_delay > 0 | 默认的典型连接(或请求)的处理延迟时间。 |
| delay_strict_mode | boolean | optional | false | [true,false] | 延迟时间的严格模式. 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 |
| only_use_default_delay | boolean | optional | false | [true,false] | 延迟时间的严格模式. 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 |
tokers marked this conversation as resolved.
Show resolved Hide resolved
| key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。<br />例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。 <br />当前接受的 key 有:"remote_addr"(客户端 IP 地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP", "consumer_name"(consumer 的 username)。 |
| rejected_code | string | optional | 503 | [200,...,599] | 当请求超过 `conn` + `burst` 这个阈值时,返回的 HTTP 状态码 |

Expand Down
73 changes: 73 additions & 0 deletions t/plugin/limit-conn2.t
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,76 @@ qr/request latency is/
--- grep_error_log_out
request latency is
request latency is



=== TEST 5: set only_use_default_delay option to true in specific route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-conn": {
"conn": 1,
"burst": 0,
"default_conn_delay": 0.3,
"rejected_code": 503,
"key": "remote_addr"
}
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(body)
return
end

local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello1",
"plugins": {
"limit-conn": {
"conn": 1,
"burst": 0,
"default_conn_delay": 0.3,
"only_use_default_delay": true,
"rejected_code": 503,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed


=== TEST 6: hit route
--- log_level: debug
--- request
GET /hello1
--- grep_error_log eval
qr/request latency is/
Copy link
Member

Choose a reason for hiding this comment

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

Need to check latency in this mode is nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know exactly what you mean,

https://github.com/apache/apisix/pull/4604/files/47dc32979a94977aa8e3d0e73270dc88a9c43149#diff-2f0a07663e8e90e9f64519ee946aaf82729c8bb43e4f9100902e87d0a325f78eR251-R253

hasn't grep_error_log_out already check that the latency is nil?
Or would you give some guidance on how to do it ?

Copy link
Member

Choose a reason for hiding this comment

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

The grep pattern should be request latency is nil.

--- grep_error_log_out
request latency is
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
request latency is
request latency is nil

request latency is

tokers marked this conversation as resolved.
Show resolved Hide resolved