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 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
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.
* When using a NoLimiter the limit will be ignored.
*/
public function create(string $key = null, int $limit = null): LimiterInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

What if you also want at some point to configure the rate dynamically? A solution could be to provide the whole config instead (and resolve it), WDYT?

Suggested change
public function create(string $key = null, int $limit = null): LimiterInterface
public function create(string $key = null, array $config = null): LimiterInterface

Copy link
Member

Choose a reason for hiding this comment

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

this would then force to validate the provided config each time this method is called.

To me, if you want to do things dynamically, it would be better to introduce a RateLimiterFactoryInterface allowing to do a custom implementation of the factory than adding a configuration override argument.
Thus, if you are creating a custom caller for the factory, you could already instantiate the limiter yourselves. And if you don't call it yourselves (relying on a usage in Symfony), you would not be able to pass a custom config.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, I that if we add an argument to the factory method to customize it, there is no point to not adding others (even if the limit is the only common parameter).

That's why, in my opinion too, it is way better to indeed introduce a RateLimiterFactoryInterface so there is no limit to the customization.

Copy link
Author

Choose a reason for hiding this comment

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

Alright :) I will create the interface instead later this week. Thanks guys!

{
$id = $this->config['id'].'-'.$key;
$lock = $this->lockFactory?->createLock($id);

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['limit'], $this->config['rate'], $this->storage, $lock),
'fixed_window' => new FixedWindowLimiter($id, $limit ?? $this->config['limit'], $this->config['interval'], $this->storage, $lock),
'sliding_window' => new SlidingWindowLimiter($id, $limit ?? $this->config['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