nikolaposa/rate-limit-middleware
Composer 安装命令:
composer require nikolaposa/rate-limit-middleware
包简介
PSR-15 middleware for rate limiting API or other application endpoints.
README 文档
README
PSR-15 middleware for rate limiting API or other application endpoints. Sits on top of general purpose Rate Limiter.
Installation
The preferred method of installation is via Composer. Run the following
command to install the latest version of a package and add it to your project's composer.json:
composer require nikolaposa/rate-limit-middleware
Usage
Rate Limit middleware is designed to be used per route, so that you can set up a rate limiting strategies for each individual endpoint or group of endpoints. This is accomplished through a mechanism for composing middleware known as piping.
Full example
Following examples demonstrate how RateLimitMiddleware can be used in a Mezzio-based
application, but the same principle applies to any middleware framework.
dependencies.php
use Laminas\Diactoros\Response\JsonResponse; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use RateLimit\Middleware\RateLimitMiddleware; use RateLimit\Middleware\ResolveIpAddressAsUserIdentity; use RateLimit\Middleware\ResolveUserIdentity; use RateLimit\Rate; use RateLimit\RateLimiter; use RateLimit\RedisRateLimiter; return [ 'dependencies' => [ 'invokables' => [ ResolveUserIdentity::class => ResolveIpAddressAsUserIdentity::class, ], 'factories' => [ 'RateLimit\\Strategy\\Api' => function (ContainerInterface $container) { return new RedisRateLimiter(Rate::perSecond(5), $container->get(Redis::class), 'rate_limit:api:'); }, 'RateLimit\\Strategy\\CreatePost' => function (ContainerInterface $container) { return new RedisRateLimiter(Rate::perDay(20), $container->get(Redis::class), 'rate_limit:web:post'); }, // default limit exceeded handler; anonymous class is used only for the sake // of simplicity of the example 'RateLimit\\LimitExceededRequestHandler' => function () { return new class implements RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface { return new JsonResponse(['error' => 'Too many requests']); } }; }, // rate limit middleware for different endpoints 'RateLimit\\ApiRateLimitMiddleware' => function (ContainerInterface $container) { return new RateLimitMiddleware( $container->get('RateLimiter\\Strategy\\Api'), 'api', $container->get(ResolveUserIdentity::class), $container->get('RateLimit\\LimitExceededRequestHandler') ); }, 'RateLimit\\CreatePostRateLimitMiddleware' => function (ContainerInterface $container) { return new RateLimitMiddleware( $container->get('RateLimiter\\Strategy\\CreatePost'), 'post.create', $container->get(ResolveUserIdentity::class), $container->get('RateLimit\\LimitExceededRequestHandler') ); }, ], ], ];
index.php
$app->get('/', App\Handler\HomePageHandler::class, 'home'); $app->get('/posts', [ App\Handler\ListPostsHandler::class, ], 'post.list'); $app->post('/posts', [ 'RateLimit\\CreatePostRateLimitMiddleware', App\Handler\CreatePostHandler::class, ], 'post.create'); $app->put('/posts/:id', App\Handler\UpdatePostHandler::class, 'post.edit'); $app->route('/api/resource[/{id:[a-f0-9]{32}}]', [ AuthenticationMiddleware::class, 'RateLimit\\ApiRateLimitMiddleware', ApiResource::class, ], ['GET', 'POST', 'PATCH', 'DELETE'], 'api-resource');
Credits
License
Released under MIT License - see the License File for details.
nikolaposa/rate-limit-middleware 适用场景与选型建议
nikolaposa/rate-limit-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.12k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2020 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rate limit」 「middleware」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nikolaposa/rate-limit-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nikolaposa/rate-limit-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nikolaposa/rate-limit-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Rapid pagination without using OFFSET
Limit number of records that can be added to a SilverStripe GridField
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Magento 2 Discount Amount Limiter
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
统计信息
- 总下载量: 13.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-22