-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ensure automic operation in limit-count plugin #3991
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
|
||
local rediscluster = require("resty.rediscluster") | ||
local core = require("apisix.core") | ||
local resty_lock = require("resty.lock") | ||
local setmetatable = setmetatable | ||
local tostring = tostring | ||
local ipairs = ipairs | ||
|
@@ -72,62 +71,28 @@ function _M.new(plugin_name, limit, window, conf) | |
return setmetatable(self, mt) | ||
end | ||
|
||
local script = "if redis.call('ttl',KEYS[1]) < 0 then " | ||
.. "redis.call('set',KEYS[1],ARGV[1]-1,'EX',ARGV[2]) " | ||
.. "return ARGV[1]-1 " | ||
.. "end " | ||
.. "return redis.call('incrby',KEYS[1],-1)" | ||
|
||
function _M.incoming(self, key) | ||
local red = self.red_cli | ||
local limit = self.limit | ||
local window = self.window | ||
local remaining | ||
key = self.plugin_name .. tostring(key) | ||
|
||
local ret, err = red:ttl(key) | ||
if not ret then | ||
return false, "failed to get redis `" .. key .."` ttl: " .. err | ||
end | ||
|
||
core.log.info("ttl key: ", key, " ret: ", ret, " err: ", err) | ||
if ret < 0 then | ||
local lock, err = resty_lock:new("plugin-limit-count") | ||
if not lock then | ||
return false, "failed to create lock: " .. err | ||
end | ||
|
||
local elapsed, err = lock:lock(key) | ||
if not elapsed then | ||
return false, "failed to acquire the lock: " .. err | ||
end | ||
|
||
ret = red:ttl(key) | ||
if ret < 0 then | ||
local ok, err = lock:unlock() | ||
if not ok then | ||
return false, "failed to unlock: " .. err | ||
end | ||
|
||
ret, err = red:set(key, limit -1, "EX", window) | ||
if not ret then | ||
return nil, err | ||
end | ||
local remaining,err = red:eval(script,1,key,limit,window) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a space around the operator. Please fix other similar places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. thanks |
||
|
||
return 0, limit -1 | ||
end | ||
|
||
local ok, err = lock:unlock() | ||
if not ok then | ||
return false, "failed to unlock: " .. err | ||
end | ||
if err then | ||
return nil,err | ||
end | ||
|
||
remaining, err = red:incrby(key, -1) | ||
if not remaining then | ||
return nil, err | ||
if remaining <0 then | ||
return nil,"rejected" | ||
end | ||
|
||
if remaining < 0 then | ||
return nil, "rejected" | ||
end | ||
|
||
return 0, remaining | ||
return 0,remaining | ||
end | ||
|
||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use
exists
instead ofttl
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ttl is better. ttl will set the expire time on the key which is permanent. Both are O(1) Time complexity.