avaibook / symfony-rate-limiter
Sf4.4 compatibility patch of Symfony RateLimiter component, that provides a Token Bucket implementation to rate limit input and output in your application
Installs: 13 010
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.5
- symfony/lock: ^4.4
- symfony/options-resolver: ^4.4
Requires (Dev)
- psr/cache: ^1.0|^2.0|^3.0
This package is not auto-updated.
Last update: 2024-11-15 02:24:02 UTC
README
The Rate Limiter component provides a Token Bucket implementation to rate limit input and output in your application.
This is a fork of the original bundle that makes it compatible with Symfony <= 4.4.
This Component is experimental. Experimental features are not covered by Symfony's Backward Compatibility Promise.
Getting Started
$ composer require avaibook/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 }