razonyang / yii2-rate-limiter
Yii2 Rate Limiter
Installs: 18 753
Dependents: 1
Suggesters: 0
Security: 0
Stars: 9
Watchers: 4
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: ^7.1
- razonyang/token-bucket: ^1.0
- razonyang/yii2-psr-log: ^1.0
- yiisoft/yii2: ~2.0.13
Requires (Dev)
- codeception/codeception: ^3.0
- codeception/verify: ^1.1
- phpunit/phpunit: ^7
- yiisoft/yii2-redis: ^2.0
Suggests
- yiisoft/yii2-redis: Yii2 Redis extension for RedisManager
This package is auto-updated.
Last update: 2024-12-21 21:31:30 UTC
README
Backends
Memcached
requires memcached extension.Redis
requires redis extension or yiisoft/yii2-redis package.
Installation
composer require razonyang/yii2-rate-limiter
Usage
Let's take 5000 requests every hours as example:
return [ public function behaviors() { return [ // redis via redis extension 'rateLimiter' => [ 'class' => \RazonYang\Yii2\RateLimiter\RedisRateLimiter::class, 'password' => '', 'hostname' => 'localhost', 'port' => 6379, 'capacity' => 5000, 'rate' => 0.72, 'limitPeriod' => 3600, 'prefix' => 'rate_limiter:', 'ttl' => 3600, // 'nameCallback' => $callback, ], // redis via yii2-redis 'rateLimiter' => [ 'class' => \RazonYang\Yii2\RateLimiter\Redis\RateLimiter::class, 'redis' => 'redis', // redis component name or definition 'capacity' => 5000, 'rate' => 0.72, 'limitPeriod' => 3600, 'prefix' => 'rate_limiter:', 'ttl' => 3600, // 'nameCallback' => $callback, ], // memcached 'rateLimiter' => [ 'class' => \RazonYang\Yii2\RateLimiter\MemcachedRateLimiter::class, 'hostname' => 'localhost', 'port' => 11211, 'capacity' => 5000, 'rate' => 0.72, 'limitPeriod' => 3600, 'prefix' => 'rate_limiter:', 'ttl' => 3600, // 'nameCallback' => $callback, ], ]; } ];
RateLimiter
takes uid:route
(authorized) or ip:route
(guest) as bucket name, you can also change this behavior via nameCallback
:
$nameCallback = function ( \yii\web\User $user, \yii\web\Request $request, \yii\base\Action $action ): string { return 'bucket name'; }