-
Notifications
You must be signed in to change notification settings - Fork 157
new module resty.limit.count #17
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
Conversation
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.
The slip-up for struct name, and some other suggestions.
-- TODO: we could avoid the tricky FFI cdata when lua_shared_dict supports | ||
-- hash-typed values as in redis. | ||
ffi.cdef[[ | ||
struct lua_resty_limit_ghe_req_rec { |
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 to be: struct lua_resty_limit_count_rec {
return nil, "shdict abused by other users" | ||
end | ||
local rec = ffi_cast(const_rec_ptr_type, v) | ||
local ttl = tonumber(rec.reset) - now |
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.
For tonumber(rec.reset) show 2 times, could you change to current way:
`
local rec = ffi_cast(const_rec_ptr_type, v)
reset = tonumber(rec.reset)
local ttl = reset - now
-- print("ttl: ", ttl, "s")
if ttl > 0 then
remaining = tonumber(rec.remaining) - 1
else
reset = now + window
remaining = limit
end
`
remaining = tonumber(rec.remaining) - 1 | ||
else | ||
reset = now + window | ||
remaining = limit |
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.
It should be: remaining = limit -1
Because initial remaining will triger by user access, so current access will using 1 access. So that need to be: limit -1
@tangjn Thank you for your review. Also need some test cases in the existing test suite to cover this new module. |
local setmetatable = setmetatable | ||
local ffi_cast = ffi.cast | ||
local ffi_str = ffi.string | ||
local abs = math.abs |
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.
not used variable
local rec = ffi_cast(const_rec_ptr_type, v) | ||
local remaining = tonumber(rec.remaining) + 1 | ||
|
||
rec_cdata.remaining = min(remaining, limit) |
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.
should be math.min
.
local limit_count = require "resty.limit.count" | ||
|
||
-- rate: 5000 requests per 3600s | ||
local lim, err = limit_count.new("api_limit_count_store", 5000, 3600) |
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.
the name of shared dict should be my_limit_count_store
.
#21 supersedes this and has already merged into master. Closing this. Thanks! |
fixes #16