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(limit-count): ensure redis cluster name is set correctly #3910

Merged
merged 15 commits into from
Apr 7, 2021
5 changes: 4 additions & 1 deletion apisix/plugins/limit-count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ local schema = {
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
han6565 marked this conversation as resolved.
Show resolved Hide resolved
type = "string",
},
},
required = {"redis_cluster_nodes"},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion apisix/plugins/limit-count/limit-count-redis-cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ local mt = {

local function new_redis_cluster(conf)
local config = {
name = "apisix-redis-cluster",
-- can set different name for different redis cluster
name = conf.redis_cluster_name,
serv_list = {},
read_timeout = conf.redis_timeout,
auth = conf.redis_password,
Expand Down
4 changes: 3 additions & 1 deletion docs/en/latest/plugins/limit-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Limit request rate by a fixed number of requests in a given time window.
| redis_database | integer | optional | 0 | redis_database >= 0 | When using the `redis` policy, this property specifies the database you selected of the Redis server, and only for non Redis cluster mode (single instance mode or Redis public cloud service that provides single entry). |
| 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_cluster_nodes | array | optional | | | When using `redis-cluster` policy,This property is a list of addresses of Redis cluster service nodes. |
| redis_cluster_name | string | optional | | | When using `redis-cluster` policy, this property is the name of Redis cluster service nodes. |
han6565 marked this conversation as resolved.
Show resolved Hide resolved

**Key can be customized by the user, only need to modify a line of code of the plug-in to complete. It is a security consideration that is not open in the plugin.**

Expand Down Expand Up @@ -129,7 +130,8 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down
4 changes: 3 additions & 1 deletion docs/zh/latest/plugins/limit-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ title: limit-count
| redis_database | integer | 可选 | 0 | redis_database >= 0 | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点中使用的 database,并且只针对非 Redis 集群模式(单实例模式或者提供单入口的 Redis 公有云服务)生效。 |
| redis_timeout | integer | 可选 | 1000 | [1,...] | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点以毫秒为单位的超时时间 |
| redis_cluster_nodes | array | 可选 | | | 当使用 `redis-cluster` 限速策略时,该属性是 Redis 集群服务节点的地址列表。 |
| redis_cluster_name | string | 可选 | | | 当使用 `redis-cluster` 限速策略时,该属性是 Redis 集群服务节点的名称。 |
han6565 marked this conversation as resolved.
Show resolved Hide resolved

**key 是可以被用户自定义的,只需要修改插件的一行代码即可完成。并没有在插件中放开是处于安全的考虑。**

Expand Down Expand Up @@ -134,7 +135,8 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down
17 changes: 11 additions & 6 deletions t/plugin/limit-count-redis-cluster.t
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ GET /t



=== TEST 2: set route, with redis host and port
=== TEST 2: set route, with redis host and port and redis_cluster_name
--- config
location /t {
content_by_lua_block {
Expand All @@ -89,7 +89,8 @@ GET /t
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down Expand Up @@ -135,7 +136,8 @@ passed
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand All @@ -159,7 +161,8 @@ passed
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down Expand Up @@ -242,7 +245,8 @@ unlock with key route#1#redis-cluster
"127.0.0.1:8001",
"127.0.0.1:8002",
"127.0.0.1:8003"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down Expand Up @@ -329,7 +333,8 @@ code: 200
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
]
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
Expand Down