Skip to content

[RateLimiter] Fix infinite values with NoLimiter #39911

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
Jan 23, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/RateLimiter/Policy/NoLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ final class NoLimiter implements LimiterInterface
{
public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
{
return new Reservation(time(), new RateLimit(\INF, new \DateTimeImmutable(), true, \INF));
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not into the code nor the component, but couldn't we use "-1" like for example done in memory_limit setting in PHP instead of a very large number? 🧐

cc @wouterj

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently the only use of the remainingTokens and limit properties is for the developer to know how many requests the user can send. This is not used internally in the RateLimiter component.

return new Reservation(time(), new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX));
}

public function consume(int $tokens = 1): RateLimit
{
return new RateLimit(\INF, new \DateTimeImmutable(), true, \INF);
return new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX);
}

public function reset(): void
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/RateLimiter/Tests/Policy/NoLimiterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;

class NoLimiterTest extends TestCase
{
public function testConsume()
{
$limiter = new NoLimiter();
$this->assertInstanceOf(RateLimit::class, $limiter->consume());
}

public function testReserve()
{
$limiter = new NoLimiter();
$this->assertInstanceOf(Reservation::class, $limiter->reserve());
}
}