-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #758 from 3scale/lua-resty-limit-traffic
[resty] use forked resty.limit.count
- Loading branch information
Showing
4 changed files
with
65 additions
and
4 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
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,60 @@ | ||
-- This file applies a patch from | ||
-- https://github.com/3scale/lua-resty-limit-traffic/commit/c53f2240e953a9656580dbafcc142363ab063100 | ||
-- It can be removed when https://github.com/openresty/lua-resty-limit-traffic/pull/34 is merged and released. | ||
|
||
local _M = {} | ||
local resty_limit_count = require('resty.limit.count') | ||
local setmetatable = setmetatable | ||
|
||
local mt = { | ||
__index = _M | ||
} | ||
|
||
function _M.new(...) | ||
return setmetatable(resty_limit_count.new(...), mt) | ||
end | ||
|
||
function _M.incoming(self, key, commit) | ||
local dict = self.dict | ||
local limit = self.limit | ||
local window = self.window | ||
|
||
local count, err | ||
|
||
if commit then | ||
count, err = dict:incr(key, 1, 0, window) | ||
|
||
if not count then | ||
return nil, err | ||
end | ||
else | ||
count = (dict:get(key) or 0) + 1 | ||
end | ||
|
||
if count > limit then | ||
return nil, "rejected" | ||
end | ||
|
||
return 0, limit - count | ||
end | ||
|
||
-- uncommit remaining and return remaining value | ||
function _M.uncommit(self, key) | ||
assert(key) | ||
local dict = self.dict | ||
local limit = self.limit | ||
|
||
local count, err = dict:incr(key, -1) | ||
if not count then | ||
if err == "not found" then | ||
count = 0 | ||
else | ||
return nil, err | ||
end | ||
end | ||
|
||
return limit - count | ||
end | ||
|
||
return setmetatable(_M, { __index = resty_limit_count }) | ||
|
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