Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(limit-count): supported global limit count with redis server. #624

Merged
merged 9 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ addons:
homebrew:
update: true

services:
- redis-server

cache:
directories:
- build-cache
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ install:
$(INSTALL) -d $(INST_LUADIR)/apisix/lua/apisix/plugins/grpc-transcode/
$(INSTALL) lua/apisix/plugins/grpc-transcode/*.lua $(INST_LUADIR)/apisix/lua/apisix/plugins/grpc-transcode/

$(INSTALL) -d $(INST_LUADIR)/apisix/lua/apisix/plugins/limit-count/
$(INSTALL) lua/apisix/plugins/limit-count/*.lua $(INST_LUADIR)/apisix/lua/apisix/plugins/limit-count/

$(INSTALL) -d $(INST_LUADIR)/apisix/lua/apisix/plugins
$(INSTALL) lua/apisix/plugins/*.lua $(INST_LUADIR)/apisix/lua/apisix/plugins/

Expand Down
54 changes: 46 additions & 8 deletions doc/plugins/limit-count-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
在指定的时间范围内,限制总的请求个数。并且在 HTTP 响应头中返回剩余可以请求的个数。

### 参数
* `count`:指定时间窗口内的请求数量阈值
* `time_window`:时间窗口的大小(以秒为单位),超过这个时间就会重置
* `rejected_code`:当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认是 503
* `key`:是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。

|名称 |可选项 |说明|
|--------- |--------|-----------|
|count |必选 |指定时间窗口内的请求数量阈值|
|time_window |必选 |时间窗口的大小(以秒为单位),超过这个时间就会重置|
|key |必选 |是用来做请求计数的依据,当前接受的 key 有: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"。|
|rejected_code |可选 |T当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认是 503|
|policy |可选 |用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地) 和 `redis`(计数器保存在 Redis 服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速).|
|redis.host |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。|
|redis.port |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口,默认值 6379。|
|redis.timeout |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的通讯超时时间,默认值 1000 ms(1 秒)。|

### 示例

#### 开启插件
下面是一个示例,在指定的 route 上开启了 limit count 插件:
下面是一个示例,在指定的 `route` 上开启了 `limit count` 插件:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
Expand Down Expand Up @@ -42,6 +49,37 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
然后在 route 页面中添加 limit-count 插件:
![](../images/plugin/limit-count-2.png)

如果你需要一个集群级别的流量控制,我们可以借助 redis server 来完成。不同的 APISIX 节点之间将共享流量限速结果,实现集群流量限速。

请看下面例子:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
{
"uri": "/index.html",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr",
"policy": "redis",
"redis": {
"host": "127.0.0.1",
"port": 6379,
"timeout": 1001
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"39.97.63.215:80": 1
}
}
}'
```

#### 测试插件
上述配置限制了 60 秒内只能访问 2 次,前两次访问都会正常访问:
```shell
Expand Down Expand Up @@ -76,10 +114,10 @@ Server: APISIX web server
</html>
```

这就表示 limit count 插件生效了。
这就表示 `limit count` 插件生效了。

#### 移除插件
当你想去掉 limit count 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
当你想去掉 `limit count` 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
Expand All @@ -95,4 +133,4 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
}'
```

现在就已经移除了 limit count 插件了。其他插件的开启和移除也是同样的方法。
现在就已经移除了 `limit count` 插件了。其他插件的开启和移除也是同样的方法。
56 changes: 48 additions & 8 deletions doc/plugins/limit-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@
# limit-count

### Parameters
* `count`: is the specified number of requests threshold.
* `time_window`: is the time window in seconds before the request count is reset.
* `rejected_code`: The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.
* `key`: is the user specified key to limit the rate, now accept those as key: "remote_addr"(client's IP), "server_addr"(server's IP), "X-Forwarded-For/X-Real-IP" in request header.

|name |option |description|
|--------- |--------|-----------|
|count |required|the specified number of requests threshold.|
|time_window |required|the time window in seconds before the request count is reset.|
|key |required|the user specified key to limit the rate. Here is fully key list: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for".|
|rejected_code |optional|The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.|
|policy |optional|The rate-limiting policies to use for retrieving and incrementing the limits. Available values are `local`(the counters will be stored locally in-memory on the node) and `redis`(counters are stored on a Redis server and will be shared across the nodes, usually used it to do the global speed limit).|
|redis.host |optional|When using the `redis` policy, this property specifies the address of the Redis server.|
|redis.port |optional|When using the `redis` policy, this property specifies the port of the Redis server. The default port is 6379.|
|redis.timeout |optional|When using the `redis` policy, this property specifies the timeout in milliseconds of any command submitted to the Redis server. The default timeout is 1000 ms(1 second).|


### example

#### enable plugin
Here's an example, enable the limit count plugin on the specified route:

Here's an example, enable the `limit count` plugin on the specified route:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
Expand Down Expand Up @@ -39,6 +48,37 @@ You can open dashboard with a browser: `http://127.0.0.1:9080/apisix/dashboard/`
Then add limit-count plugin:
![](../images/plugin/limit-count-2.png)

If you need a cluster-level precision traffic limit, then we can do it with the redis server. The rate limit of the traffic will be shared between different APISIX nodes to limit the rate of cluster traffic.

Here is the example:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
{
"uri": "/index.html",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr",
"policy": "redis",
"redis": {
"host": "127.0.0.1",
"port": 6379,
"timeout": 1001
membphis marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"39.97.63.215:80": 1
}
}
}'
```

#### test plugin
The above configuration limits access to only 2 times in 60 seconds. The first two visits will be normally:
```shell
Expand Down Expand Up @@ -74,10 +114,10 @@ Server: APISIX web server
</html>
```

This means that the limit count plugin is in effect.
This means that the `limit count` plugin is in effect.

#### disable plugin
When you want to disable the limit count plugin, it is very simple,
When you want to disable the `limit count` plugin, it is very simple,
you can delete the corresponding json configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:
```shell
Expand All @@ -94,4 +134,4 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
}'
```

The limit count plugin has been disabled now. It works for other plugins.
The `limit count` plugin has been disabled now. It works for other plugins.
65 changes: 59 additions & 6 deletions lua/apisix/plugins/limit-count.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,93 @@
local limit_count_new = require("resty.limit.count").new
local limit_local_new = require("resty.limit.count").new
local core = require("apisix.core")
local plugin_name = "limit-count"
local limit_redis_new
do
local redis_src = "apisix.plugins.limit-count.limit-count-redis"
limit_redis_new = require(redis_src).new
end


local schema = {
type = "object",
properties = {
count = {type = "integer", minimum = 0},
time_window = {type = "integer", minimum = 0},
key = {type = "string",
key = {
type = "string",
enum = {"remote_addr", "server_addr", "http_x_real_ip",
"http_x_forwarded_for"},
},
rejected_code = {type = "integer", minimum = 200, maximum = 600},
policy = {
type = "string",
enum = {"local", "redis"},
},
redis = {
type = "object",
properties = {
host = {
type = "string", minLength = 2
},
port = {
type = "integer", minimum = 1
},
timeout = {
type = "integer", minimum = 1
},
},
required = {"host"},
},
},
additionalProperties = false,
required = {"count", "time_window", "key", "rejected_code"},
}


local _M = {
version = 0.1,
priority = 1002, -- TODO: add a type field, may be a good idea
version = 0.2,
priority = 1002,
name = plugin_name,
schema = schema,
}


function _M.check_schema(conf)
return core.schema.check(schema, conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

if not conf.policy then
conf.policy = "local"
end

if conf.policy == "redis" then
if not conf.redis then
return false, "missing valid redis options"
end

conf.redis.port = conf.redis.port or 6379
conf.redis.timeout = conf.redis.timeout or 1000
end

return true
end


local function create_limit_obj(conf)
core.log.info("create new limit-count plugin instance")
return limit_count_new("plugin-limit-count", conf.count, conf.time_window)

if not conf.policy or conf.policy == "local" then
return limit_local_new("plugin-" .. plugin_name, conf.count,
conf.time_window)
end

if conf.policy == "redis" then
return limit_redis_new(conf.count, conf.time_window, conf.redis)
end

return nil
end


Expand Down
67 changes: 67 additions & 0 deletions lua/apisix/plugins/limit-count/limit-count-redis.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local redis_new = require("resty.redis").new
local core = require("apisix.core")


local _M = {}


local mt = {
__index = _M
}


function _M.new(limit, window, redis_conf)
assert(limit > 0 and window > 0)

local self = {limit = limit, window = window, redis = redis_conf}
return setmetatable(self, mt)
end


function _M.incoming(self, key)
local red = redis_new()
local conf = self.redis
local timeout = conf.timeout or 1000 -- 1sec
core.log.info("ttl key: ", key, " timeout: ", timeout)

red:set_timeouts(timeout, timeout, timeout)

local ok, err = red:connect(conf.host, conf.port or 6379)
if not ok then
return false, err
end

local limit = self.limit
local window = self.window
local remaining

local ret, err = red:ttl(key)
core.log.info("ttl key: ", key, " ret: ", ret, " err: ", err)
if ret < 0 then
ret, err = red:set(key, limit -1, "EX", window, "NX")
if not ret then
return nil, err
end

return 0, limit -1
end

remaining, err = red:incrby(key, -1)
if not remaining then
return nil, err
end

local ok, err = red:set_keepalive(10000, 100)
if not ok then
return nil, err
end

if remaining < 0 then
return nil, "rejected"
end

return 0, remaining
end


return _M
4 changes: 2 additions & 2 deletions t/node/merge-route.t
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ location /t {
--- request
GET /t
--- error_log eval
[qr/merge_service_route.*"time_window":60,"rejected_code":503/,
qr/merge_service_route.*"time_window":60,"rejected_code":503/]
[qr/merge_service_route.*"time_window":60,/,
qr/merge_service_route.*"time_window":60,/]
Loading