Skip to content

symfony/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

823d97d · Sep 25, 2024
Sep 25, 2024
Jan 23, 2024
Jan 23, 2024
May 14, 2022
Jan 23, 2024
Jul 20, 2022
Sep 25, 2024
Sep 16, 2020
Oct 1, 2023
Jan 23, 2024
Jan 24, 2023
Jan 23, 2024
Sep 10, 2021
Dec 8, 2023
Jan 22, 2022
Jan 23, 2024
Dec 9, 2022
Oct 19, 2023
Jun 2, 2021

Repository files navigation

Rate Limiter Component

The Rate Limiter component provides a Token Bucket implementation to rate limit input and output in your application.

Getting Started

composer require symfony/rate-limiter
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\RateLimiterFactory;

$factory = new RateLimiterFactory([
    'id' => 'login',
    'policy' => 'token_bucket',
    'limit' => 10,
    'rate' => ['interval' => '15 minutes'],
], new InMemoryStorage());

$limiter = $factory->create();

// blocks until 1 token is free to use for this process
$limiter->reserve(1)->wait();
// ... execute the code

// only claims 1 token if it's free at this moment (useful if you plan to skip this process)
if ($limiter->consume(1)->isAccepted()) {
   // ... execute the code
}

Resources