forked from go-redis/redis_rate
-
Notifications
You must be signed in to change notification settings - Fork 1
Allow heartbeating a request #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- Heartbeat to extend the expiration time for a request ID. | ||
local rate_limit_key = KEYS[1] | ||
local request_id = ARGV[1] | ||
local max_request_time_seconds = tonumber(ARGV[2]) | ||
|
||
-- redis returns time as an array containing two integers: seconds of the epoch | ||
-- time (10 digits) and microseconds (6 digits). for convenience we need to | ||
-- convert them to a floating point number. the resulting number is 16 digits, | ||
-- bordering on the limits of a 64-bit double-precision floating point number. | ||
-- adjust the epoch to be relative to Jan 1, 2017 00:00:00 GMT to avoid floating | ||
-- point problems. this approach is good until "now" is 2,483,228,799 (Wed, 09 | ||
-- Sep 2048 01:46:39 GMT), when the adjusted value is 16 digits. | ||
local jan_1_2017 = 1483228800 | ||
local now = redis.call("TIME") | ||
now = (now[1] - jan_1_2017) + (now[2] / 1000000) | ||
|
||
-- Check if the request ID exists in the hash | ||
local exists = redis.call("HEXISTS", rate_limit_key, request_id) | ||
if exists == 0 then | ||
return {0, 0} -- Request ID not found | ||
end | ||
|
||
-- Update the expiration time for the request ID | ||
redis.call("HSET", rate_limit_key, request_id, now + max_request_time_seconds) | ||
redis.call("EXPIRE", rate_limit_key, 5 * max_request_time_seconds) | ||
|
||
-- Get the current count of active requests | ||
local count = 0 | ||
local bulk = redis.call('HGETALL', rate_limit_key) | ||
local nextkey | ||
for i, v in ipairs(bulk) do | ||
if i % 2 == 1 then | ||
nextkey = v | ||
else | ||
if tonumber(v) >= now then | ||
count = count + 1 | ||
end | ||
end | ||
end | ||
|
||
return {1, count} -- Success, return the current count |
This file contains hidden or 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
Oops, something went wrong.
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.
This is O(N) on the number of requests for a key - is that OK?
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's not what I'd love to do but the N is going to be small and it's the same implementation as the way we calculate concurrency for
take
redis_rate/script_concurrency_take.lua
Lines 22 to 32 in 94b3aa6