tal7aouy/api-rate-limiter
最新稳定版本:v1.0.0
Composer 安装命令:
composer require tal7aouy/api-rate-limiter
包简介
API Rate Limiting Library for PHP
README 文档
README
This library provides rate limiting functionality for PHP applications, using Redis for storage. It supports multiple rate limiting strategies and can be integrated with PSR-15 middleware.
Installation
To use the library, you need to have the following Composer packages, which are already included in the library's composer.json file:
predis/predis- A Redis client library.laminas/laminas-diactoros- Provides PSR-7 (HTTP message) and PSR-15 (HTTP server) implementations.psr/http-server-handler- Provides theRequestHandlerInterfacefor handling HTTP requests.psr/http-server-middleware- Provides theMiddlewareInterfacefor HTTP middleware.laminas/laminas-httphandlerrunner- Provides theSapiEmitterclass for emitting HTTP responses.
In addition, for development and testing, the library includes:
phpstan/phpstan- For static analysis.pestphp/pest- For testing.
Install Library and Dependencies
Run the following Composer command to install all required packages:
composer require tal7aouy/api-rate-limiter
Ensure Redis is installed and running on your system.
Usage
Basic Setup
-
Initialize Redis Connection:
Create a
RedisConnectioninstance to handle the connection to the Redis server.use Tal7aouy\ApiRateLimiter\RedisConnection; $redisConnection = new RedisConnection();
-
Initialize Redis Rate Limiter:
Create a
RedisRateLimiterinstance using the Redis connection.use Tal7aouy\ApiRateLimiter\RedisRateLimiter; $redisRateLimiter = new RedisRateLimiter($redisConnection->getClient());
-
Initialize Rate Limiter Middleware:
Create a
RateLimiterMiddlewareinstance with the desired rate limit and time window.use Tal7aouy\ApiRateLimiter\RateLimiterMiddleware; $rateLimiterMiddleware = new RateLimiterMiddleware($redisRateLimiter, 10, 60); // Limit: 10 requests per minute
-
Create a Request Handler:
Implement a simple request handler to handle incoming requests.
use Laminas\Diactoros\Response; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class SimpleHandler implements RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface { $response = new Response(); $response->getBody()->write('Hello, World!'); return $response; } }
-
Create Server Request and Process Through Middleware:
Create a server request and process it through the rate limiter middleware.
use Laminas\Diactoros\ServerRequestFactory; $serverParams = ['REMOTE_ADDR' => '127.0.0.1']; $request = ServerRequestFactory::fromGlobals($serverParams, [], [], [], []); $response = $rateLimiterMiddleware->process($request, new SimpleHandler()); // Emit Response use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; $emitter = new SapiEmitter(); $emitter->emit($response);
Advanced Configuration
Custom Redis Configuration
You can configure the Redis connection by passing a custom configuration array to the RedisConnection class.
$redisConnection = new RedisConnection([ 'scheme' => 'tcp', 'host' => 'localhost', 'port' => 6379, ]);
Custom Rate Limiting Strategies
The library supports multiple rate limiting strategies. You can implement your own strategy by extending the RateLimiter interface.
use Tal7aouy\ApiRateLimiter\RateLimiter; class CustomRateLimiter implements RateLimiter { public function isAllowed(string $identifier, int $limit, int $window): bool { // Custom rate limiting logic } }
Then use the custom rate limiter with the middleware.
$customRateLimiter = new CustomRateLimiter(); $rateLimiterMiddleware = new RateLimiterMiddleware($customRateLimiter, 10, 60);
Testing
Unit tests are provided for each rate limiting strategy and Redis integration. To run the tests, use PestPHP.
composer test
Documentation
For detailed documentation and examples, please refer to the source code and comments within the library.
In the examples/index.php file, you can see a complete example of how to use the library.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.
License
This software licensed under the MIT license.
tal7aouy/api-rate-limiter 适用场景与选型建议
tal7aouy/api-rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tal7aouy/api-rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tal7aouy/api-rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-30