Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[RateLimiter] Document the RateLimit object#14461
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
rate_limiter.rst Outdated
| headers in the response to expose the limit status (e.g. remaining tokens, when | ||
| new tokens will be available, etc.) | ||
| That's why the ``consume()`` object returns a:class:`Symfony\\Component\\RateLimiter\\RateLimit` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thereserve() method returns aReservation object that also has agetRateLimit() method.
rate_limiter.rst Outdated
| $response->headers->add([ | ||
| 'X-RateLimit-Remaining' => $limit->getRemainingTokens(), | ||
| 'X-RateLimit-Reset' => $limit->getRetryAfter()->getTimestamp(), | ||
| 'X-RateLimit-Limit' => $limit->getLimit(), | ||
| ]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
These headers should be present in all responses, also the rejected ones. So what about changing this controller to:
$limiter =$anonymousApiLimiter->create($request->getClientIp());$rateLimit =$limiter->consume();$headers = ['X-RateLimit-Remaining' =>$limit->getRemainingTokens(),'X-RateLimit-Retry-After' =>$limit->getRetryAfter()->getTimestamp(),'X-RateLimit-Limit' =>$limit->getLimit(),];if (false ===$limit->isAccepted()) {returnnewResponse(null, Response::HTTP_TOO_MANY_REQUESTS,$headers);}// ...$response =newResponse(...);$response->headers->add($headers);return$response;
(btw, note that we should useX-RateLimit-Retry-After instead ofX-RateLimit-Reset)
javiereguiluz commentedOct 22, 2020
Wouter, thanks for your review. These were fantastic fixes and suggestions, so I implemented all of them. |
wouterj left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thank you for taking care of documenting these changes Javier. Looks perfect to me this way!
Fixes#14447.