Skip to content

fix: ensure automic operation in limit-count plugin#3991

Merged
spacewander merged 5 commits into
apache:masterfrom
ychdesign:fix_3988
Apr 8, 2021
Merged

fix: ensure automic operation in limit-count plugin#3991
spacewander merged 5 commits into
apache:masterfrom
ychdesign:fix_3988

Conversation

@ychdesign

@ychdesign ychdesign commented Apr 6, 2021

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok. thanks

return setmetatable(self, mt)
end

local script = "if redis.call('ttl',KEYS[1]) < 0 then "

Copy link
Copy Markdown
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
Copy Markdown
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

@Firstsawyou Firstsawyou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice, LGTM.

}


local script = "if redis.call('ttl', KEYS[1]) < 0 then "

Copy link
Copy Markdown
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
Copy Markdown
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

}


local script = "if redis.call('ttl', KEYS[1]) < 0 then "

Copy link
Copy Markdown
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