symfony/rate-limiter
Symfony Rate Limiter provides a token bucket implementation for rate limiting input and output in PHP applications. Configure policies and storage with RateLimiterFactory, then reserve tokens to wait or consume tokens to check acceptance.
The Rate Limiter component provides a Token Bucket implementation to rate limit input and output in your application.
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
}
This package is looking for a backer.
Help Symfony by sponsoring its development!
How can I help you explore Laravel packages today?