8000 [RateLimiter] Document the RateLimit object by javiereguiluz · Pull Request #14461 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[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 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 22, 2020

Conversation

javiereguiluz
Copy link
Member

Fixes #14447.

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`
Copy link
Member

Choose a reason for hiding this comment

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

The reserve() method returns a Reservation object that also has a getRateLimit() method.

rate_limiter.rst Outdated
Comment on lines 246 to 250
$response->headers->add([
'X-RateLimit-Remaining' => $limit->getRemainingTokens(),
'X-RateLimit-Reset' => $limit->getRetryAfter()->getTimestamp(),
'X-RateLimit-Limit' => $limit->getLimit(),
]);
Copy link
Member

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()) {
    return new Response(null, Response::HTTP_TOO_MANY_REQUESTS, $headers);
}

// ...

$response = new Response(...);
$response->headers->add($headers);

return $response;

(btw, note that we should use X-RateLimit-Retry-After instead of X-RateLimit-Reset)

@javiereguiluz
Copy link
Member Author

Wouter, thanks for your review. These were fantastic fixes and suggestions, so I implemented all of them.

Copy link
Member
@wouterj wouterj 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 for taking care of documenting these changes Javier. Looks perfect to me this way!

< 8962 div class="text-right ml-1"> 9d189ef
@javiereguiluz javiereguiluz merged commit bc16e02 into symfony:5.x Oct 22, 2020
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] rename Limit to RateLimit and add RateLimit::getLimit()
3 participants
0