-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
ok. thanks
@@ -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 " |
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 of ttl
?
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.
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.
Nice, LGTM.
@@ -30,6 +29,13 @@ local mt = { | |||
} | |||
|
|||
|
|||
local script = "if redis.call('ttl', KEYS[1]) < 0 then " |
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.
What about using long string quotes.
local scripts [=[ .... ]=]
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.
Can't be better
@@ -30,6 +29,13 @@ local mt = { | |||
} | |||
|
|||
|
|||
local script = "if redis.call('ttl', KEYS[1]) < 0 then " |
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.
Ditto.
Close apache#3988 Signed-off-by: spacewander <[email protected]>
Signed-off-by: spacewander <[email protected]>
What this PR does / why we need it:
Use redis's eval() to ensure atomic operations of ttl and incrby, and solve the situation where a small number of requests are accidentally killed in high concurrency scenarios
使用redis的eval()确保ttl和incrby的原子性操作,解决高并发场景下少量请求被误杀的情况
Pre-submission checklist: