You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/08/05 08:15:28 UTC
[GitHub] [apisix] jagerzhang opened a new issue #4760: request help: limit-count 精细化限速需求
jagerzhang opened a new issue #4760:
URL: https://github.com/apache/apisix/issues/4760
Issue description
希望`limit-count`可以支持对不同的`route_id`或`service_id`设置不同的限速阈值。比如,对比现有版本功能的改变:
当前版本功能:只能针对某个固定`key`来限速
```
"limit-count": {
"count": 10,
"key": "service_id",
"policy": "local",
"rejected_code": 503,
"time_window": 1
}
```
如下所示,希望可以在指定`apisix`对象限速的基础上,支持更精细化的限速设置,即可以针对不同的`apisix`对象来设置不同的`count`和`time_window`:
```
"limit-count": {
"by": "service_id", # 选择需要限速的类型,one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"],希望还可以新增一个route_id
"policy": "local",
"rejected_code": 503,
"limit_config": { # 针对不同的apisix对象设置不同的限速阈值
"service_A": {
"time_window": 1,
"count": 10
},
"service_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
插件文档:https://apisix.apache.org/docs/apisix/plugins/limit-count/
Environment
基于官方最新docker镜像部署。
apisix version (cmd: apisix version): 2.8
OS (cmd: uname -a): centos7
OpenResty / Nginx version (cmd: nginx -V or openresty -V): openresty/1.19.3.2
etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API): 3.3.4
apisix-dashboard version, if have: 2.7
luarocks version, if the issue is about installation (cmd: luarocks --version): null
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code like:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name", "service_id"},
default = "remote_addr",
},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 503
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
There are two feature requirements:
- can limit by route_id
- can set diffent limit count for diffent service, then diffent consumer has diffent count limit
```
# limit-count plugin in Consumers
"limit-count": {
# route_id is required --> feature1
"by": "route_Id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent route_id, then diffent consumer has diffent count limit --> feature2
"limit_config": {
"route_id_A": {
"time_window": 1,
"count": 10
},
"route_id_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894006788
> The limit key can be customized by the user, so can use `key = consumer_name + route_id` to solve your problem?
I can't find a way to customize the key from this document: https://apisix.apache.org/docs/apisix/plugins/limit-count
Can you provide relevant guidance documents, tks.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
```
# limit-count plugin in Consumers
"limit-count": {
# route_id is required --> feature1
"by": "service_id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent service, then diffent consumer has diffent count limit --> feature2
"limit_config": {
"service_A": {
"time_window": 1,
"count": 10
},
"service_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893348886
Why not configure `limit-count` in route level?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894058539
What about relaxing the check and allowing vars combinations? @membphis
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
There are two feature requirements:
- can limit by route_id
- can set diffent limit count for diffent route_id or other type, then diffent consumer has diffent count limit
```
# limit-count plugin in Consumers
"limit-count": {
# route_id is required --> feature1
"by": "route_Id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent route_id, then diffent consumer has diffent count limit --> feature2
"limit_config": {
"route_id_A": {
"time_window": 1,
"count": 10
},
"route_id_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
if conf.map[req_key] ~= nil then
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
else
local item_count = conf.default_count
local item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set diffent limit count/time_window for the diffent client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tokers commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894782470
Since the configurations are unique for consumers, why not just configuring them on consumers? IMHO, the `map` on the plugin configuration snippet is not so smooth for me.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893935300
> Why not configure `limit-count` in route level?
因为我们想从消费者这边给不同的路由设置不同的限速。
Because we want to set different speed limit values from the consumer side according to different road origins.
比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by add a new plugin base on plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
if conf.map[req_key] ~= nil then
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
else
local item_count = conf.default_count
local item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set diffent limit count/time_window for the diffent client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
membphis commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894850971
> What about relaxing the check and allowing vars combinations? @membphis
LGTM
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by add a new plugin base on plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = 0
local item_time_window = 0
if conf.map[req_key] ~= nil then
item_count = conf.map[req_key].count
item_time_window = conf.map[req_key].time_window
else
item_count = conf.default_count
item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
local item_count = 0
local item_time_window = 0
if conf.map[req_key] ~= nil then
item_count = conf.map[req_key].count
else
item_count = conf.default_count
end
core.response.set_header("X-RateLimit-Limit", item_count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set different limit count/time_window for the different client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name", "service_id"},
default = "remote_addr",
},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 503
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
```
# limit-count plugin in consumer
"limit-count": {
# route_id is required
"by": "service_id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent service, then diffent consumer has diffent count limit
"limit_config": {
"service_A": {
"time_window": 1,
"count": 10
},
"service_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893348886
Why not configure `limit-count` in route level?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
```
# limit-count plugin in Consumers
"limit-count": {
# route_id is required
"by": "service_id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent service, then diffent consumer has diffent count limit
"limit_config": {
"service_A": {
"time_window": 1,
"count": 10
},
"service_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tzssangglass commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894021273
route id -> `ctx.route_id`
consumer_name -> `ctx.consumer_name`
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tzssangglass commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894012059
You can modify the source code directly.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894910830
> Since the configurations are unique for consumers, why not just configuring them on consumers? IMHO, the `map` on the plugin configuration snippet is not so smooth for me.
it can not set `different count for different route/service` when the limit plugin configuring on consumer.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893935300
> Why not configure `limit-count` in route level?
因为我们想从消费者这边给不同的路由设置不同的限速。
Because we want to set different speed limit values from the consumer side according to different road origins.
比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
if conf.map[req_key] ~= nil then
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
else
local item_count = conf.default_count
local item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894800244
>
>
> > What about relaxing the check and allowing vars combinations? @membphis
>
> Sounds good, but I'm worried that incorrect use will lead to accidents. I think we have to figure out the range of variables that can be combined first.
We can fall back to default if the variable eval to "". It is the solution used in upstream's chash.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander edited a comment on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
spacewander edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894936428
What about splitting them into multiple consumers?
Allow route level configuration in a consumer makes thing very complex...
~~I prefer the idea of consumer group:~~ https://github.com/apache/apisix/issues/1548. (It doesn't work)
Anyway, it requires some top-level design and careful discussion.
Would you start the discussion in the mail list?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tokers commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893944676
> > Why not configure `limit-count` in route level?
>
> 因为我们想从消费者这边给不同的路由设置不同的限速。
> Because we want to set different speed limit values from the consumer side according to different road origins.
> 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894936428
What about splitting them into multiple consumers?
Allow route level configuration in a consumer makes thing very complex...
I prefer the idea of consumer group: https://github.com/apache/apisix/issues/1548.
Anyway, it requires some top-level design and careful discussion.
Would you start the discussion in the mail list?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important...
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
membphis commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894851119
@jagerzhang please change the issue title to English
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tzssangglass commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894275929
> What about relaxing the check and allowing vars combinations? @membphis
Sounds good, but I'm worried that incorrect use will lead to accidents. I think we have to figure out the range of variables that can be combined first.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by add a new plugin base on plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = 0
local item_time_window = 0
if conf.map[req_key] ~= nil then
item_count = conf.map[req_key].count
item_time_window = conf.map[req_key].time_window
else
item_count = conf.default_count
item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set diffent limit count/time_window for the diffent client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894022172
> You can modify the source code directly.
>route id -> ctx.route_id
>consumer_name -> ctx.consumer_name
got it
It must be possible to change it src code by ourself, but it's best way to support it with the official....
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
if conf.map[req_key] ~= nil then
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
else
local item_count = conf.default_count
local item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set diffent limit count/time_window for the diffent client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tzssangglass commented on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894003217
The limit key can be customized by the user, so can use `key = consumer_name + route_id` to solve your problem?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-893996503
> > > Why not configure `limit-count` in route level?
> >
> >
> > 因为我们想从消费者这边给不同的路由设置不同的限速。
> > Because we want to set different speed limit values from the consumer side according to different road origins.
> > 比如,A消费者去申请不同的API的时候,需要遵循API发布方的能力限制,会要求给予不同的速率限制。
> > For example, when a consumer applies for different APIs, it needs to comply with the capability limits of the API publisher and will require different rate limits.
>
> It works well if configuring the `limit-count` in the routes, as long as the limit threshold for consumers are same.
I known...
but current can not set diffent limit count for diffent consumer. however, it is important and necessary...
There are two feature requirements:
- can limit by route_id
- can set diffent limit count for diffent service, then diffent consumer has diffent count limit
```
# limit-count plugin in Consumers
"limit-count": {
# route_id is required --> feature1
"by": "service_id", # one of ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name", "service_id", "route_id"]
"policy": "local",
"rejected_code": 503,
# set diffent limit count for diffent service, then diffent consumer has diffent count limit --> feature2
"limit_config": {
"service_A": {
"time_window": 1,
"count": 10
},
"service_B": {
"time_window": 60,
"count": 300
}
# more ..
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander edited a comment on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
spacewander edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894936428
What about splitting them into multiple consumers?
Allow route level configuration in a consumer makes thing very complex...
~~I prefer the idea of consumer group: https://github.com/apache/apisix/issues/1548.~~(It doesn't work)
Anyway, it requires some top-level design and careful discussion.
Would you start the discussion in the mail list?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by add a new plugin base on plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
local item_count = 0
local item_time_window = 0
if conf.map[req_key] ~= nil then
item_count = conf.map[req_key].count
item_time_window = conf.map[req_key].time_window
else
item_count = conf.default_count
item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set different limit count/time_window for the different client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang edited a comment on issue #4760: request help: limit-count 精细化限速需求
Posted by GitBox <gi...@apache.org>.
jagerzhang edited a comment on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-894090586
we solved this case by modify the source code of plugins/limit-count.lua:
```
--
-- 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 limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count-by-client"
local limit_redis_cluster_new
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
limit_redis_cluster_new = require(cluster_src).new
end
local lrucache = core.lrucache.new({
type = 'plugin', serial_creating = true,
})
local schema = {
type = "object",
properties = {
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for", "consumer_name"},
default = "remote_addr",
},
default_count = {type = "integer", exclusiveMinimum = 0},
default_time_window = {type = "integer", exclusiveMinimum = 0},
scope = {
type = "string",
enum = {"route_id", "service_id"},
default = "route_id",
},
map = {
type = "table",
items = {
type = "object",
count = {type = "integer", exclusiveMinimum = 0},
time_window = {type = "integer", exclusiveMinimum = 0},
}
},
rejected_code = {
type = "integer", minimum = 200, maximum = 599, default = 429
},
policy = {
type = "string",
enum = {"local", "redis", "redis-cluster"},
default = "local",
}
},
dependencies = {
policy = {
oneOf = {
{
properties = {
policy = {
enum = {"local"},
},
},
},
{
properties = {
policy = {
enum = {"redis"},
},
redis_host = {
type = "string", minLength = 2
},
redis_port = {
type = "integer", minimum = 1, default = 6379,
},
redis_password = {
type = "string", minLength = 0,
},
redis_database = {
type = "integer", minimum = 0, default = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
},
required = {"redis_host"},
},
{
properties = {
policy = {
enum = {"redis-cluster"},
},
redis_cluster_nodes = {
type = "array",
minItems = 2,
items = {
type = "string", minLength = 2, maxLength = 100
},
},
redis_password = {
type = "string", minLength = 0,
},
redis_timeout = {
type = "integer", minimum = 1, default = 1000,
},
redis_cluster_name = {
type = "string",
},
},
required = {"redis_cluster_nodes", "redis_cluster_name"},
}
}
}
}
}
local _M = {
version = 0.4,
priority = 1002,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function create_limit_obj(conf, ctx)
core.log.info("create new limit-count plugin instance")
local req_key = ctx.var[conf.key]
if conf.map[req_key] ~= nil then
local item_count = conf.map[req_key].count
local item_time_window = conf.map[req_key].time_window
else
local item_count = conf.default_count
local item_time_window = conf.default_time_window
end
if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, item_count,
item_time_window)
end
if conf.policy == "redis" then
return limit_redis_new("plugin-" .. plugin_name,
item_count, item_time_window, conf)
end
if conf.policy == "redis-cluster" then
return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
item_time_window, conf)
end
return nil
end
function _M.access(conf, ctx)
core.log.info("ver: ", ctx.conf_version)
local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
if not lim then
core.log.error("failed to fetch limit.count object: ", err)
return 500
end
local req_key = ctx.var[conf.key]
local limit_key = req_key .. conf.scope
local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
core.log.info("limit key: ", key)
local delay, remaining = lim:incoming(key, true)
if not delay then
local err = remaining
if err == "rejected" then
return conf.rejected_code
end
core.log.error("failed to limit req: ", err)
return 500, {error_msg = "failed to limit count: " .. err}
end
core.response.set_header("X-RateLimit-Limit", conf.map[req_key].count,
"X-RateLimit-Remaining", remaining)
end
return _M
```
Add this plugin to a route or a service, then could set diffent limit count/time_window for the diffent client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
```
{
"uris": [
"/hello"
],
"name": "route_foo",
"desc": "demo",
"methods": [
"GET"
],
"hosts": [
"test.foo.com"
],
"plugins": {
"hmac-auth": {
"disable": false
},
"limit-count-by-client": {
"default_count": 1000, # set default count when client not in map table
"default_time_window": 60, # set default time window when client not in map table
"key": "consumer_name", # type of key
"map": {
"consumer_A": { # limit configuration for consumer A
"count": 1000,
"time_window": 60
},
"consumer_B": { # limit configuration for consumer B
"count": 500,
"time_window": 60
}
},
"policy": "local",
"rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
"scope": "route_id" # mean the plugin is add in a router
}
},
"service_id": "service_foo",
"status": 1
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang closed issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
jagerzhang closed issue #4760:
URL: https://github.com/apache/apisix/issues/4760
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] jagerzhang commented on issue #4760: request help: limit-count could set different limit count/time_window for the different client
Posted by GitBox <gi...@apache.org>.
jagerzhang commented on issue #4760:
URL: https://github.com/apache/apisix/issues/4760#issuecomment-898084784
> we solved this case by add a new plugin base on plugins/limit-count.lua:
>
> ```
> --
> -- 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 limit_local_new = require("resty.limit.count").new
> local core = require("apisix.core")
> local plugin_name = "limit-count-by-client"
> local limit_redis_cluster_new
> local limit_redis_new
> do
> local redis_src = "apisix.plugins.limit-count.limit-count-redis"
> limit_redis_new = require(redis_src).new
>
> local cluster_src = "apisix.plugins.limit-count.limit-count-redis-cluster"
> limit_redis_cluster_new = require(cluster_src).new
> end
> local lrucache = core.lrucache.new({
> type = 'plugin', serial_creating = true,
> })
>
>
> local schema = {
> type = "object",
> properties = {
> key = {
> type = "string",
> enum = {"remote_addr", "server_addr", "http_x_real_ip",
> "http_x_forwarded_for", "consumer_name"},
> default = "remote_addr",
> },
> default_count = {type = "integer", exclusiveMinimum = 0},
> default_time_window = {type = "integer", exclusiveMinimum = 0},
> scope = {
> type = "string",
> enum = {"route_id", "service_id"},
> default = "route_id",
> },
> map = {
> type = "table",
> items = {
> type = "object",
> count = {type = "integer", exclusiveMinimum = 0},
> time_window = {type = "integer", exclusiveMinimum = 0},
> }
> },
> rejected_code = {
> type = "integer", minimum = 200, maximum = 599, default = 429
> },
> policy = {
> type = "string",
> enum = {"local", "redis", "redis-cluster"},
> default = "local",
> }
> },
> dependencies = {
> policy = {
> oneOf = {
> {
> properties = {
> policy = {
> enum = {"local"},
> },
> },
> },
> {
> properties = {
> policy = {
> enum = {"redis"},
> },
> redis_host = {
> type = "string", minLength = 2
> },
> redis_port = {
> type = "integer", minimum = 1, default = 6379,
> },
> redis_password = {
> type = "string", minLength = 0,
> },
> redis_database = {
> type = "integer", minimum = 0, default = 0,
> },
> redis_timeout = {
> type = "integer", minimum = 1, default = 1000,
> },
> },
> required = {"redis_host"},
> },
> {
> properties = {
> policy = {
> enum = {"redis-cluster"},
> },
> redis_cluster_nodes = {
> type = "array",
> minItems = 2,
> items = {
> type = "string", minLength = 2, maxLength = 100
> },
> },
> redis_password = {
> type = "string", minLength = 0,
> },
> redis_timeout = {
> type = "integer", minimum = 1, default = 1000,
> },
> redis_cluster_name = {
> type = "string",
> },
> },
> required = {"redis_cluster_nodes", "redis_cluster_name"},
> }
> }
> }
> }
> }
>
>
> local _M = {
> version = 0.4,
> priority = 1002,
> name = plugin_name,
> schema = schema,
> }
>
>
> function _M.check_schema(conf)
> local ok, err = core.schema.check(schema, conf)
> if not ok then
> return false, err
> end
>
> return true
> end
>
>
> local function create_limit_obj(conf, ctx)
> core.log.info("create new limit-count plugin instance")
>
> local req_key = ctx.var[conf.key]
> local item_count = 0
> local item_time_window = 0
> if conf.map[req_key] ~= nil then
> item_count = conf.map[req_key].count
> item_time_window = conf.map[req_key].time_window
> else
> item_count = conf.default_count
> item_time_window = conf.default_time_window
>
> end
>
> if not conf.policy or conf.policy == "local" then
> return limit_local_new("plugin-" .. plugin_name, item_count,
> item_time_window)
> end
>
> if conf.policy == "redis" then
> return limit_redis_new("plugin-" .. plugin_name,
> item_count, item_time_window, conf)
> end
>
> if conf.policy == "redis-cluster" then
> return limit_redis_cluster_new("plugin-" .. plugin_name, item_count,
> item_time_window, conf)
> end
>
> return nil
> end
>
>
> function _M.access(conf, ctx)
> core.log.info("ver: ", ctx.conf_version)
> local lim, err = core.lrucache.plugin_ctx(lrucache, ctx, conf.policy, create_limit_obj, conf, ctx)
> if not lim then
> core.log.error("failed to fetch limit.count object: ", err)
> return 500
> end
> local req_key = ctx.var[conf.key]
> local limit_key = req_key .. conf.scope
> local key = (limit_key or "") .. ctx.conf_type .. ctx.conf_version
> core.log.info("limit key: ", key)
>
> local delay, remaining = lim:incoming(key, true)
> if not delay then
> local err = remaining
> if err == "rejected" then
> return conf.rejected_code
> end
>
> core.log.error("failed to limit req: ", err)
> return 500, {error_msg = "failed to limit count: " .. err}
> end
> local item_count = 0
> local item_time_window = 0
> if conf.map[req_key] ~= nil then
> item_count = conf.map[req_key].count
> else
> item_count = conf.default_count
>
> end
> core.response.set_header("X-RateLimit-Limit", item_count,
> "X-RateLimit-Remaining", remaining)
> end
>
>
> return _M
> ```
>
> Add this plugin to a route or a service, then could set different limit count/time_window for the different client(support "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name") :
>
> ```
> {
> "uris": [
> "/hello"
> ],
> "name": "route_foo",
> "desc": "demo",
> "methods": [
> "GET"
> ],
> "hosts": [
> "test.foo.com"
> ],
> "plugins": {
> "hmac-auth": {
> "disable": false
> },
> "limit-count-by-client": {
> "default_count": 1000, # set default count when client not in map table
> "default_time_window": 60, # set default time window when client not in map table
> "key": "consumer_name", # type of key
> "map": {
> "consumer_A": { # limit configuration for consumer A
> "count": 1000,
> "time_window": 60
> },
> "consumer_B": { # limit configuration for consumer B
> "count": 500,
> "time_window": 60
> }
> },
> "policy": "local",
> "rejected_code": 429, # HTTP_CODE 429(Too Many Requests) is more explicit meaning then 503
> "scope": "route_id" # mean the plugin is add in a router
> }
> },
> "service_id": "service_foo",
> "status": 1
> }
> ```
> What about splitting them into multiple consumers?
> Allow route level configuration in a consumer makes thing very complex...
>
> ~I prefer the idea of consumer group:~ #1548. (It doesn't work)
>
> Anyway, it requires some top-level design and careful discussion.
> Would you start the discussion in the mail list?
Can you review our modifications? Although this plug-in can only be added to service or route, it can really meet this requirements.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org