limit req - add burst and delay parameters
This commit is contained in:
parent
4c77a14825
commit
bfb5319c16
@ -33,32 +33,34 @@ function M.incr (key)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.check (url, rate)
|
function M.check (rate, burst, sleep)
|
||||||
if url == "/" or url == ngx.var.request_uri then
|
local key = ngx.var.remote_addr .. ngx.var.uri
|
||||||
local key = ngx.var.remote_addr .. url
|
local rate_split = rate:gmatch("([^r/]+)")
|
||||||
local rate_split = rate:gmatch("([^/]+)")
|
local max = rate_split[1]
|
||||||
local max = rate_split[1]
|
local unit = rate_split[2]
|
||||||
local unit = rate_split[2]
|
local delay = 0
|
||||||
local delay = 0
|
if unit == "s" then
|
||||||
if unit == "s" then
|
delay = 1
|
||||||
delay = 1
|
elseif unit == "m" then
|
||||||
elseif unit == "m" then
|
delay = 60
|
||||||
delay = 60
|
elseif unit == "h" then
|
||||||
elseif unit == "h" then
|
delay = 3600
|
||||||
delay = 3600
|
elseif unit == "d" then
|
||||||
elseif unit == "d" then
|
delay = 86400
|
||||||
delay = 86400
|
end
|
||||||
end
|
if M.incr(key) then
|
||||||
if M.incr(key) then
|
local current, flags = ngx.shared.limit_req:get(key)
|
||||||
local current, flags = ngx.shared.limit_req:get(key)
|
if M.decr(key, delay) then
|
||||||
if M.decr(key, delay) then
|
if current > max + burst then
|
||||||
if current > max 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 .. ")")
|
||||||
logger.log(ngx.WARN, "REQ LIMIT", "ip " .. ngx.var.remote_addr .. " has reached the limit : " .. current .. "/" .. unit .. " (max = " .. rate .. ")")
|
return true
|
||||||
return true
|
elseif current > max then
|
||||||
|
if sleep > 0 then
|
||||||
|
ngx.sleep(sleep)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
ngx.shared.limit_req:set(key, current-1, 0)
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
ngx.shared.limit_req:set(key, current-1, 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
|
|||||||
@ -877,18 +877,27 @@
|
|||||||
"env": "LIMIT_REQ_RATE",
|
"env": "LIMIT_REQ_RATE",
|
||||||
"id": "limit-req-rate",
|
"id": "limit-req-rate",
|
||||||
"label": "Limit req rate",
|
"label": "Limit req rate",
|
||||||
"regex": "^\\d+r/(ms|s|m|h|d)$",
|
"regex": "^\\d+r/(s|m|h|d)$",
|
||||||
"type": "text"
|
"type": "text"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "multisite",
|
"context": "multisite",
|
||||||
"default": "2",
|
"default": "5",
|
||||||
"env": "LIMIT_REQ_BURST",
|
"env": "LIMIT_REQ_BURST",
|
||||||
"id": "limit-req-burst",
|
"id": "limit-req-burst",
|
||||||
"label": "Limit req burst",
|
"label": "Limit req burst",
|
||||||
"regex": "^\\d+$",
|
"regex": "^\\d+$",
|
||||||
"type": "text"
|
"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",
|
"context": "global",
|
||||||
"default": "10m",
|
"default": "10m",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user