Skip to content

🚔 General purpose rate limiter implementation.

License

Notifications You must be signed in to change notification settings

2Book/rate-limit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rate Limit

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version PDS Skeleton

General purpose rate limiter that can be used to limit the rate at which certain operation can be performed. Default implementation uses Redis as backend.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require nikolaposa/rate-limit

Usage

Offensive rate limiting

use RateLimit\Exception\LimitExceeded;
use RateLimit\Rate;
use RateLimit\RedisRateLimiter;
use Redis;

$rateLimiter = new RedisRateLimiter(new Redis());

$apiKey = 'abc123';

try {
    $rateLimiter->limit($apiKey, Rate::perMinute(100));
    
    //on success
} catch (LimitExceeded $exception) {
   //on limit exceeded
}

Silent rate limiting

use RateLimit\Rate;
use RateLimit\RedisRateLimiter;
use Redis;

$rateLimiter = new RedisRateLimiter(new Redis());

$apiKey = 'abc123';
$status = $rateLimiter->limitSilently($apiKey, Rate::perMinute(100));

echo $status->getRemainingAttempts(); //99

Credits

License

Released under MIT License - see the License File for details.

About

🚔 General purpose rate limiter implementation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%