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

Conversation

IlonavD
Copy link

@IlonavD IlonavD commented Aug 16, 2023

Add an optional custom limit to the RateLimiterFactory to overwrite configured values.

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #51401
License MIT
Doc PR symfony/symfony-docs#...

The RateLimiterFactory by default uses the limit set in Config. However in certain situations you might want to change this limit per user. This allows for for that option. When no limit is passed to the Factory, the default configured value will be used.

Add an optional custom limit to the RateLimiterFactory to overwrite configured values.
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

* 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!

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@yceruto yceruto modified the milestones: 6.4, 7.1 Nov 15, 2023
@yceruto yceruto removed the Bug label Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@kbond
Copy link
Member

kbond commented Mar 26, 2025

I put some ideas on this feature here: #51401 (comment)

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RateLimiter] Use RateLimiterFactory with custom limit
10 participants