silencenjoyer / rate-limiter
A rate limiter to control and manage execution flow.
2.0.0
2025-03-15 12:51 UTC
Requires
- php: ^7.4|^8.0
- ext-redis: *
Requires (Dev)
- phpunit/phpunit: ^9.0
README
This package provides possibility to control and manage execution flow.
Installation
You can install the package via composer:
composer require silencenjoyer/rate-limiter
Usage
use Silencenjoyer\RateLimit\Counters\RedisCounter; use Silencenjoyer\RateLimit\Intervals\Interval; use Silencenjoyer\RateLimit\Limiters\RateLimiter; use Silencenjoyer\RateLimit\Rates\Rate; $counter = new RedisCounter('rate:send:api', new Redis()); $rateLimiter = new RateLimiter($counter, new Rate(10, new Interval('PT1S'))); foreach ($messages as $message) { $rateLimiter->stretch(function() use ($api) { $api->post($message); }); }
use Silencenjoyer\RateLimit\Counters\LocalCounter; use Silencenjoyer\RateLimit\Intervals\Interval; use Silencenjoyer\RateLimit\Limiters\RateLimiter; use Silencenjoyer\RateLimit\Rates\Rate; require_once __DIR__ . '/vendor/autoload.php'; $counter = new LocalCounter(); $rateLimiter = new RateLimiter($counter, new Rate(5, new Interval('PT1S'))); if (!$rateLimiter->isExceed()) { $rateLimiter->collectUsage(); // do some logic } throw new RuntimeException('Rate limit has been exceeded.');
Testing
composer test
composer test-coverage
docker-compose -f tests/docker/docker-compose.test.yml up
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email an_gebrich@outlook.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.