lua-resty-redis-ratelimit

限制多个由 Redis 支持的 NGINX 实例之间的请求处理速率

$ opm get timebug/lua-resty-redis-ratelimit

名称

[!构建状态](https://travis-ci.org/timebug/lua-resty-redis-ratelimit)

lua-resty-redis-ratelimit - 限制多个由 Redis 支持的 NGINX 实例之间的请求处理速率。

状态

准备测试。在大多数情况下可能已准备好投入生产,尽管尚未在实际环境中得到验证。请查看问题列表,如果您有任何问题/疑问,请告知我。

描述

此 lua 库是 ngx_lua 的请求处理速率限制模块

http://wiki.nginx.org/HttpLuaModule

它用于限制多个 NGINX 实例之间根据定义的键的请求处理速率。限制是使用“漏桶”方法完成的。

!

此模块使用 Redis (>= 2.6.0) 作为后端存储,因此您还需要 lua-resty-redis 库才能与之配合使用。

注意:如果您不使用duration功能并且传入流量均匀分布,建议使用模块resty.limit.req以避免不必要的网络延迟。

概要

``lua lua_package_path "/path/to/lua-resty-redis-ratelimit/lib/?.lua;;";

server {

    listen 9090;

    location /t {
        access_by_lua_block {
            local ratelimit = require "resty.redis.ratelimit"

            local lim, err = ratelimit.new("one", "2r/s", 0, 2)
            if not lim then
                ngx.log(ngx.ERR,
                        "failed to instantiate a resty.redis.ratelimit object: ", err)
                return ngx.exit(500)
            end

            -- NOTICE: the following call must be per-request.

            -- local redis = require "resty.redis"
            -- local red = redis:new()

            -- red:set_timeout(1000)

            -- local ok, err = red:connect("127.0.0.1", 6379)
            -- if not ok then
            --     ngx.log(ngx.ERR, "failed to connect redis: ", err)
            --     return ngx.exit(500)
            -- end

            local red = { host = "127.0.0.1", port = 6379, timeout = 1 }

            local key = ngx.var.binary_remote_addr
            local delay, err = lim:incoming(key, red)
            if not delay then
                if err == "rejected" then
                    return ngx.exit(503)
                end
                ngx.log(ngx.ERR, "failed to limit req: ", err)
                return ngx.exit(500)
            end

            if delay >= 0.001 then
                -- the 2nd return value holds the number of excess requests
                -- per second for the specified key.
                local excess = err

                ngx.sleep(delay)
            end
        '}

        echo Logged in;
    }

} ``

方法

new

语法:obj, err = class.new(zone, rate, burst, duration)

实例化此类的对象。类值由调用 require resty.redis.ratelimit 返回。

此方法采用以下参数

  • zone:设置命名空间,特别是,我们使用<zone>:<key>字符串作为 Redis 中的唯一状态标识符。

  • rate:速率以每秒请求数 (r/s) 指定。如果需要每秒少于一个请求的速率,则以每分钟请求数 (r/m) 指定。例如,每秒半请求为 30r/m。

  • burst:定义可以超出 zone 指定速率的请求数,默认为 0。

  • duration:恢复到正常状态的时间延迟(以秒为单位),在此期间,请求始终被拒绝,默认为 0。

失败时,此方法返回 nil 和一个描述错误的字符串。

incoming

语法:delay, err = obj:incoming(key, redis)

触发新的请求传入事件并计算当前请求对指定键所需的延迟(如果有)或用户是否应立即拒绝它。

此方法接受以下参数

  • key:键是指定变量的任何非空值。

  • redis:设置 Redis 配置,hostporttimeout 等(见下文);您可以直接设置已连接的 Redis 对象,而不是特定的 Redis 配置。

    - redis.host: Default 127.0.0.1.
    - redis.port: Default 80.
    - redis.timeout: Default 1s.
    - redis.pass: Request for authentication in a password-protected Redis server.
    - redis.dbid: Select the Redis logical database.

返回值取决于以下情况

  1. 如果请求未超过 new 方法中指定的rate值,则此方法返回0作为延迟以及当前时间超出的请求数(为零)。

  2. 如果请求超过 new 方法中指定的rate限制,但未超过rate + burst值,则此方法返回当前请求的适当延迟(以秒为单位),以便它仍然符合rate阈值,就像它稍后而不是现在到来一样。第二个返回值指示此时超出的请求数(包括当前请求)。

  3. 如果请求超过rate + burst限制,则此方法返回nil和错误字符串"rejected"

  4. 如果发生错误,则此方法返回nil和一个描述错误的字符串。例如"failed to create redis - connection refused"

此方法本身从不休眠。它仅在必要时返回延迟,并要求调用者稍后调用 ngx.sleep 方法进行休眠。

set_burst

语法:obj:set_burst(burst)

覆盖 new 方法中指定的burst阈值。

作者

Monkey Zhang <timebug.info@gmail.com>,又拍云

灵感来自 https://nginx.ac.cn/en/docs/http/ngx_http_limit_req_module.html。

版权和许可

此模块根据 2 条款 BSD 许可证许可。

版权所有 (c) 2014 - 2017,Monkey Zhang <timebug.info@gmail.com>,又拍云

保留所有权利。

在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用,无论是否修改。

  • 源代码的再分发必须保留上述版权声明、此条件列表和以下免责声明。

  • 二进制形式的再分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权所有者和贡献者“按原样”提供,并且不提供任何明示或暗示的保证,包括但不限于适销性和适用于特定目的的暗示保证。在任何情况下,版权所有者或贡献者均不对任何直接、间接、附带、特殊、惩戒性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润损失;或业务中断)负责,无论这些损害是基于任何责任理论引起的,无论是合同、严格责任或侵权行为(包括疏忽或其他原因),即使已被告知此类损害的可能性。

另请参阅

  • 使用 NGINX 进行速率限制:https://www.nginx.com/blog/rate-limiting-nginx/

  • ngx_lua 模块:https://github.com/openresty/lua-nginx-module

  • OpenResty:https://openresty.org.cn/

作者

Monkey Zhang timebug.info@gmail.com

许可证

2bsd

依赖项

版本