All Projects → timebug → Lua Resty Redis Ratelimit

timebug / Lua Resty Redis Ratelimit

Limit the request processing rate between multiple NGINX instances backed by Redis

Programming Languages

perl
6916 projects

Labels

Projects that are alternatives of or similar to Lua Resty Redis Ratelimit

Vagrant Elastic Stack
Giving the Elastic Stack a try in Vagrant
Stars: ✭ 131 (-5.76%)
Mutual labels:  redis
Redis
《Redis Command Reference》全文的中文翻译版。
Stars: ✭ 1,700 (+1123.02%)
Mutual labels:  redis
Sequelize Transparent Cache
Simple to use and universal cache layer for Sequelize
Stars: ✭ 137 (-1.44%)
Mutual labels:  redis
Autoloadcache
AutoLoadCache 是基于AOP+Annotation等技术实现的高效的缓存管理解决方案,实现缓存与业务逻辑的解耦,并增加异步刷新及“拿来主义机制”,以适应高并发环境下的使用。
Stars: ✭ 1,794 (+1190.65%)
Mutual labels:  redis
Hippo
💨A well crafted go packages that help you build robust, reliable, maintainable microservices.
Stars: ✭ 134 (-3.6%)
Mutual labels:  redis
Getaredis
A one click, docker based, auto scaling, Redis host implemented in Go and hosted on Digitalocean.
Stars: ✭ 135 (-2.88%)
Mutual labels:  redis
Zhttp
基于swoole的异步轻量级web框架,内部封装协程异步非阻塞全套mysql、redis、mongo、memcached连接池,可以轻松start、reload、stop,加入数据库的查询模块,框架已经封装好近乎同步写法,底层异步调用
Stars: ✭ 131 (-5.76%)
Mutual labels:  redis
E3 Springboot
SpringBoot+Docker重构宜立方商城
Stars: ✭ 139 (+0%)
Mutual labels:  redis
Nosqlmap
Automated NoSQL database enumeration and web application exploitation tool.
Stars: ✭ 1,928 (+1287.05%)
Mutual labels:  redis
Quicklogger
Library for logging on files, console, memory, email, rest, eventlog, syslog, slack, telegram, redis, logstash, elasticsearch, influxdb, graylog, Sentry, Twilio, ide debug messages and throw events for Delphi/Firemonkey/freepascal/.NET (Windows/Linux/OSX/IOS/Android).
Stars: ✭ 137 (-1.44%)
Mutual labels:  redis
Goch
Self hosted live chat server written in Go
Stars: ✭ 133 (-4.32%)
Mutual labels:  redis
Echo
🦄 开源社区系统:基于 SpringBoot + MyBatis + MySQL + Redis + Kafka + Elasticsearch + Spring Security + ... 并提供详细的开发文档和配套教程。包含帖子、评论、私信、系统通知、点赞、关注、搜索、用户设置、数据统计等模块。
Stars: ✭ 129 (-7.19%)
Mutual labels:  redis
Aioredis Py
asyncio (PEP 3156) Redis support
Stars: ✭ 2,003 (+1341.01%)
Mutual labels:  redis
Jiiiiiin Security
一个前后端分离的内管基础项目
Stars: ✭ 132 (-5.04%)
Mutual labels:  redis
Gores
👷 Redis-backed library for creating background jobs in Go. Placing jobs in multiple queues, and process them later asynchronously.
Stars: ✭ 137 (-1.44%)
Mutual labels:  redis
Scredis
Non-blocking, ultra-fast Scala Redis client built on top of Akka IO.
Stars: ✭ 131 (-5.76%)
Mutual labels:  redis
Sourcecodeofmongoredis
《左手MongoDB,右手Redis——从入门到商业实战》书籍配套源代码。
Stars: ✭ 135 (-2.88%)
Mutual labels:  redis
Enteletaor
Message Queue & Broker Injection tool
Stars: ✭ 139 (+0%)
Mutual labels:  redis
Coolstore Microservices
A full-stack .NET microservices build on Dapr and Tye
Stars: ✭ 1,903 (+1269.06%)
Mutual labels:  redis
Go Rm
Write Redis module in Golang
Stars: ✭ 136 (-2.16%)
Mutual labels:  redis

Name

Build Status

lua-resty-redis-ratelimit - Limit the request processing rate between multiple NGINX instances backed by Redis.

Table of Contents

Status

Ready for testing. Probably production ready in most cases, though not yet proven in the wild. Please check the issues list and let me know if you have any problems / questions.

Description

This lua library is a request processing rate limit module for ngx_lua:

http://wiki.nginx.org/HttpLuaModule

It is used to limit the request processing rate per a defined key between multiple NGINX instances. The limitation is done using the "leaky bucket" method.

This module use Redis (>= 2.6.0) as the backend storage, so you also need the lua-resty-redis library work with it.

NOTICE: If you do not use the duration feature and the incoming traffic is evenly distrbuted, it is recommended that use the module resty.limit.req to avoid unnecessary network delays.

Synopsis

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;
    }

}

Methods

Back to TOC

new

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

Instantiates an object of this class. The class value is returned by the call require resty.redis.ratelimit.

This method takes the following arguments:

  • zone: Sets the namespace, in particular, we use <zone>:<key> string as a unique state identifier inside Redis.
  • rate: The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.
  • burst: Defines how many requests can make in excess of the rate specified by the zone, default 0.
  • duration: The time delay (in seconds) before back to normal state, during this period, the request is always rejected, default 0.

On failure, this method returns nil and a string describing the error.

Back to TOC

incoming

syntax: delay, err = obj:incoming(key, redis)

Fires a new request incoming event and calculates the delay needed (if any) for the current request upon the specified key or whether the user should reject it immediately.

This method accepts the following arguments:

  • key: The key is any non-empty value of the specified variable.
  • redis: Sets the Redis configuration, host, port, timeout and so on (see below); Instead of the specific Redis configuration, you can also sets the connected Redis object directly.
- 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.

The return values depend on the following cases:

  1. If the request does not exceed the rate value specified in the new method, then this method returns 0 as the delay and the (zero) number of excessive requests per second at the current time.
  2. If the request exceeds the rate limit specified in the new method but not the rate + burst value, then this method returns a proper delay (in seconds) for the current request so that it still conform to the rate threshold as if it came a bit later rather than now. The 2nd return value indicating the number of excessive reqeusts per second at this point (including the current request).
  3. If the request exceeds the rate + burst limit, then this method returns nil and the error string "rejected".
  4. If an error occurred, then this method returns nil and a string describing the error. Such as "failed to create redis - connection refused".

This method never sleeps itself. It simply returns a delay if necessary and requires the caller to later invoke the ngx.sleep method to sleep.

Back to TOC

set_burst

syntax: obj:set_burst(burst)

Overwrites the burst threshold as specified in the new method.

Back to TOC

Author

Monkey Zhang [email protected], UPYUN Inc.

Inspired from http://nginx.org/en/docs/http/ngx_http_limit_req_module.html.

Back to TOC

Copyright and License

This module is licensed under the 2-clause BSD license.

Copyright (c) 2014 - 2017, Monkey Zhang [email protected], UPYUN Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

See Also

Back to TOC

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].