Skip to content

[RateLimiter] Use RateLimiterFactory with custom limit #51401

@IlonavD

Description

@IlonavD

Description

Currently the RateLimitFactory fetches the configuration and determines the limit based of that. We'd like to be more flexible with that, allowing certain users a higher limit than others. Adding an optional parameter to the create function which will default to the limit set in the config.

Example

// src/Controller/ApiController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\RateLimiter\RateLimiterFactory;

class ApiController extends AbstractController
{
    public function registerUser(Request $request, RateLimiterFactory $authenticatedApiLimiter): Response
    {
        $user = $request->getUser();
        $limiter = $authenticatedApiLimiter->create($user->getApiKey(), $user->getRateLimit());

        // ...
    }

    // ...
}

I am happy to make this PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXDX = Developer eXperience (anything that improves the experience of using Symfony)RateLimiter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions