hi-man / rateguardian
local rate limit based on yac
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/hi-man/rateguardian
Requires
Requires (Dev)
- phpunit/phpunit: ^9.4
README
keep your server calm down
Requirements
- php 7.3+
- yac 2.0.3+
Installation
composer require hi-man/rateguardian
Usage
step 1 : initialize guardian
RateGuardian::getInstance()->guardianOn($key, $total, $ttl)
$keythe unique guardian key, such as api pathname, not longer than 41 characters$ttlin seconds, a counter will be increased in this period$totalthe max counter value allowd in$ttlsecond
step 2 : guard api with guardian key
RateGuardian::getInstance()->guard($key);
- a
falsevalue indicates the api is overloaded, application should handle this situation instead of providing service - return value
- return
trueif counter less than$total - return
trueif counter equals$total - return
trueif$keydoes not be registered withguardianOn - otherwise return
false
- return
step 3 : clear guardian
RateGuardian::getInstance()->guardianOff($key);
- restart
php-fpmor php script also cleared guardian setting
optional: get guardian info
RateGuardian::getInstance()->show($key);
- return a array of guardian info
totalthe value provided byguardianOnttlthe value provided byguardianOnexpiredunix timestamp that calculating periods endedcurrentcurrent counter value