This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/RateLimiter
2020-10-01 14:20:33 +02:00
..
Exception [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
Storage [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
Tests [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
Util [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
.gitattributes [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
.gitignore [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
CHANGELOG.md [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
composer.json [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
CompoundLimiter.php Call all compound limiters on failure and added IO blocking 2020-09-30 12:14:20 +02:00
FixedWindowLimiter.php [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
LICENSE [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
Limit.php Call all compound limiters on failure and added IO blocking 2020-09-30 12:14:20 +02:00
Limiter.php [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
LimiterInterface.php [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
LimiterStateInterface.php Use __sleep/__wakeup instead of Serializable 2020-10-01 14:20:33 +02:00
NoLimiter.php Added request rate limiters and improved login throttling 2020-09-30 21:18:40 +02:00
phpunit.xml.dist [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
Rate.php [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
README.md [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
Reservation.php [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
ResetLimiterTrait.php [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
TokenBucket.php Use __sleep/__wakeup instead of Serializable 2020-10-01 14:20:33 +02:00
TokenBucketLimiter.php [RateLimiter] Return Limit object on Consume method 2020-09-30 07:47:20 +02:00
Window.php Use __sleep/__wakeup instead of Serializable 2020-10-01 14:20:33 +02:00

Rate Limiter Component

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

This Component is experimental. Experimental features are not covered by Symfony's Backward Compatibility Promise.

Getting Started

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

$limiter = new Limiter([
    'id' => 'login',
    'strategy' => 'token_bucket', // or 'fixed_window'
    'limit' => 10,
    'rate' => ['interval' => '15 minutes'],
], new InMemoryStorage());

// 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