-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RateLimiter] Fix wait duration for fixed window policy #42168
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
Conversation
Hey! I think @kbond has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
Thank you. Isn't the correct behaviour to not accept that you are trying to overconsume? |
@Nyholm, thanks for looking at this PR. I think you can always call Here's a code I used to compare different policies: foreach (['fixed_window', 'sliding_window', 'token_bucket'] as $policy) {
echo "\nPOLICY: $policy\n";
$factory = new RateLimiterFactory([
'id' => 'db_write',
'policy' => $policy,
'limit' => 10,
'interval' => '2 seconds',
'rate' => ['amount' => 10,'interval' => '2 seconds'],
], new InMemoryStorage());
$limiter = $factory->create();
$limit = $limiter->consume(8);
$limit = $limiter->consume(4);
echo "REMAINING TOKENS: " . $limit->getRemainingTokens() . PHP_EOL;
echo "IS ACCEPTED: " . (int)$limit->isAccepted() . PHP_EOL;
$start = microtime(true);
$limit->wait();
echo 'WAITED: ' . 1000 * (microtime(true) - $start) . PHP_EOL;
$limit = $limiter->consume(4);
echo "REMAINING TOKENS: " . $limit->getRemainingTokens() . PHP_EOL;
echo "IS ACCEPTED: " . (int)$limit->isAccepted() . PHP_EOL;
} BTW, there might be another issue with |
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.
I finally got my head around this PR.
What do you think of changing the solution slightly? Will it still work?
src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
Outdated
Show resolved
Hide resolved
@Nyholm, thanks for having a closer look into this. Fixing the number of tokens to calculate time for works too. I made the changes according to your suggestions. If that looks fine, I will:
The reason behind removing |
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.
Im happy with this.
Please squash the commits and rebase on 5.3. Make sure that none of the test failures are related to this PR. Then I would be happy if @wouterj saw this PR too.
About removing the method.
I didn't see the class was internal, if so, you are correct. Removing a method from an internal class is not a BC break.
Thank you @Nyholm. I squashed and rebased my changes.
@wouterj, please have a look how you feel about this change. My original commit was here jlekowski@44807a6 with |
Thank you @jlekowski. |
This PR was merged into the 5.3 branch. Discussion ---------- [RateLimiter] Increase delta in limiter tests | Q | A | ------------- | --- | Branch? | 5.3 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT Tests from [PR](#42168) might randomly fail [1](https://ci.appveyor.com/project/fabpot/symfony/builds/41346868#L597) [2](https://ci.appveyor.com/project/fabpot/symfony/builds/41346183#L650) I increased delta like in [this test](https://github.com/symfony/symfony/blob/5.3/src/Symfony/Component/RateLimiter/Tests/Policy/TokenBucketLimiterTest.php#L90).   `@fabpot` `@Nyholm` please review Commits ------- 3e2ca33 [RateLimiter] Increase delta in limiter tests
When using
fixed_window
policy and calling->wait()
after overconsuming, there might be nosleep()
called. Reproducer below: