Skip to content

[RateLimiter] Fix SlidingWindow calculations #47557

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

Closed
wants to merge 1 commit into from

Conversation

Jeroeny
Copy link
Contributor

@Jeroeny Jeroeny commented Sep 12, 2022

Q A
Branch? 6.4
Bug fix? yes
New feature? yes
Deprecations? no
Tickets Fix #40289
License MIT
Doc PR

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).

Some other things I've noticed in the RateLimiter:

  • FixedWindowLimiter uses a Window class, whereas SlidingWindowLimiter uses a SlidingWindow. Shouldn't the first have a FixedWindow class?
  • The Window class takes its $windowSize as argument, SlidingWindow does 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 of calculateTimeForTokens. Making it more like the fixed window.

Edit: This should fix #40289, I've added it to the QA table.

@carsonbot carsonbot added this to the 6.2 milestone Sep 12, 2022
@Jeroeny Jeroeny changed the title [RateLimit] Implement reserve method for SlidingWindow [RateLimiter] Implement reserve method for SlidingWindow Sep 12, 2022
@Jeroeny Jeroeny force-pushed the ratelimit branch 3 times, most recently from 11da730 to e7a8d43 Compare September 12, 2022 15:35
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@Jeroeny Jeroeny force-pushed the ratelimit branch 2 times, most recently from d5629b0 to a585b4c Compare November 23, 2022 10:19
@Jeroeny Jeroeny changed the title [RateLimiter] Implement reserve method for SlidingWindow [RateLimiter] FIx SlidingWindow calculations Nov 30, 2022
@Jeroeny Jeroeny changed the title [RateLimiter] FIx SlidingWindow calculations [RateLimiter] Fix SlidingWindow calculations Nov 30, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@Jeroeny Jeroeny closed this Sep 7, 2023
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()
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] retry-after of SlidingWindowLimiter is incorrect
3 participants