Fix bug that allows check_rate to succeed 2x in sequence even if the bucket limit is 1 attempt per hour #99
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.
Problem
When
check_rate
is called for the first time, it's expected thatms_to_next_bucket
would be qual toscale_ms
, but that's not the case at the moment due to:Utils.stamp_key/2
is currently creating a variable bucket key for the same ID andscale_ms
. The bucket key changes everyscale_ms
milliseconds.scale_ms
, instead they rely that a new key is provided (1)This bug allows check_rate to be called 2x in sequence even if the bucket limit is 1 attempt per hour. Both calls could receive
:allow
if timed when the bucket is about to expire (see how to reproduce the bug below).How to reproduce the bug
Proposed solution
This PR fixes this issue by:
Utils.stamp_key/2
for the same bucket ID andscale_ms
scale_ms
. I will open a PR onhammer-backend-redis
if this PR is accepted: Expire bucket after scale_ms nash-io/hammer-backend-redis#1