5dc562a318
Fixes #38338 |
||
---|---|---|
.. | ||
Exception | ||
Storage | ||
Tests | ||
Util | ||
.gitattributes | ||
.gitignore | ||
CHANGELOG.md | ||
composer.json | ||
CompoundLimiter.php | ||
FixedWindowLimiter.php | ||
LICENSE | ||
Limit.php | ||
Limiter.php | ||
LimiterInterface.php | ||
LimiterStateInterface.php | ||
NoLimiter.php | ||
phpunit.xml.dist | ||
Rate.php | ||
README.md | ||
Reservation.php | ||
ResetLimiterTrait.php | ||
TokenBucket.php | ||
TokenBucketLimiter.php | ||
Window.php |
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
}