antikirra / rate-limiter
Installs: 190
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/antikirra/rate-limiter
Requires
- php: ^7.4 || ^8.0
- ext-redis: *
- antikirra/probability: ^2.0.0
README
Install
composer require antikirra/rate-limiter
Basic usage
<?php use Antikirra\RateLimiter; require __DIR__ . '/vendor/autoload.php'; $redis = new Redis(); $redis->connect('127.0.0.1'); // maximum of three requests per minute $limiter = new RateLimiter($redis, "signin_by_ip{$_SERVER['REMOTE_ADDR']}", 3, 60, 0.75); $result = $limiter->check(); // returns the actual counter value without any side effects echo $result->getCount(); // (int) actual counter value print_r($result->isPassed()); // (bool) true - it's okay, false - flood has been detected!!! $result = $limiter->hit(); // increments the counter value, returning the result of the check echo $result->getCount(); // (int) updated counter value print_r($result->isPassed()); // (bool) true - it's okay, false - flood has been detected!!! print_r($result->isFailed()); // (bool) true - flood has been detected!!!, false - it's okay $limiter->reset(); // (void) resets counters to zero