From 344701bfeadfbe7271bdc5c9e6443b03f5916b02 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Thu, 24 Jun 2021 21:01:11 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AAreque?= =?UTF-8?q?st-id=E4=BA=A7=E7=94=9F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apisix/plugins/request-id.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua index 60e590a3498c..13bb6fb31f9b 100644 --- a/apisix/plugins/request-id.lua +++ b/apisix/plugins/request-id.lua @@ -49,7 +49,7 @@ function _M.rewrite(conf, ctx) end if conf.include_in_response then - ctx.x_request_id = uuid_val + ctx[conf.header_name] = uuid_val end end @@ -61,7 +61,7 @@ function _M.header_filter(conf, ctx) local headers = ngx.resp.get_headers() if not headers[conf.header_name] then - core.response.set_header(conf.header_name, ctx.x_request_id) + core.response.set_header(conf.header_name, ctx[conf.header_name]) end end From ffe870ef8ce14f27978638017970fac42df7a884 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Sun, 27 Jun 2021 22:59:32 +0800 Subject: [PATCH 02/11] =?UTF-8?q?1=E3=80=81request-id=20add=20prefix=202?= =?UTF-8?q?=E3=80=81complete=20test=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apisix/plugins/request-id.lua | 4 +- t/plugin/request-id.t | 83 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua index 13bb6fb31f9b..79b8b183dad1 100644 --- a/apisix/plugins/request-id.lua +++ b/apisix/plugins/request-id.lua @@ -49,7 +49,7 @@ function _M.rewrite(conf, ctx) end if conf.include_in_response then - ctx[conf.header_name] = uuid_val + ctx["request-id-" .. conf.header_name] = uuid_val end end @@ -61,7 +61,7 @@ function _M.header_filter(conf, ctx) local headers = ngx.resp.get_headers() if not headers[conf.header_name] then - core.response.set_header(conf.header_name, ctx[conf.header_name]) + core.response.set_header(conf.header_name, ctx["request-id-" .. conf.header_name]) end end diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index ff4778a24d17..b8dad2d509ff 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -387,3 +387,86 @@ GET /t request header not present --- no_error_log [error] + + + +=== TEST 10: add plugin with custom header name in global rule and add plugin with default header name in specific route +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/global_rules/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "request-id": { + "header_name":"Custom-Header-Name" + } + } + }]] + ) + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "request-id": { + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]] + ) + if code >= 300 then + ngx.status = code + return + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + +=== TEST 11: check for multiple request-ids in the response header are different +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local httpc = http.new() + local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/opentracing" + local res, err = httpc:request_uri(uri, + { + method = "GET", + headers = { + ["Content-Type"] = "application/json", + } + }) + + if res.headers["X-Request-Id"] and res.headers["Custom-Header-Name"] and res.headers["X-Request-Id"] ~= res.headers["Custom-Header-Name"] then + ngx.say("X-Request-Id and Custom-Header-Name is different") + else + ngx.say("failed") + end + } + } +--- request +GET /t +--- response_body +X-Request-Id and Custom-Header-Name is different +--- no_error_log +[error] + From 6f1efedff7677faa4d5b9755d70b4e2d30977be5 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Mon, 28 Jun 2021 14:39:20 +0800 Subject: [PATCH 03/11] remove unnecessary logic --- t/plugin/request-id.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index b8dad2d509ff..110dbd5208cc 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -456,8 +456,8 @@ passed } }) - if res.headers["X-Request-Id"] and res.headers["Custom-Header-Name"] and res.headers["X-Request-Id"] ~= res.headers["Custom-Header-Name"] then - ngx.say("X-Request-Id and Custom-Header-Name is different") + if res.headers["X-Request-Id"] ~= res.headers["Custom-Header-Name"] then + ngx.say("X-Request-Id and Custom-Header-Name are different") else ngx.say("failed") end @@ -466,7 +466,7 @@ passed --- request GET /t --- response_body -X-Request-Id and Custom-Header-Name is different +X-Request-Id and Custom-Header-Name are different --- no_error_log [error] From 2489318ad5fb695bc508b2e764f5717baa8b33bb Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Wed, 30 Jun 2021 09:44:59 +0800 Subject: [PATCH 04/11] fix the linter --- t/plugin/request-id.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t index 110dbd5208cc..5fadc447738e 100644 --- a/t/plugin/request-id.t +++ b/t/plugin/request-id.t @@ -441,6 +441,7 @@ passed [error] + === TEST 11: check for multiple request-ids in the response header are different --- config location /t { @@ -469,4 +470,3 @@ GET /t X-Request-Id and Custom-Header-Name are different --- no_error_log [error] - From 753a9563ab05f39b98d99e4fe7e469961fbcc09e Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Wed, 14 Jul 2021 18:19:33 +0800 Subject: [PATCH 05/11] add additional option to control the default_conn_delay parameter --- apisix/plugins/limit-conn.lua | 1 + apisix/plugins/limit-conn/init.lua | 13 +++++++------ apisix/stream/plugins/limit-conn.lua | 1 + docs/en/latest/plugins/limit-conn.md | 1 + docs/zh/latest/plugins/limit-conn.md | 1 + 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apisix/plugins/limit-conn.lua b/apisix/plugins/limit-conn.lua index 2f174c9ac3ae..e1c0a66fae13 100644 --- a/apisix/plugins/limit-conn.lua +++ b/apisix/plugins/limit-conn.lua @@ -25,6 +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}, key = {type = "string", enum = {"remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"}, diff --git a/apisix/plugins/limit-conn/init.lua b/apisix/plugins/limit-conn/init.lua index d2935c273146..e10c9825402f 100644 --- a/apisix/plugins/limit-conn/init.lua +++ b/apisix/plugins/limit-conn/init.lua @@ -83,14 +83,15 @@ function _M.decrease(conf, ctx) local delay = limit_conn[i + 2] local latency - if ctx.proxy_passed then - latency = ctx.var.upstream_response_time - else - latency = ctx.var.request_time - delay + if not conf.delay_strict_mode 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 core.log.error("failed to record the connection leaving request: ", diff --git a/apisix/stream/plugins/limit-conn.lua b/apisix/stream/plugins/limit-conn.lua index 6f949c3d081c..773aac4986d3 100644 --- a/apisix/stream/plugins/limit-conn.lua +++ b/apisix/stream/plugins/limit-conn.lua @@ -25,6 +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}, key = { type = "string", enum = {"remote_addr", "server_addr"} diff --git a/docs/en/latest/plugins/limit-conn.md b/docs/en/latest/plugins/limit-conn.md index b402a268ab07..99c56d780fee 100644 --- a/docs/en/latest/plugins/limit-conn.md +++ b/docs/en/latest/plugins/limit-conn.md @@ -40,6 +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. | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | to limit the concurrency level.
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.
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. | diff --git a/docs/zh/latest/plugins/limit-conn.md b/docs/zh/latest/plugins/limit-conn.md index ef02c93c0cbd..39cf13b3fe3e 100644 --- a/docs/zh/latest/plugins/limit-conn.md +++ b/docs/zh/latest/plugins/limit-conn.md @@ -30,6 +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`的话,将会严格按照设置的时间来进行延迟 | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。
例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
当前接受的 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 状态码 | From abfa56ccee5c0781bcea6d6466e91caa7e3efb94 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Wed, 14 Jul 2021 18:47:21 +0800 Subject: [PATCH 06/11] adjustment format --- docs/en/latest/plugins/limit-conn.md | 2 +- docs/zh/latest/plugins/limit-conn.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/latest/plugins/limit-conn.md b/docs/en/latest/plugins/limit-conn.md index 99c56d780fee..89a335ea7c4f 100644 --- a/docs/en/latest/plugins/limit-conn.md +++ b/docs/en/latest/plugins/limit-conn.md @@ -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. | +| 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. | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | to limit the concurrency level.
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.
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. | diff --git a/docs/zh/latest/plugins/limit-conn.md b/docs/zh/latest/plugins/limit-conn.md index 39cf13b3fe3e..f08f96163879 100644 --- a/docs/zh/latest/plugins/limit-conn.md +++ b/docs/zh/latest/plugins/limit-conn.md @@ -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`的话,将会严格按照设置的时间来进行延迟 +| delay_strict_mode | boolean | optional | false | [true,false] | 延迟时间的严格模式. 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。
例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
当前接受的 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 状态码 | From c54d2bc1af5e3b626db5decc0a1d5a15f50e5b35 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Wed, 14 Jul 2021 18:49:51 +0800 Subject: [PATCH 07/11] adjustment format --- docs/zh/latest/plugins/limit-conn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/latest/plugins/limit-conn.md b/docs/zh/latest/plugins/limit-conn.md index f08f96163879..0da68fcec622 100644 --- a/docs/zh/latest/plugins/limit-conn.md +++ b/docs/zh/latest/plugins/limit-conn.md @@ -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`的话,将会严格按照设置的时间来进行延迟 | +| delay_strict_mode | boolean | optional | false | [true,false] | 延迟时间的严格模式. 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。
例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
当前接受的 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 状态码 | From 0ee8e03ebacb5d40392b5a6419b9ca828bb24290 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Sun, 18 Jul 2021 20:59:15 +0800 Subject: [PATCH 08/11] add test cases --- apisix/plugins/limit-conn.lua | 2 +- apisix/plugins/limit-conn/init.lua | 4 +- apisix/stream/plugins/limit-conn.lua | 2 +- docs/en/latest/plugins/limit-conn.md | 2 +- docs/zh/latest/plugins/limit-conn.md | 2 +- t/plugin/limit-conn2.t | 73 ++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 6 deletions(-) diff --git a/apisix/plugins/limit-conn.lua b/apisix/plugins/limit-conn.lua index e1c0a66fae13..40a85c1b0740 100644 --- a/apisix/plugins/limit-conn.lua +++ b/apisix/plugins/limit-conn.lua @@ -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"}, diff --git a/apisix/plugins/limit-conn/init.lua b/apisix/plugins/limit-conn/init.lua index 2b149f090e28..103d36cffe83 100644 --- a/apisix/plugins/limit-conn/init.lua +++ b/apisix/plugins/limit-conn/init.lua @@ -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 diff --git a/apisix/stream/plugins/limit-conn.lua b/apisix/stream/plugins/limit-conn.lua index 773aac4986d3..d2bd25e97d57 100644 --- a/apisix/stream/plugins/limit-conn.lua +++ b/apisix/stream/plugins/limit-conn.lua @@ -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"} diff --git a/docs/en/latest/plugins/limit-conn.md b/docs/en/latest/plugins/limit-conn.md index 89a335ea7c4f..bba984ef1cc7 100644 --- a/docs/en/latest/plugins/limit-conn.md +++ b/docs/en/latest/plugins/limit-conn.md @@ -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.
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.
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. | diff --git a/docs/zh/latest/plugins/limit-conn.md b/docs/zh/latest/plugins/limit-conn.md index 0da68fcec622..4e5d3c515667 100644 --- a/docs/zh/latest/plugins/limit-conn.md +++ b/docs/zh/latest/plugins/limit-conn.md @@ -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`的话,将会严格按照设置的时间来进行延迟 | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。
例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
当前接受的 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 状态码 | diff --git a/t/plugin/limit-conn2.t b/t/plugin/limit-conn2.t index c1d8f0683cd0..a199852b9aa2 100644 --- a/t/plugin/limit-conn2.t +++ b/t/plugin/limit-conn2.t @@ -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/ +--- grep_error_log_out +request latency is +request latency is + From 00d716cf45737912c9e69dfe8e6754961e0bd463 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Sun, 18 Jul 2021 21:04:15 +0800 Subject: [PATCH 09/11] fix code style error --- t/plugin/limit-conn2.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/plugin/limit-conn2.t b/t/plugin/limit-conn2.t index a199852b9aa2..20190e421de6 100644 --- a/t/plugin/limit-conn2.t +++ b/t/plugin/limit-conn2.t @@ -241,6 +241,7 @@ request latency is passed + === TEST 6: hit route --- log_level: debug --- request From 47dc32979a94977aa8e3d0e73270dc88a9c43149 Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Mon, 19 Jul 2021 09:38:37 +0800 Subject: [PATCH 10/11] fix code style error --- docs/zh/latest/plugins/limit-conn.md | 2 +- t/plugin/limit-conn2.t | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/zh/latest/plugins/limit-conn.md b/docs/zh/latest/plugins/limit-conn.md index 4e5d3c515667..7dfb2fa952af 100644 --- a/docs/zh/latest/plugins/limit-conn.md +++ b/docs/zh/latest/plugins/limit-conn.md @@ -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 | 默认的典型连接(或请求)的处理延迟时间。 | -| only_use_default_delay | boolean | optional | false | [true,false] | 延迟时间的严格模式. 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 | +| only_use_default_delay | boolean | optional | false | [true,false] | 延迟时间的严格模式。 如果设置为`true`的话,将会严格按照设置的时间来进行延迟 | | key | object | required | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用户指定的限制并发级别的关键字,可以是客户端 IP 或服务端 IP。
例如,可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 否则,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
当前接受的 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 状态码 | diff --git a/t/plugin/limit-conn2.t b/t/plugin/limit-conn2.t index 20190e421de6..1f4461460346 100644 --- a/t/plugin/limit-conn2.t +++ b/t/plugin/limit-conn2.t @@ -251,4 +251,3 @@ qr/request latency is/ --- grep_error_log_out request latency is request latency is - From bdedb6cbbd0b0ec3fddafa33f644a0c29e91c06f Mon Sep 17 00:00:00 2001 From: zuiyangqingzhou Date: Thu, 22 Jul 2021 14:44:59 +0800 Subject: [PATCH 11/11] fix log phase conf param error --- apisix/plugins/limit-conn/init.lua | 7 ++++--- t/plugin/limit-conn2.t | 27 ++------------------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/apisix/plugins/limit-conn/init.lua b/apisix/plugins/limit-conn/init.lua index 103d36cffe83..16a771745f91 100644 --- a/apisix/plugins/limit-conn/init.lua +++ b/apisix/plugins/limit-conn/init.lua @@ -62,7 +62,7 @@ function _M.increase(conf, ctx) ctx.limit_conn = core.tablepool.fetch("plugin#limit-conn", 0, 6) end - core.table.insert_tail(ctx.limit_conn, lim, key, delay) + core.table.insert_tail(ctx.limit_conn, lim, key, delay, conf.only_use_default_delay) end if delay >= 0.001 then @@ -77,13 +77,14 @@ function _M.decrease(conf, ctx) return end - for i = 1, #limit_conn, 3 do + for i = 1, #limit_conn, 4 do local lim = limit_conn[i] local key = limit_conn[i + 1] local delay = limit_conn[i + 2] + local use_delay = limit_conn[i + 3] local latency - if not conf.only_use_default_delay then + if not use_delay then if ctx.proxy_passed then latency = ctx.var.upstream_response_time else diff --git a/t/plugin/limit-conn2.t b/t/plugin/limit-conn2.t index 1f4461460346..bbad64894cd4 100644 --- a/t/plugin/limit-conn2.t +++ b/t/plugin/limit-conn2.t @@ -186,28 +186,6 @@ request latency is 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, [[{ @@ -247,7 +225,6 @@ passed --- request GET /hello1 --- grep_error_log eval -qr/request latency is/ +qr/request latency is nil/ --- grep_error_log_out -request latency is -request latency is +request latency is nil