在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):timebug/lua-resty-redis-ratelimit开源软件地址(OpenSource Url):https://github.com/timebug/lua-resty-redis-ratelimit开源编程语言(OpenSource Language):Perl 71.6%开源软件介绍(OpenSource Introduction):Namelua-resty-redis-ratelimit - Limit the request processing rate between multiple NGINX instances backed by Redis. Table of ContentsStatusReady 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. DescriptionThis 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 Synopsislua_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;
}
} Methodsnewsyntax: Instantiates an object of this class. The class value is returned by the call require This method takes the following arguments:
On failure, this method returns nil and a string describing the error. incomingsyntax: 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:
The return values depend on the following cases:
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. set_burstsyntax: Overwrites the AuthorMonkey Zhang [email protected], UPYUN Inc. Inspired from http://nginx.org/en/docs/http/ngx_http_limit_req_module.html. Copyright and LicenseThis 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:
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. See Also
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论