michaelesmith / throttle
A basic throttling implementation
Installs: 2 373
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 2
Open Issues: 1
Requires
- php: ^7.1
- psr/cache: ^1.0
Requires (Dev)
- mockery/mockery: ^1.2
- php-mock/php-mock-mockery: ^1.3
- phpunit/phpunit: ^7.4
Suggests
- cache/cache: Library of all the php-cache adapters
- symfony/cache: Symfony Cache component with PSR-6, PSR-16, and tags
This package is auto-updated.
Last update: 2024-10-18 05:10:58 UTC
README
What is it?
A basic throttling implementation
Versions
If you are looking for the old version compatible with PHP 5.3 try the v0.1 branch and update your composer requirement to "michaelesmith/throttle": "^0.1"
Installation
composer require "michaelesmith/throttle"
You will also need a PSR-6 compatible cache library such as "cache/cache"
or "symfony/cache"
.
Examples
$throttle = new Throttle(new \Cache\Adapter\PHPArray\ArrayCachePool()); $throttle->add(new Condition(60, 2)); // adds an interval where 2 increments are allowed in 60 seconds $throttle->add(new Condition(600, 5)); // adds an interval where 5 increments are allowed in 10 minutes // in each action you want to limit try { $throttle->increment($_SERVER['REMOTE_ADDR']); // some client identifier like an ip or session id // NOTE: $_SERVER['REMOTE_ADDR'] may not gove you the actual client IP if you are behind a reverse proxy // Here is how Symfony finds the client IP // @link: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L786-L805 } catch(RateException $e) { $condition = $e->getCondition(); // the condition that hit the rate limit printf('You can only make %d requests in %d seconds', $condition->getLimit(), $condition->getTtl()); }
You can use any PSR-6 compatible cache pool, but you need to use one that is persistent across requests for most cases unlike this example.