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 retryAfter value on last token consume (SlidingWindow).#54163

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

Open
ERuban wants to merge2 commits intosymfony:6.4
base:6.4
Choose a base branch
Loading
fromERuban:fix_rate_limiter_last_token_consume

Conversation

ERuban
Copy link
Contributor

@ERubanERuban commentedMar 5, 2024
edited
Loading

QA
Branch?6.4
Bug fix?yes
New feature?no
Deprecations?no
LicenseMIT

Fix retryAfter value when the last tokens are consumed ($availableTokens === $tokens).

/cc@wouterj 🙏

@carsonbotcarsonbot added this to the6.4 milestoneMar 5, 2024
@ERubanERuban changed the title[RateLimiter] Fix results on last token consume.[RateLimiter] Fix retryAfter value on last token consume (SlidingWindow).Mar 5, 2024
Comment on lines +77 to +84
if ($availableTokens === $tokens) {
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount());
$retryAfter = $now + $resetDuration;
} else {
$retryAfter = $now;
}

$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($retryAfter)), true, $this->limit));

Choose a reason for hiding this comment

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

Hi!

I was wondering if we were following the spirit and formalism ofthe previous lines of code 69 to 72 :

$resetDuration =$window->calculateTimeForTokens($this->limit,$window->getHitCount());$resetTime = \DateTimeImmutable::createFromFormat('U',$availableTokens ?floor($now) :floor($now +$resetDuration));returnnewReservation($now,newRateLimit($availableTokens,$resetTime,true,$this->limit));

it might be possible to propose the following code:

Suggested change
if ($availableTokens ===$tokens) {
$resetDuration =$window->calculateTimeForTokens($this->limit,$window->getHitCount());
$retryAfter =$now +$resetDuration;
}else {
$retryAfter =$now;
}
$reservation =newReservation($now,newRateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U',floor($retryAfter)),true,$this->limit));
$resetDuration =$window->calculateTimeForTokens($this->limit,$window->getHitCount());
$resetTime = \DateTimeImmutable::createFromFormat('U',$availableTokens ===$tokens ?floor($now +$resetDuration) :floor($now));
$reservation =newReservation($now,newRateLimit($this->getAvailableTokens($window->getHitCount()),$resetTime,true,$this->limit));

Do you think it's worth keeping this consistency or do you think it's too much?
(This suggestion passes the tests.)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Not sure that it makes sense, and imo using if-else is more readable than ternary ones

@jprivet-dev
Copy link

jprivet-dev commentedMar 6, 2024
edited
Loading

Hello again!

I noticed a small detail: thetryon the line 57 of the file SlidingWindowLimiter.php never catches an exception. It's only used forfinally, which executes in all cases,even with a return in thetry:

try {...returnnewReservation($now,newRateLimit($availableTokens,$resetTime,true,$this->limit));...        }finally {$this->lock?->release();        }

In this case, all the errors in thetry will be silent. Isn't that risky? 😥

@ERuban
Copy link
ContributorAuthor

In this case, all the errors in thetry will be silent. Isn't that risky? 😥

Seems it is not related to that PR, so you can suggest some fixes in a separate PR.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jprivet-devjprivet-devjprivet-dev left review comments

Assignees
No one assigned
Projects
None yet
Milestone
6.4
Development

Successfully merging this pull request may close these issues.

3 participants
@ERuban@jprivet-dev@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp