Skip to content

[RateLimiter] bug #42194 fix: sliding window policy to use microtime #43677

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 24, 2021
Merged

[RateLimiter] bug #42194 fix: sliding window policy to use microtime #43677

merged 1 commit into from
Oct 24, 2021

Conversation

jlekowski
Copy link
Contributor

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

Other parts of RateLimiter use microtime(), while sliding window policy uses time() instead. That means the window may expire only up to 0.(9) seconds after the interval. Below an example that fails (window not expired) majority of the times (window is expired only if it starts e.g. at 1634992063.4999 and is checked at 1634992065.5000 - with time() rounding it would be 3 seconds).

<?php

use Symfony\Component\RateLimiter\Policy\SlidingWindow;

require_once './vendor/autoload.php';

$window = new SlidingWindow('some_id', 2);
sleep(2); // mimic $limit->wait();
var_dump($window->isExpired()); // false - same second

@carsonbot carsonbot added this to the 5.3 milestone Oct 23, 2021
@carsonbot carsonbot changed the title bug #42194 [RateLimiter] fix: sliding window policy to use microtime [RateLimiter] bug #42194 fix: sliding window policy to use microtime Oct 23, 2021
@@ -72,4 +72,34 @@ public function testLongIntervalCreate()
$new = SlidingWindow::createFromPreviousWindow($window, 60);
$this->assertFalse($new->isExpired());
}

public function testCreateFromPreviousWindowUsesMicrotime()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests consistently pass after the changes. The old code would inconsistently fail, though (depend on the microtome(true) result at the moment of running the test.

@jlekowski
Copy link
Contributor Author

@Nyholm, @wouterj - please have a look at this PR.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you

@fabpot fabpot modified the milestones: 5.3, 5.4 Oct 24, 2021
@fabpot fabpot changed the base branch from 5.3 to 5.4 October 24, 2021 08:45
@fabpot
Copy link
Member

fabpot commented Oct 24, 2021

Thank you @jlekowski.

@fabpot fabpot merged commit 85bf403 into symfony:5.4 Oct 24, 2021
@chalasr
Copy link
Member

chalasr commented Oct 24, 2021

This PR introduced a randomly failing test https://github.com/symfony/symfony/runs/3990643552?check_suite_focus=true#step:8:987. @jlekowski could you please have a look?

@jlekowski
Copy link
Contributor Author

@chalasr, I'll have a look at it. Thanks for letting me know.

chalasr added a commit that referenced this pull request Oct 26, 2021
…se microtime - fix test (jlekowski)

This PR was merged into the 5.4 branch.

Discussion
----------

[RateLimiter] bug #42194 fix: sliding window policy to use microtime - fix test

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #42194 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

As pointed out in the comment #43677 (comment), `RateLimitTest::testWaitUsesMicrotime()` fails intermittently.
I have looked at all `PHPUnit` actions since the test was introduced and interestingly, the fails only occurred during `7.2` test, and the time difference was always ~1.3s.

Commits
-------

e616963 bug #42194 [RateLimiter] fix: sliding window policy to use microtime - fix test
This was referenced Nov 5, 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.

[RateLimiter] SlidingWindow to use microtime() instead of time()
5 participants