This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
Conversation
This file contains 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
ashleym1972
reviewed
Dec 14, 2017
lib/traffic_jam/simple_limit.rb
Outdated
# would exceed the limit | ||
def increment(amount = 1, time: Time.now) | ||
return amount.zero? if max.zero? | ||
raise ArgumentError.new("Amount must be positive") if amount < 0 |
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 return here if amount is 0.
ashleym1972
reviewed
Dec 14, 2017
scripts/increment_simple.lua
Outdated
if old_value == -1 then -- key exists but has no associated expire | ||
return -1 -- -1 signals key exists but has no associated expire | ||
elseif old_value == -2 then -- key does not exist | ||
if arg_incrby > arg_max 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.
This should be in ruby code.
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.
Good point. I was trying to be defensive, but it doesn't make sense here.
ashleym1972
approved these changes
Dec 19, 2017
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.
Love it.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
A SimpleLimit is a limit type that is more efficient for increments but does not support decrements or changing the max value without a complete reset. This means that if the period or max value for an action, value key changes, the used and remaining values cannot be preserved.
This works by storing a key in Redis with a millisecond-precision expiry representing the time that the limit will be completely reset. Each increment operation converts the increment amount into the number of milliseconds to be added to the expiry.
Example: Limit is 5 per 10 seconds.
An increment by 1 first sets the key to expire in 2s.
Another immediate increment by 4 sets the expiry to 10s.
Subsequent increments fail until clock time catches up to expiry