Skip to content

[RateLimiter] Fix sliding_window misbehaving with stale records #40141

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 1 commit into from
Feb 11, 2021

Conversation

xesxen
Copy link
Contributor

@xesxen xesxen commented Feb 9, 2021

Q A
Branch? 5.2
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

Currently the SlidingWindow RateLimiter returns a negative value for getHitCount if the previous SlidingWindow was too long ago. This results in a really high value from SlidingWindowLimiter::getAvailableTokens() which is higher than the configured limit.

This limits the value of percentOfCurrentTimeframe in SlidingWindow::getHitCount() to 1 so it can't result in a negative hitcount.

The 2nd fix fixes the SlidingWindow instance (essentially) not storing hits if the previous instance is way in the past, as the next instance will still be "in the past". This causes RateLimit to behave as if it were disabled until it has caught up again, which could take a long time when it is configured with a small window size.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@xesxen xesxen force-pushed the no-negative-hitcount branch 2 times, most recently from 1073a52 to 16a3f0c Compare February 10, 2021 00:30
@xesxen xesxen changed the title [RateLimiter] Fix remaining tokens for sliding_window overflowing the configured limit [RateLimiter] Fix sliding_window misbehaving with stale records Feb 10, 2021
@xesxen
Copy link
Contributor Author

xesxen commented Feb 10, 2021

Essentially only the last 2 commits are needed to fix the issue, but getHitCount() returning <0 even when it is invalid is still a bug worth fixing imho (the 1st commit).

If wanted, I can remove the 1st commit from this PR.

@fabpot fabpot force-pushed the no-negative-hitcount branch from f39d2ae to 5703316 Compare February 11, 2021 07:56
@fabpot
Copy link
Member

fabpot commented Feb 11, 2021

Thank you @xesxen.

@fabpot fabpot merged commit b636e66 into symfony:5.2 Feb 11, 2021
@fabpot fabpot mentioned this pull request Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants