Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:5.3fromjlekowski:fixed-policy-wait
Oct 30, 2021
Merged

[RateLimiter] Fix wait duration for fixed window policy#42168

fabpot merged 1 commit intosymfony:5.3fromjlekowski:fixed-policy-wait
Oct 30, 2021

Conversation

@jlekowski
Copy link
Contributor

@jlekowskijlekowski commentedJul 17, 2021
edited by Nyholm
Loading

QA
Branch?5.3
Bug fix?yes
New feature?no
Deprecations?no
TicketsFix #...
LicenseMIT
Doc PRsymfony/symfony-docs#...

When usingfixed_window policy and calling->wait() after overconsuming, there might be nosleep() called. Reproducer below:

<?phpuseSymfony\Component\RateLimiter\RateLimiterFactory;useSymfony\Component\RateLimiter\Storage\InMemoryStorage;require_once'./vendor/autoload.php';$factory =newRateLimiterFactory(['id' =>'some_id','policy' =>'fixed_window','limit' =>10,'interval' =>'2 seconds',],newInMemoryStorage());$limiter =$factory->create();$limit =$limiter->consume(8);$limit =$limiter->consume(4);$start =microtime(true);$limit->wait();echo'WAITED:' .1000 * (microtime(true) -$start);// WAITED: 0.0030994415283203 - instantaneous instead of WAITED: 2000.3020763397 after the fix

@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

@Nyholm
Copy link
Member

Thank you.
It is indeed a bug. But Im not sure you have the correct solution.

Isn't the correct behaviour to not accept that you are trying to overconsume?

@jlekowski
Copy link
ContributorAuthor

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->consume(), but you can check something likevar_dump($limit->getRemainingTokens(), $limit->isAccepted()); afterwards to see there are still 2 tokens remaining and that it has not been accepted.

Here's a code I used to compare different policies:

foreach (['fixed_window','sliding_window','token_bucket']as$policy) {echo"\nPOLICY:$policy\n";$factory =newRateLimiterFactory(['id' =>'db_write','policy' =>$policy,'limit' =>10,'interval' =>'2 seconds','rate' => ['amount' =>10,'interval' =>'2 seconds'],    ],newInMemoryStorage());$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 withsliding_window policy, but it's not for this ticket.

@fabpotfabpot modified the milestones:5.2,5.3Jul 26, 2021
@jlekowski
Copy link
ContributorAuthor

@Nyholm, would you have any suggestions for this PR? Could this be merged, or requires a change?

Also, could you have a look at#42194, please?

Copy link
Member

@NyholmNyholm left a 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?

@jlekowski
Copy link
ContributorAuthor

@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:

  • squash my commits
  • rebase the branch from 5.3 and point the PR to 5.3 branch (as the milestone here was set)

The reason behind removingcalculateTimeForTokens() was that the class is marked as@internal and in the current flow$window->calculateTimeForTokens($tokens); will always return the equivalent of$this->interval; (see my comment in44807a6#diff-26af5f9f51ed8bd11b4b02e6d02eef2872e174b3ba50a6f6aa9edb218400ac13).
That means that even if we keepcalculateTimeForTokens() for BC, we can avoid calling it.

Copy link
Member

@NyholmNyholm left a 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.

@jlekowski
Copy link
ContributorAuthor

Thank you@Nyholm. I squashed and rebased my changes.

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.

@wouterj, please have a look how you feel about this change. My original commit was herejlekowski@44807a6 withSymfony\Component\RateLimiter\Policy\Window::calculateTimeForTokens() being removed as it does not need to be used inFixedWindowLimiter. But it's your party, so I'm fine with either change :)

@jlekowskijlekowski changed the base branch from5.2 to5.3October 23, 2021 20:29
@fabpot
Copy link
Member

Thank you@jlekowski.

@fabpotfabpot merged commita972a6a intosymfony:5.3Oct 30, 2021
fabpot added a commit that referenced this pull requestNov 1, 2021
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       | MITTests 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).![image](https://user-images.githubusercontent.com/57155526/139543405-e8e1b760-96b2-4aea-8ed2-15c6221cd387.png)![image](https://user-images.githubusercontent.com/57155526/139543418-d9558c2e-9248-49dd-b941-ad917f5e23d2.png)`@fabpot` `@Nyholm` please reviewCommits-------3e2ca33 [RateLimiter] Increase delta in limiter tests
@fabpotfabpot mentioned this pull requestNov 22, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@NyholmNyholmNyholm approved these changes

@chalasrchalasrAwaiting requested review from chalasr

@dunglasdunglasAwaiting requested review from dunglas

@lyrixxlyrixxAwaiting requested review from lyrixx

@wouterjwouterjAwaiting requested review from wouterj

@xabbuhxabbuhAwaiting requested review from xabbuh

@ycerutoycerutoAwaiting requested review from yceruto

Assignees

No one assigned

Projects

None yet

Milestone

5.3

Development

Successfully merging this pull request may close these issues.

4 participants

@jlekowski@carsonbot@Nyholm@fabpot

[8]ページ先頭

©2009-2025 Movatter.jp