limit req - add burst and delay parameters

This commit is contained in:
bunkerity 2021-10-13 20:53:10 +02:00
parent 4c77a14825
commit bfb5319c16
No known key found for this signature in database
GPG Key ID: 3D80806F12602A7C
2 changed files with 37 additions and 26 deletions

View File

@ -33,32 +33,34 @@ function M.incr (key)
return true
end
function M.check (url, rate)
if url == "/" or url == ngx.var.request_uri then
local key = ngx.var.remote_addr .. url
local rate_split = rate:gmatch("([^/]+)")
local max = rate_split[1]
local unit = rate_split[2]
local delay = 0
if unit == "s" then
delay = 1
elseif unit == "m" then
delay = 60
elseif unit == "h" then
delay = 3600
elseif unit == "d" then
delay = 86400
end
if M.incr(key) then
local current, flags = ngx.shared.limit_req:get(key)
if M.decr(key, delay) then
if current > max then
logger.log(ngx.WARN, "REQ LIMIT", "ip " .. ngx.var.remote_addr .. " has reached the limit : " .. current .. "/" .. unit .. " (max = " .. rate .. ")")
return true
function M.check (rate, burst, sleep)
local key = ngx.var.remote_addr .. ngx.var.uri
local rate_split = rate:gmatch("([^r/]+)")
local max = rate_split[1]
local unit = rate_split[2]
local delay = 0
if unit == "s" then
delay = 1
elseif unit == "m" then
delay = 60
elseif unit == "h" then
delay = 3600
elseif unit == "d" then
delay = 86400
end
if M.incr(key) then
local current, flags = ngx.shared.limit_req:get(key)
if M.decr(key, delay) then
if current > max + burst then
logger.log(ngx.WARN, "REQ LIMIT", "ip " .. ngx.var.remote_addr .. " has reached the limit for uri " .. ngx.var.uri .. " : " .. current .. "r/" .. unit .. " (max = " .. rate .. ")")
return true
elseif current > max then
if sleep > 0 then
ngx.sleep(sleep)
end
else
ngx.shared.limit_req:set(key, current-1, 0)
end
else
ngx.shared.limit_req:set(key, current-1, 0)
end
end
return false

View File

@ -877,18 +877,27 @@
"env": "LIMIT_REQ_RATE",
"id": "limit-req-rate",
"label": "Limit req rate",
"regex": "^\\d+r/(ms|s|m|h|d)$",
"regex": "^\\d+r/(s|m|h|d)$",
"type": "text"
},
{
"context": "multisite",
"default": "2",
"default": "5",
"env": "LIMIT_REQ_BURST",
"id": "limit-req-burst",
"label": "Limit req burst",
"regex": "^\\d+$",
"type": "text"
},
{
"context": "multisite",
"default": "1",
"env": "LIMIT_REQ_DELAY",
"id": "limit-req-delay",
"label": "Limit req delay",
"regex": "^\\d+(\\.\\d+)?$",
"type": "text"
},
{
"context": "global",
"default": "10m",