-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fengfei
committed
May 2, 2018
1 parent
c28a077
commit c0591f4
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
local config = require "config" | ||
local _M = {} | ||
local log = ngx.log | ||
local ERR = ngx.ERR | ||
|
||
function open_redis() | ||
local redis = require "resty.redis" | ||
local red = redis:new() | ||
red:set_timeout(1000) -- 1 sec | ||
local ok, err = red:connect(config["redis_host"], config["redis_port"]) | ||
if not ok then | ||
log(ERR, "failed to connect: ", err) | ||
end | ||
return red, err | ||
end | ||
|
||
function return_redis(red) | ||
-- put it into the connection pool of size 100, | ||
-- with 10 seconds max idle time | ||
local ok, err = red:set_keepalive(10000, 100) | ||
if not ok then | ||
log(ERR, "failed to set keepalive: ", err) | ||
return | ||
end | ||
end | ||
|
||
function _M.checkAccessLimit(uri, uriSeconds, uriTimes, ipUriSeconds, ipUriTimes) | ||
if not uriSeconds or not ipUriSeconds then | ||
return | ||
end | ||
|
||
local red, err = open_redis() | ||
if err then | ||
return | ||
end | ||
|
||
-- 针对整个接口限流 | ||
if uriSeconds then | ||
local value, _ = red:get(uri) | ||
if value then | ||
if value >= uriTimes then | ||
return_redis() | ||
ngx.exit(403) | ||
end | ||
red:incr(uri) | ||
else | ||
red:setex(uri, uriSeconds, 1) | ||
end | ||
end | ||
|
||
-- 针对IP接口限流 | ||
if ipUriSeconds then | ||
local headers = ngx.req.get_headers() | ||
local ip = headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0" | ||
local value, _ = red:get(ip .. uri) | ||
if value then | ||
if value >= ipUriTimes then | ||
return_redis() | ||
ngx.exit(403) | ||
end | ||
red:incr(ip .. uri) | ||
else | ||
red:setex(ip .. uri, ipUriSeconds, 1) | ||
end | ||
end | ||
return_redis() | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters