Skip to content

[RateLimiter] TokenBucket policy fix for adding tokens with a predefined frequency #44766

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
Oct 10, 2023

Conversation

relo-san
Copy link
Contributor

@relo-san relo-san commented Dec 22, 2021

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #42627
License MIT
Doc PR symfony/symfony-docs#...

Fixes bug, explained in issue #42627: when interval between requests smaller than rate interval, new tokens can not be added to bucket, because each request updates timer, and therefore rate interval never be reached.
I replace this with approach where timer updates to time, when should have been last updated, and only when rate interval reached.
I added test case for test adding tokens.
As far as I see, there is no BC and no deprecations.
There is method calculateNextTokenAvailability() on Symfony\Component\RateLimiter\Policy\Rate. It does not work correctly (does not match the name and the description), but as far as I can see, it is not used anywhere, so I left it unchanged. The Rate class simply cannot return this data, because it does not have a timer and does not know when the previous addition was made and, accordingly, what to calculate the next from.

@carsonbot
Copy link

Hey!

I think @alexandre-daubois has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@fabpot
Copy link
Member

fabpot commented Aug 14, 2022

@wouterj Can you have a look at this PR?

@fabpot fabpot requested a review from wouterj August 14, 2022 19:59
@nicolas-grekas nicolas-grekas modified the milestones: 5.3, 5.4 Feb 16, 2023
@nicolas-grekas nicolas-grekas changed the base branch from 5.3 to 5.4 February 16, 2023 08:05
@TCM-dev
Copy link

TCM-dev commented Oct 6, 2023

Are there any news about this MR being merged ? I just discovered this problem myself and tested the fix proposed, it works flawlessly on my end.

When the consumption rate is low enough, I never get throttled, and when it's too high, once everyt okens are consumed, it waits to get new tokens like it's supposed to.

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

Sorry for stalling so long, I always need some time to get into the rate limit logic again before I can properly review fixes like this.

I think this is a valid fix indeed, nice job! 👍

In 6.4 or 7.1, we can deprecate the calculateNextTokenAvailability() method. This is a leftover and completely wrong method, so we can better prevent user-land code from calling it as well.

@fabpot
Copy link
Member

fabpot commented Oct 10, 2023

Thank you @relo-san.

@fabpot fabpot merged commit adef494 into symfony:5.4 Oct 10, 2023
@fabpot
Copy link
Member

fabpot commented Oct 10, 2023

@relo-san Can you work on a PR to deprecate calculateNextTokenAvailability in 6.4?

@relo-san
Copy link
Contributor Author

@fabpot Yes, I will do that.

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.

7 participants