Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\RateLimiter\TokenBucketLimiter;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
Expand Down Expand Up @@ -1843,7 +1843,7 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
->info('The service ID of a custom storage implementation, this precedes any configured "cache_pool"')
->defaultNull()
->end()
->enumNode('strategy')
->enumNode('policy')
->info('The rate limiting algorithm to use for this rate')
->isRequired()
->values(['fixed_window', 'token_bucket', 'sliding_window', 'no_limit'])
Expand All @@ -1853,10 +1853,10 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
->isRequired()
->end()
->scalarNode('interval')
->info('Configures the fixed interval if "strategy" is set to "fixed_window" or "sliding_window". The value must be a number followed by "second", "minute", "hour", "day", "week" or "month" (or their plural equivalent).')
->info('Configures the fixed interval if "policy" is set to "fixed_window" or "sliding_window". The value must be a number followed by "second", "minute", "hour", "day", "week" or "month" (or their plural equivalent).')
->end()
->arrayNode('rate')
->info('Configures the fill rate if "strategy" is set to "token_bucket"')
->info('Configures the fill rate if "policy" is set to "token_bucket"')
->children()
->scalarNode('interval')
->info('Configures the rate interval. The value must be a number followed by "second", "minute", "hour", "day", "week" or "month" (or their plural equivalent).')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
}

$limiterOptions = [
'strategy' => 'fixed_window',
'policy' => 'fixed_window',
'limit' => $config['max_attempts'],
'interval' => '1 minute',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\NoLimiter;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\RateLimit;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Lock\NoLock;
use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;
use Symfony\Component\RateLimiter\Storage\StorageInterface;
use Symfony\Component\RateLimiter\Util\TimeUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;

/**
* Implements a non limiting limiter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\RateLimiter\Util\TimeUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\Lock\LockInterface;
use Symfony\Component\RateLimiter\Storage\StorageInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\RateLimiter\Exception\InvalidIntervalException;
use Symfony\Component\RateLimiter\LimiterStateInterface;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Lock\NoLock;
use Symfony\Component\RateLimiter\Exception\ReserveNotSupportedException;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;
use Symfony\Component\RateLimiter\Storage\StorageInterface;
use Symfony\Component\RateLimiter\Util\TimeUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\RateLimiter\LimiterStateInterface;

/**
* @author Wouter de Jong <wouter@wouterj.nl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Lock\NoLock;
use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;
use Symfony\Component\RateLimiter\Storage\StorageInterface;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;
namespace Symfony\Component\RateLimiter\Policy;

use Symfony\Component\RateLimiter\LimiterStateInterface;

/**
* @author Wouter de Jong <wouter@wouterj.nl>
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/RateLimiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use Symfony\Component\RateLimiter\RateLimiterFactory;

$limiter = new RateLimiterFactory([
'id' => 'login',
'strategy' => 'token_bucket',
'policy' => 'token_bucket',
'limit' => 10,
'rate' => ['interval' => '15 minutes'],
], new InMemoryStorage());
Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/RateLimiter/RateLimiterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
use Symfony\Component\Lock\NoLock;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\Policy\Rate;
use Symfony\Component\RateLimiter\Policy\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\Storage\StorageInterface;

/**
Expand Down Expand Up @@ -44,7 +49,7 @@ public function create(?string $key = null): LimiterInterface
$id = $this->config['id'].$key;
$lock = $this->lockFactory ? $this->lockFactory->createLock($id) : new NoLock();

switch ($this->config['strategy']) {
switch ($this->config['policy']) {
case 'token_bucket':
return new TokenBucketLimiter($id, $this->config['limit'], $this->config['rate'], $this->storage, $lock);

Expand All @@ -58,7 +63,7 @@ public function create(?string $key = null): LimiterInterface
return new NoLimiter();

default:
throw new \LogicException(sprintf('Limiter strategy "%s" does not exists, it must be either "token_bucket", "sliding_window", "fixed_window" or "no_limit".', $this->config['strategy']));
throw new \LogicException(sprintf('Limiter policy "%s" does not exists, it must be either "token_bucket", "sliding_window", "fixed_window" or "no_limit".', $this->config['policy']));
}
}

Expand All @@ -78,7 +83,7 @@ protected static function configureOptions(OptionsResolver $options): void

$options
->define('id')->required()
->define('strategy')
->define('policy')
->required()
->allowedValues('token_bucket', 'fixed_window', 'sliding_window', 'no_limit')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\CompoundLimiter;
use Symfony\Component\RateLimiter\Exception\ReserveNotSupportedException;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/RateLimiter/Tests/LimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\StorageInterface;
use Symfony\Component\RateLimiter\TokenBucketLimiter;

class LimiterTest extends TestCase
{
public function testTokenBucket()
{
$factory = $this->createFactory([
'id' => 'test',
'strategy' => 'token_bucket',
'policy' => 'token_bucket',
'limit' => 10,
'rate' => ['interval' => '1 second'],
]);
Expand All @@ -37,7 +37,7 @@ public function testFixedWindow()
{
$factory = $this->createFactory([
'id' => 'test',
'strategy' => 'fixed_window',
'policy' => 'fixed_window',
'limit' => 10,
'interval' => '1 minute',
]);
Expand All @@ -53,7 +53,7 @@ public function testWrongInterval()

$this->createFactory([
'id' => 'test',
'strategy' => 'fixed_window',
'policy' => 'fixed_window',
'limit' => 10,
'interval' => '1 minut',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\NoLimiter;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\Policy\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\TokenBucketLimiter;

class RateLimiterFactoryTest extends TestCase
{
Expand All @@ -35,27 +35,27 @@ public function testValidConfig(string $expectedClass, array $config)
public function validConfigProvider()
{
yield [TokenBucketLimiter::class, [
'strategy' => 'token_bucket',
'policy' => 'token_bucket',
'id' => 'test',
'limit' => 5,
'rate' => [
'interval' => '5 seconds',
],
]];
yield [FixedWindowLimiter::class, [
'strategy' => 'fixed_window',
'policy' => 'fixed_window',
'id' => 'test',
'limit' => 5,
'interval' => '5 seconds',
]];
yield [SlidingWindowLimiter::class, [
'strategy' => 'sliding_window',
'policy' => 'sliding_window',
'id' => 'test',
'limit' => 5,
'interval' => '5 seconds',
]];
yield [NoLimiter::class, [
'strategy' => 'no_limit',
'policy' => 'no_limit',
'id' => 'test',
]];
}
Expand All @@ -73,7 +73,7 @@ public function testInvalidConfig(string $exceptionClass, array $config)
public function invalidConfigProvider()
{
yield [MissingOptionsException::class, [
'strategy' => 'token_bucket',
'policy' => 'token_bucket',
]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\RateLimiter\Policy\Window;
use Symfony\Component\RateLimiter\Storage\CacheStorage;
use Symfony\Component\RateLimiter\Window;

class CacheStorageTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests;
namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests;
namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\Exception\ReserveNotSupportedException;
use Symfony\Component\RateLimiter\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Policy\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests;
namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Component\RateLimiter\Exception\InvalidIntervalException;
use Symfony\Component\RateLimiter\SlidingWindow;
use Symfony\Component\RateLimiter\Policy\SlidingWindow;

class SlidingWindowTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Tests;
namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException;
use Symfony\Component\RateLimiter\Rate;
use Symfony\Component\RateLimiter\Policy\Rate;
use Symfony\Component\RateLimiter\Policy\TokenBucket;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;
use Symfony\Component\RateLimiter\TokenBucket;
use Symfony\Component\RateLimiter\TokenBucketLimiter;

/**
* @group time-sensitive
Expand Down
Loading