Skip to content
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 5 commits into from
Apr 8, 2021

Conversation

ychdesign
Copy link
Contributor

@ychdesign ychdesign commented Apr 6, 2021

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:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@ychdesign ychdesign changed the title ensure automic operation in limit-count plugin fix:ensure automic operation in limit-count plugin Apr 6, 2021
@spacewander spacewander changed the title fix:ensure automic operation in limit-count plugin fix: ensure automic operation in limit-count plugin Apr 6, 2021
@ychdesign ychdesign marked this pull request as draft April 6, 2021 09:52
@ychdesign ychdesign marked this pull request as ready for review April 6, 2021 09:59
@ychdesign ychdesign marked this pull request as draft April 6, 2021 10:03
@ychdesign ychdesign marked this pull request as ready for review April 6, 2021 10:40
if not ret then
return nil, err
end
local remaining,err = red:eval(script,1,key,limit,window)
Copy link
Member

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.

Copy link
Contributor Author

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 "
Copy link
Member

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?

Copy link
Contributor Author

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.

@ychdesign ychdesign marked this pull request as draft April 7, 2021 03:55
@ychdesign ychdesign marked this pull request as ready for review April 7, 2021 04:35
Copy link
Contributor

@Firstsawyou Firstsawyou left a 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 "
Copy link
Contributor

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 [=[ .... ]=]

Copy link
Contributor Author

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 "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@ychdesign ychdesign marked this pull request as draft April 7, 2021 12:50
@ychdesign ychdesign marked this pull request as ready for review April 7, 2021 13:27
@spacewander spacewander merged commit 252af41 into apache:master Apr 8, 2021
@ychdesign ychdesign deleted the fix_3988 branch April 16, 2021 04:25
spacewander added a commit to spacewander/incubator-apisix that referenced this pull request Apr 19, 2021
Yiyiyimu pushed a commit that referenced this pull request Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants