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-12-02 09:46:43 +01:00
..
Exception [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
Policy [RateLimiter] Moved classes implementing LimiterInterface to a new namespace 2020-10-25 09:35:33 +01:00
Storage Fix delete method on RateLimiter's cache storage 2020-10-22 11:48:25 +02:00
Tests [RateLimiter] Moved classes implementing LimiterInterface to a new namespace 2020-10-25 09:35:33 +01: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 Merge branch '5.1' into 5.x 2020-10-24 14:08:07 +02:00
CompoundLimiter.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
LICENSE [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
LimiterInterface.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
LimiterStateInterface.php Use __sleep/__wakeup instead of Serializable 2020-10-01 14:20:33 +02:00
phpunit.xml.dist [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
RateLimit.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
RateLimiterFactory.php [RateLimiter] Moved classes implementing LimiterInterface to a new namespace 2020-10-25 09:35:33 +01:00
README.md Fix rate limiter documentation 2020-12-02 09:46:43 +01:00
Reservation.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +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\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