From d9170398c6eb50ac05c2b823b52ea46924e07907 Mon Sep 17 00:00:00 2001 From: "Katlinsky, Ilya" Date: Thu, 7 Dec 2023 08:15:48 +0100 Subject: [PATCH 1/4] feat: set minLength of redis_cluster_nodes to 1 --- apisix/plugins/limit-count/init.lua | 2 +- docs/en/latest/plugins/limit-count.md | 2 +- t/plugin/limit-count-redis-cluster.t | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/apisix/plugins/limit-count/init.lua b/apisix/plugins/limit-count/init.lua index a280592b90f5..22c1a7579c2d 100644 --- a/apisix/plugins/limit-count/init.lua +++ b/apisix/plugins/limit-count/init.lua @@ -76,7 +76,7 @@ local policy_to_additional_properties = { type = "array", minItems = 2, items = { - type = "string", minLength = 2, maxLength = 100 + type = "string", minLength = 1, maxLength = 100 }, }, redis_password = { diff --git a/docs/en/latest/plugins/limit-count.md b/docs/en/latest/plugins/limit-count.md index 46a775a00226..241d7a102339 100644 --- a/docs/en/latest/plugins/limit-count.md +++ b/docs/en/latest/plugins/limit-count.md @@ -52,7 +52,7 @@ The `limit-count` Plugin limits the number of requests to your service by a give | redis_ssl_verify | boolean | False | false | | If set to `true`, then verifies the validity of the server SSL certificate. Used when the `policy` attribute is set to `redis`. See [tcpsock:sslhandshake](https://github.com/openresty/lua-nginx-module#tcpsocksslhandshake). | | redis_database | integer | False | 0 | redis_database >= 0 | Selected database of the Redis server (for single instance operation or when using Redis cloud with a single entrypoint). Used when the `policy` attribute is set to `redis`. | | redis_timeout | integer | False | 1000 | [1,...] | Timeout in milliseconds for any command submitted to the Redis server. Used when the `policy` attribute is set to `redis` or `redis-cluster`. | -| redis_cluster_nodes | array | required when `policy` is `redis-cluster` | | | Addresses of Redis cluster nodes. Used when the `policy` attribute is set to `redis-cluster`. | +| redis_cluster_nodes | array | required when `policy` is `redis-cluster` | | from 1 to 100 nodes | Addresses of Redis cluster nodes. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_name | string | required when `policy` is `redis-cluster` | | | Name of the Redis cluster service nodes. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_ssl | boolean | False | false | | If set to `true`, then uses SSL to connect to redis-cluster. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_ssl_verify | boolean | False | false | | If set to `true`, then verifies the validity of the server SSL certificate. Used when the `policy` attribute is set to `redis-cluster`. | diff --git a/t/plugin/limit-count-redis-cluster.t b/t/plugin/limit-count-redis-cluster.t index 7a4798a60e48..4197ae874f76 100644 --- a/t/plugin/limit-count-redis-cluster.t +++ b/t/plugin/limit-count-redis-cluster.t @@ -95,8 +95,7 @@ __DATA__ "policy": "redis-cluster", "redis_timeout": 1001, "redis_cluster_nodes": [ - "127.0.0.1:5000", - "127.0.0.1:5001" + "127.0.0.1:5000" ], "redis_cluster_name": "redis-cluster-1" } From c09a993c5aa5e7815367c71d4fbf973e94befe28 Mon Sep 17 00:00:00 2001 From: "Katlinsky, Ilya" Date: Thu, 7 Dec 2023 08:53:41 +0100 Subject: [PATCH 2/4] feat: bump version of the limit-count plugin to 0.5 --- apisix/plugins/limit-count.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/limit-count.lua b/apisix/plugins/limit-count.lua index 72ab574df775..3e2443d0eade 100644 --- a/apisix/plugins/limit-count.lua +++ b/apisix/plugins/limit-count.lua @@ -18,7 +18,7 @@ local limit_count = require("apisix.plugins.limit-count.init") local plugin_name = "limit-count" local _M = { - version = 0.4, + version = 0.5, priority = 1002, name = plugin_name, schema = limit_count.schema, From a59bfebf9a3345ff837ad324bf71a923bd765d93 Mon Sep 17 00:00:00 2001 From: "Katlinsky, Ilya" Date: Thu, 7 Dec 2023 13:13:07 +0100 Subject: [PATCH 3/4] feat(tests): add separate test to cover changes in schema of limit-count plugin --- t/plugin/limit-count-redis-cluster.t | 3 +- t/plugin/limit-count-redis-cluster3.t | 51 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/t/plugin/limit-count-redis-cluster.t b/t/plugin/limit-count-redis-cluster.t index 4197ae874f76..7a4798a60e48 100644 --- a/t/plugin/limit-count-redis-cluster.t +++ b/t/plugin/limit-count-redis-cluster.t @@ -95,7 +95,8 @@ __DATA__ "policy": "redis-cluster", "redis_timeout": 1001, "redis_cluster_nodes": [ - "127.0.0.1:5000" + "127.0.0.1:5000", + "127.0.0.1:5001" ], "redis_cluster_name": "redis-cluster-1" } diff --git a/t/plugin/limit-count-redis-cluster3.t b/t/plugin/limit-count-redis-cluster3.t index b534c3a2c363..017c3048e675 100644 --- a/t/plugin/limit-count-redis-cluster3.t +++ b/t/plugin/limit-count-redis-cluster3.t @@ -77,3 +77,54 @@ __DATA__ } --- response_body remaining: 1 + + + +=== TEST 2: set route, with single node in redis_cluster_nodes and redis_cluster_name +--- 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, + [[{ + "uri": "/hello", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr", + "policy": "redis-cluster", + "redis_timeout": 1001, + "redis_cluster_nodes": [ + "127.0.0.1:5000" + ], + "redis_cluster_name": "redis-cluster-1" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: up the limit for single node in redis_cluster_nodes +--- pipelined_requests eval +["GET /hello", "GET /hello", "GET /hello"] +--- error_code eval +[200, 200, 503] From 2be7bc1be652e5763e198a2278b1fa086aa4f82b Mon Sep 17 00:00:00 2001 From: "Katlinsky, Ilya" Date: Thu, 7 Dec 2023 13:46:49 +0100 Subject: [PATCH 4/4] fix: apply correct changes to limit-count plugin schema --- apisix/plugins/limit-count/init.lua | 4 ++-- docs/en/latest/plugins/limit-count.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/limit-count/init.lua b/apisix/plugins/limit-count/init.lua index 22c1a7579c2d..049e238e916a 100644 --- a/apisix/plugins/limit-count/init.lua +++ b/apisix/plugins/limit-count/init.lua @@ -74,9 +74,9 @@ local policy_to_additional_properties = { properties = { redis_cluster_nodes = { type = "array", - minItems = 2, + minItems = 1, items = { - type = "string", minLength = 1, maxLength = 100 + type = "string", minLength = 2, maxLength = 100 }, }, redis_password = { diff --git a/docs/en/latest/plugins/limit-count.md b/docs/en/latest/plugins/limit-count.md index 241d7a102339..46a775a00226 100644 --- a/docs/en/latest/plugins/limit-count.md +++ b/docs/en/latest/plugins/limit-count.md @@ -52,7 +52,7 @@ The `limit-count` Plugin limits the number of requests to your service by a give | redis_ssl_verify | boolean | False | false | | If set to `true`, then verifies the validity of the server SSL certificate. Used when the `policy` attribute is set to `redis`. See [tcpsock:sslhandshake](https://github.com/openresty/lua-nginx-module#tcpsocksslhandshake). | | redis_database | integer | False | 0 | redis_database >= 0 | Selected database of the Redis server (for single instance operation or when using Redis cloud with a single entrypoint). Used when the `policy` attribute is set to `redis`. | | redis_timeout | integer | False | 1000 | [1,...] | Timeout in milliseconds for any command submitted to the Redis server. Used when the `policy` attribute is set to `redis` or `redis-cluster`. | -| redis_cluster_nodes | array | required when `policy` is `redis-cluster` | | from 1 to 100 nodes | Addresses of Redis cluster nodes. Used when the `policy` attribute is set to `redis-cluster`. | +| redis_cluster_nodes | array | required when `policy` is `redis-cluster` | | | Addresses of Redis cluster nodes. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_name | string | required when `policy` is `redis-cluster` | | | Name of the Redis cluster service nodes. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_ssl | boolean | False | false | | If set to `true`, then uses SSL to connect to redis-cluster. Used when the `policy` attribute is set to `redis-cluster`. | | redis_cluster_ssl_verify | boolean | False | false | | If set to `true`, then verifies the validity of the server SSL certificate. Used when the `policy` attribute is set to `redis-cluster`. |