moonming commented on a change in pull request #2340:
URL: https://github.com/apache/apisix/pull/2340#discussion_r502789923
##########
File path: apisix/plugins/limit-count.lua
##########
@@ -70,11 +74,36 @@ local schema = {
type = "string", minLength = 0,
},
redis_timeout = {
- type = "integer", minimum = 1,
- default = 1000,
+ type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
+ },
+ {
+ properties = {
+ policy = {
+ enum = {"redis-cluster"},
+ },
+ redis_serv_list = {
Review comment:
`redis_serv_list` is not a good name, how about `nodes`?
##########
File path: apisix/plugins/limit-count.lua
##########
@@ -119,6 +148,11 @@ local function create_limit_obj(conf)
conf.count, conf.time_window, conf)
end
+ if conf.policy == "redis-cluster" then
+ return limit_redis_cluster_new("plugin-" .. plugin_name,conf.count,
Review comment:
Add a space after `,`
##########
File path: doc/zh-cn/plugins/limit-count.md
##########
@@ -26,17 +26,18 @@
## 参数
-| 名称 | 类型 | 必选项 | 默认值 | 有效值
| 描述
|
-| -------------- | ------- | ------------ | ------- |
------------------------------------------------------------------------ |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
-| count | integer | 必须 | | [0,...]
| 指定时间窗口内的请求数量阈值
|
-| time_window | integer | 必须 | | [0,...]
| 时间窗口的大小(以秒为单位),超过这个时间就会重置
|
-| key | string | 必须 | | ["remote_addr",
"server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据
|
-| rejected_code | integer | 可选 | 503 | [200,600]
| 当请求超过阈值被拒绝时,返回的 HTTP 状态码
|
-| policy | string | 可选 | "local" | ["local", "redis"]
|
用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis
服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速) |
-| redis_host | string | `redis` 必须 | |
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的地址。
|
-| redis_port | integer | 可选 | 6379 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的端口
|
-| redis_password | string | 可选 | |
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的密码。
|
-| redis_timeout | integer | 可选 | 1000 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点以毫秒为单位的超时时间
|
+| 名称 | 类型 | 必选项 | 默认值 | 有效值
| 描述
|
+| --------------- | -------- | ------------ | ------- |
------------------------------------------------------------ |
------------------------------------------------------------ |
+| count | integer | 必须 | | [0,...]
| 指定时间窗口内的请求数量阈值
|
+| time_window | integer | 必须 | | [0,...]
| 时间窗口的大小(以秒为单位),超过这个时间就会重置 |
+| key | string | 必须 | | ["remote_addr",
"server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据
|
+| rejected_code | integer | 可选 | 503 | [200,600]
| 当请求超过阈值被拒绝时,返回的 HTTP 状态码
|
Review comment:
why change those lines?
##########
File path: doc/zh-cn/plugins/limit-count.md
##########
@@ -26,17 +26,18 @@
## 参数
-| 名称 | 类型 | 必选项 | 默认值 | 有效值
| 描述
|
-| -------------- | ------- | ------------ | ------- |
------------------------------------------------------------------------ |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
-| count | integer | 必须 | | [0,...]
| 指定时间窗口内的请求数量阈值
|
-| time_window | integer | 必须 | | [0,...]
| 时间窗口的大小(以秒为单位),超过这个时间就会重置
|
-| key | string | 必须 | | ["remote_addr",
"server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据
|
-| rejected_code | integer | 可选 | 503 | [200,600]
| 当请求超过阈值被拒绝时,返回的 HTTP 状态码
|
-| policy | string | 可选 | "local" | ["local", "redis"]
|
用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis
服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速) |
-| redis_host | string | `redis` 必须 | |
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的地址。
|
-| redis_port | integer | 可选 | 6379 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的端口
|
-| redis_password | string | 可选 | |
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点的密码。
|
-| redis_timeout | integer | 可选 | 1000 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点以毫秒为单位的超时时间
|
+| 名称 | 类型 | 必选项 | 默认值 | 有效值
| 描述
|
+| --------------- | -------- | ------------ | ------- |
------------------------------------------------------------ |
------------------------------------------------------------ |
+| count | integer | 必须 | | [0,...]
| 指定时间窗口内的请求数量阈值
|
+| time_window | integer | 必须 | | [0,...]
| 时间窗口的大小(以秒为单位),超过这个时间就会重置 |
+| key | string | 必须 | | ["remote_addr",
"server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据
|
+| rejected_code | integer | 可选 | 503 | [200,600]
| 当请求超过阈值被拒绝时,返回的 HTTP 状态码
|
+| policy | string | 可选 | "local" | ["local", "redis",
"redis-cluster"] |
用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis
服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速);以及`redis-cluster`,跟redis功能一样,只是使用redis集群方式。 |
+| redis_host | string | `redis` 必须 | |
| 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。 |
+| redis_port | integer | 可选 | 6379 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口 |
+| redis_password | string | 可选 | |
| 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的密码。 |
+| redis_timeout | integer | 可选 | 1000 | [1,...]
| 当使用 `redis` 限速策略时,该属性是 Redis
服务节点以毫秒为单位的超时时间 |
Review comment:
ditto
##########
File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua
##########
@@ -0,0 +1,127 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local resty_lock = require("resty.lock")
+local assert = assert
+local error = error
+local setmetatable = setmetatable
+local tostring = tostring
+local ipairs = ipairs
+
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+-- https://github.com/steve0511/resty-redis-cluster
Review comment:
why add this comment?
##########
File path: doc/plugins/limit-count.md
##########
@@ -39,11 +39,12 @@ Limit request rate by a fixed number of requests in a given
time window.
| time_window | integer | required | | [0,...]
| the time window in
seconds before the request count is reset.
|
| key | string | required | | ["remote_addr",
"server_addr", "http_x_real_ip", "http_x_forwarded_for"] | the user specified
key to limit the rate.
|
| rejected_code | integer | optional | 503 | [200,600]
| The HTTP status code
returned when the request exceeds the threshold is rejected, default 503.
|
-| policy | string | optional | "local" | ["local",
"redis"] | 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) 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). |
+| policy | string | optional | "local" | ["local",
"redis", "redis-cluster"]
| 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) 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 | string | required for `redis` | |
| When using the
`redis` policy, this property specifies the address of the Redis server.
|
| redis_port | integer | optional | 6379 | [1,...]
| When using the
`redis` policy, this property specifies the port of the Redis server.
|
| redis_password | string | optional | |
| When using the
`redis` policy, this property specifies the password of the Redis server.
|
| redis_timeout | integer | optional | 1000 | [1,...]
| When using the
`redis` policy, this property specifies the timeout in milliseconds of any
command submitted to the Redis server.
|
+| redis_serv_list | array | optional | |
| When using `redis-cluster` policy,This
property is a list of addresses of Redis cluster service nodes. |
Review comment:
be careful with English grammar
##########
File path: doc/plugins/limit-count.md
##########
@@ -109,6 +110,41 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H
'X-API-KEY: edd1c9f034335
}'
```
+If using redis-cluster:
+
+```shell
+curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "uri": "/index.html",
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis-cluster",
+ "redis_serv_list": [
+ {
Review comment:
why not {'127.0.0.1:6373', '127.0.0.1:6374'}? which is the same style as
upstream
##########
File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua
##########
@@ -0,0 +1,127 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local resty_lock = require("resty.lock")
+local assert = assert
+local error = error
+local setmetatable = setmetatable
+local tostring = tostring
+local ipairs = ipairs
+
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+-- https://github.com/steve0511/resty-redis-cluster
+local function new_redis_cluster(conf)
+ local config = {
+ name = "apisix-rediscluster",
+ serv_list = {},
+ read_timeout = conf.redis_timeout,
+ auth = conf.redis_password
+ }
+
+ for i, c in ipairs(conf.redis_serv_list) do
Review comment:
`i` `c` both not good name
##########
File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua
##########
@@ -0,0 +1,127 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local resty_lock = require("resty.lock")
+local assert = assert
+local error = error
+local setmetatable = setmetatable
+local tostring = tostring
+local ipairs = ipairs
+
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+-- https://github.com/steve0511/resty-redis-cluster
+local function new_redis_cluster(conf)
+ local config = {
+ name = "apisix-rediscluster",
+ serv_list = {},
+ read_timeout = conf.redis_timeout,
+ auth = conf.redis_password
+ }
+
+ for i, c in ipairs(conf.redis_serv_list) do
+ config.serv_list[i] = {ip = c.host, port = c.port}
+ end
+
+ local red_cli = rediscluster:new(config)
Review comment:
not return error msg?
##########
File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua
##########
@@ -0,0 +1,127 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local resty_lock = require("resty.lock")
+local assert = assert
+local error = error
+local setmetatable = setmetatable
+local tostring = tostring
+local ipairs = ipairs
+
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+-- https://github.com/steve0511/resty-redis-cluster
+local function new_redis_cluster(conf)
+ local config = {
+ name = "apisix-rediscluster",
+ serv_list = {},
+ read_timeout = conf.redis_timeout,
+ auth = conf.redis_password
+ }
+
+ for i, c in ipairs(conf.redis_serv_list) do
+ config.serv_list[i] = {ip = c.host, port = c.port}
+ end
+
+ local red_cli = rediscluster:new(config)
+ if not red_cli then
+ error("connect to redis cluster fails")
Review comment:
Please check the grammar
##########
File path: rockspec/apisix-master-0.rockspec
##########
@@ -54,6 +54,7 @@ dependencies = {
"skywalking-nginx-lua-plugin = 1.0-0",
"base64 = 1.5-2",
"dkjson = 2.5-2",
+ "resty-redis-cluster = 1.02-4",
Review comment:
the version is `1.02-4`? why so strange?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]