yiisoft / rate-limiter
Yii Rate Limiter Middleware
Fund package maintenance!
Opencollective
yiisoft
Installs: 24 325
Dependents: 3
Suggesters: 0
Security: 0
Stars: 28
Watchers: 20
Forks: 10
Open Issues: 0
Requires
- php: ^8.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1|^2.0
- psr/http-message-implementation: 1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/simple-cache: ^2.0|^3.0
- yiisoft/http: ^1.2
Requires (Dev)
- ext-apcu: *
- maglnet/composer-require-checker: ^4.4
- nyholm/psr7: ^1.0
- phpunit/phpunit: ^9.5
- rector/rector: ^0.17.7
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.13
- yiisoft/cache: ^3.0
Suggests
- ext-apcu: To use APCu storage
This package is auto-updated.
Last update: 2024-10-25 07:47:06 UTC
README
Yii Rate Limiter Middleware
Rate limiter middleware helps to prevent abuse by limiting the number of requests that could be me made consequentially.
For example, you may want to limit the API usage of each user to be at most 100 API calls within a period of 10 minutes. If too many requests are received from a user within the stated period of the time, a response with status code 429 (meaning "Too Many Requests") should be returned.
Requirements
- PHP 8.0 or higher.
Installation
The package could be installed with Composer:
composer require yiisoft/rate-limiter
General usage
use Psr\Http\Message\ServerRequestInterface; use Yiisoft\Yii\RateLimiter\LimitRequestsMiddleware; use Yiisoft\Yii\RateLimiter\Counter; use Nyholm\Psr7\Factory\Psr17Factory; use Yiisoft\Yii\RateLimiter\Policy\LimitAlways; use Yiisoft\Yii\RateLimiter\Policy\LimitPerIp; use Yiisoft\Yii\RateLimiter\Policy\LimitCallback; use Yiisoft\Yii\RateLimiter\Storage\StorageInterface; use Yiisoft\Yii\RateLimiter\Storage\SimpleCacheStorage; /** @var StorageInterface $storage */ $storage = new SimpleCacheStorage($cache); $counter = new Counter($storage, 2, 5); $responseFactory = new Psr17Factory(); $middleware = new LimitRequestsMiddleware($counter, $responseFactory); // LimitPerIp by default
In the above 2 is the maximum number of counter increments (requests) that could be performed before increments are limited and 5 is a period to apply limit to, in seconds.
The Counter
implements generic cell rate limit algorithm (GCRA)
that ensures that after reaching the limit further increments are distributed equally.
Note: While it is sufficiently effective, it is preferred to use Nginx or another webserver capabilities for rate limiting. This package allows rate-limiting in the project with deployment environment you cannot control such as installable CMS.
Implementing your own limiting policy
There are two ready to use limiting policies available in the package:
LimitAlways
- to count all incoming requests.LimitPerIp
- to count requests from different IPs separately.
These could be applied as follows:
$middleware = new LimitRequestsMiddleware($counter, $responseFactory, new LimitPerIp()); // or $middleware = new LimitRequestsMiddleware($counter, $responseFactory, new LimitAlways());
Easiest way to customize a policy is to use LimitCallback
:
$middleware = new LimitRequestsMiddleware($counter, $responseFactory, new LimitCallback(function (ServerRequestInterface $request): string { // return user id from database if authentication id used i.e. limit guests and each authenticated user separately. }));
Another way it to implement Yiisoft\Yii\RateLimiter\Policy\LimitPolicyInterface
and use it in a similar way as above.
Implementing your own counter storage
There are two ready to use counter storages available in the package:
\Yiisoft\Yii\RateLimiter\Storage\SimpleCacheStorage
- stores counters in any PSR-16 cache.\Yiisoft\Yii\RateLimiter\Storage\ApcuStorage
- stores counters by using the APCu PHP extension while taking concurrency into account.
To use your own storage implement Yiisoft\Yii\RateLimiter\Storage\StorageInterface
.
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Rate Limiter Middleware is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.