Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Simple limit #13

Merged
merged 4 commits into from Jan 9, 2018
Merged

Simple limit #13

merged 4 commits into from Jan 9, 2018

Conversation

jimpo
Copy link
Contributor

@jimpo jimpo commented Dec 14, 2017

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

@coveralls
Copy link

coveralls commented Dec 14, 2017

Coverage Status

Coverage decreased (-8.7%) to 90.873% when pulling faf1137 on simple-limit into 365ff23 on master.

@coveralls
Copy link

coveralls commented Dec 14, 2017

Coverage Status

Coverage decreased (-1.5%) to 98.016% when pulling 03021a9 on simple-limit into 365ff23 on master.

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

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.

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

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.

Copy link
Contributor Author

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.

@coveralls
Copy link

coveralls commented Dec 19, 2017

Coverage Status

Coverage decreased (-1.5%) to 98.031% when pulling f0f0d45 on simple-limit into 7150556 on master.

Copy link
Contributor

@ashleym1972 ashleym1972 left a comment

Choose a reason for hiding this comment

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

Love it.

@jimpo jimpo changed the title [WIP] Simple limit Simple limit Jan 5, 2018
@coveralls
Copy link

coveralls commented Jan 9, 2018

Coverage Status

Coverage decreased (-1.5%) to 98.031% when pulling 68b377d on simple-limit into 2cf2a45 on master.

@jimpo jimpo merged commit 3f50b82 into master Jan 9, 2018
@jimpo jimpo deleted the simple-limit branch January 9, 2018 18:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants