Skip to content
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

[5.2] Fixed the remaining attempts calculation #13756

Merged
merged 1 commit into from
May 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ protected function buildResponse($key, $maxAttempts)
{
$response = new Response('Too Many Attempts.', 429);

$retryAfter = $this->limiter->availableIn($key);

return $this->addHeaders(
$response, $maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts),
$this->limiter->availableIn($key)
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
}

Expand Down Expand Up @@ -112,10 +114,15 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp
*
* @param string $key
* @param int $maxAttempts
* @param int|null $retryAfter
* @return int
*/
protected function calculateRemainingAttempts($key, $maxAttempts)
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null)
{
return $maxAttempts - $this->limiter->attempts($key) + 1;
if ($retryAfter) {
Copy link
Member

Choose a reason for hiding this comment

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

What are we actually trying to check here? That it is greater than 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

Checking if it's null or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make it more strict, one sec...

return 0;
}

return $this->limiter->retriesLeft($key, $maxAttempts);
}
}