[RateLimiter] Fix SlidingWindow calculation#51592
Closed
Jeroeny wants to merge 1 commit intosymfony:5.4from
Closed
[RateLimiter] Fix SlidingWindow calculation#51592Jeroeny wants to merge 1 commit intosymfony:5.4from
Jeroeny wants to merge 1 commit intosymfony:5.4from
Conversation
f7da2b6 to
d52042a
Compare
d52042a to
cf9d0a3
Compare
Contributor
Author
Nyholm
requested changes
Sep 16, 2023
Member
There was a problem hiding this comment.
Thank you for this PR.
This is a bit tricky part of the code base. I suggest some extra eyes to review this.
Can you please add some more test for the new calculateTimeForToken()?
I also have to ask you to target 6.4 since this is a new feature and not a bugfix. The feature is "Add SlidingWindowLimiter::reserve()".
| return (int) floor($this->hitCountForLastWindow * (1 - $percentOfCurrentTimeFrame) + $this->hitCount); | ||
| } | ||
|
|
||
| public function getRetryAfter(): \DateTimeImmutable |
Member
There was a problem hiding this comment.
You cannot remove public methods. That is a BC break.
Contributor
Author
There was a problem hiding this comment.
My bad, thought the @internal class allowed this.
Contributor
Author
|
Reopened as feature PR: #51676 |
fabpot
added a commit
that referenced
this pull request
Oct 1, 2023
…oeny) This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [RateLimiter] Add SlidingWindowLimiter::reserve() | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | yes | Deprecations? | yes | Tickets | Fix #40289, Fixes #46875 | License | MIT This implements `LimiterInterface->reserve()` for `SlidingWindowLimiter`. I'm not sure if the lack of implementation was due to time/scope or if it's actually not possible and my approach is incorrect. But I like to give it a try anyway. Perhaps `@wouterj` you could elaborate on that? The calculation does the following: 1. Calculate tokens to be released within this window. E.g. if 4 were used in the last window, at 50% into the current, 2 are still to be released. 2. Calculate the time-per-token within the remainder of the window. If the requested tokens will be available before the end of the current window, return the time-per-token * needed-tokens. 3. Otherwise return time-per-token of the regular interval * needed-tokens(-after the current window). This wasn't a [bugfix](#51592), so back to feature PR. I couldn't reopen #47557, So had to create this new PR. Commits ------- 1b4a2df [RateLimiter] Add SlidingWindowLimiter::reserve()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reopened #47557 but as a bugfix PR targeting 5.4 instead.
This implements
LimiterInterface->reserve()forSlidingWindowLimiter. I'm not sure if the lack of implementation was due to time/scope or if it's actually not possible and my approach is incorrect. But I like to give it a try anyway. Perhaps @wouterj you could elaborate on that?The calculation does the following:
Some other things I've noticed in the RateLimiter:
FixedWindowLimiteruses aWindowclass, whereasSlidingWindowLimiteruses aSlidingWindow. Shouldn't the first have aFixedWindowclass?Windowclass takes its$windowSizeas argument,SlidingWindowdoes not. Perhaps the latter could also take this argument in this PR, since it's used in the calculation. But I guess those classes have to be backwards compatible.SlidingWindow->getRetryAfter()is no longer used and could be deprecated in favor ofcalculateTimeForTokens. Making it more like the fixed window.