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-22 12:14:59 +02:00
..
Exception [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
Storage Fix delete method on RateLimiter's cache storage 2020-10-22 11:48:25 +02:00
Tests minor 2020-10-22 12:14:59 +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 Merge branch '5.1' into 5.x 2020-10-14 19:08:19 +02:00
CompoundLimiter.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
FixedWindowLimiter.php [RateLimiter] Adding annotations 2020-10-22 10:25:47 +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
NoLimiter.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +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
RateLimit.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
RateLimiter.php [RateLimiter] Allow configuration value "no_limit" 2020-10-22 10:35:15 +02:00
README.md [RateLimiter] Added reserve() to LimiterInterface and rename Limiter to RateLimiter 2020-10-16 07:10:27 +02:00
Reservation.php [RateLimiter] rename Limit to RateLimit and add RateLimit::getLimit() 2020-10-20 08:15:14 +02:00
ResetLimiterTrait.php [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
SlidingWindow.php [RateLimiter] Adding annotations 2020-10-22 10:25:47 +02:00
SlidingWindowLimiter.php [RateLimiter] Adding annotations 2020-10-22 10:25:47 +02:00
TokenBucket.php [RateLimiter] Adding annotations 2020-10-22 10:25:47 +02:00
TokenBucketLimiter.php [RateLimiter] Adding annotations 2020-10-22 10:25:47 +02:00
Window.php minor #38668 [RateLimiter] Remove Window::sleep() (Nyholm) 2020-10-22 10:31:04 +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\RateLimiter;

$limiter = new RateLimiter([
    '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