Skip to content

[RateLimiter] Use RateLimiterFactory with custom limit #51403

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

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
issue-51401: Add custom limit to RateLimiterFactory
Add an optional custom limit to the RateLimiterFactory to overwrite configured values.
  • Loading branch information
Dooij committed Aug 16, 2023
commit 233f00e6b8ed7076ef0622ee55f452082aa3e0b6
12 changes: 8 additions & 4 deletions src/Symfony/Component/RateLimiter/RateLimiterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ public function __construct(array $config, StorageInterface $storage, LockFactor
$this->config = $options->resolve($config);
}

public function create(string $key = null): LimiterInterface
/**
* Parsing a custom limit to this function will overwrite the limit in your configuration for this request.
*/
public function create(string $key = null, ?int $limit = null): LimiterInterface
{
$id = $this->config['id'].'-'.$key;
$lock = $this->lockFactory?->createLock($id);
$limit = $limit ?? $this->config['limit'];

return match ($this->config['policy']) {
'token_bucket' => new TokenBucketLimiter($id, $this->config['limit'], $this->config['rate'], $this->storage, $lock),
'fixed_window' => new FixedWindowLimiter($id, $this->config['limit'], $this->config['interval'], $this->storage, $lock),
'sliding_window' => new SlidingWindowLimiter($id, $this->config['limit'], $this->config['interval'], $this->storage, $lock),
'token_bucket' => new TokenBucketLimiter($id, $limit, $this->config['rate'], $this->storage, $lock),
'fixed_window' => new FixedWindowLimiter($id, $limit, $this->config['interval'], $this->storage, $lock),
'sliding_window' => new SlidingWindowLimiter($id, $limit, $this->config['interval'], $this->storage, $lock),
'no_limit' => new NoLimiter(),
default => 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 Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/RateLimiter/Tests/RateLimiterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public function testInvalidConfig(string $exceptionClass, array $config)
$factory->create('key');
}

public function testCustomLimit()
{
$factory = new RateLimiterFactory(
[
'policy' => 'sliding_window',
'id' => 'test',
'limit' => 5,
'interval' => '5 seconds',
],
new InMemoryStorage()
);

$rateLimiter = $factory->create('key', 10);
$rateLimit = $rateLimiter->consume(0);
$this->assertSame(10, $rateLimit->getRemainingTokens());
}

public static function invalidConfigProvider()
{
yield [MissingOptionsException::class, [
Expand Down