phapi/middleware-rate-limit
最新稳定版本:1.0.0
Composer 安装命令:
composer require phapi/middleware-rate-limit
包简介
Middleware handling rate limiting
README 文档
README
The rate limit middleware handles how many times a client are allowed to do requests to endpoints. Different settings can be set to different endpoints so it is possible that a client are allowed to do more requests to one endpoint than another endpoint. Please note that, depending on the configuration, this middleware uses different counters for each endpoint and client. So if a client hits the limit on one endpoint might not stop the client from doing request to other endpoints.
Installation
This middleware is not included by default in the Phapi Framework but if you need to install it it's available to install via Packagist and Composer.
$ php composer.phar require phapi/middleware-rate-limit:1.*
Configuration
There are a few settings that needs to be configured before the rate limit middleware works.
The rate limit middleware requires a cache, for example Memcache.
The middleware constructor needs four parameters, the name of the header that should be used as the identifier for each client, rate limit buckets, cache and the name of the request attribute that the route middleware used to store the matched endpoint.
The unique identifier can for example be a Client-ID or something else that is used for identification. This is usually the same header that's used for authentication.
The rate limit buckets are essentially settings on a endpoint level. The \Phapi\Middleware\RateLimit\Bucket() constructor takes four parameters:
- Total number of tokens in bucket (default: 800),
- Number of new tokens that should be added within a time window (default: 400),
- Time window in number of seconds (default: 60),
- If new tokens should be added continuously (true) or if the time window needs to pass (false) before new tokens are added (default: true).
A default bucket is required. This bucket will act as a fallback if the requested endpoint does not have an own bucket. Example:
<?php // config rate limit middleware resources $rateLimitBuckets = [ 'default' => new \Phapi\Middleware\RateLimit\Bucket(), '\\Phapi\\Endpoint\\Page' => new \Phapi\Middleware\RateLimit\Bucket(600, 60, 10, false), ]; // Add Middleware $pipeline->pipe(new \Phapi\Middleware\RateLimit( 'Client-ID', $rateLimitBuckets, $container['cache'], // Get the configured cache $requestAttribName = 'routeEndpoint' // The name of the attribute that the route middleware used to store the matched endpoint. ));
The $requestAttribName doesn't need to be used if you use the route middleware and you use it's default configuration.
Exceptions
The middleware throws \Phapi\Exception\InternalServerError when:
- no default bucket is configured
- when it's unable to find the matched endpoint (check $requestAttribName)
- no cache is configured
It also throws \Phapi\Exception\TooManyRequests when the counter hits zero and the client has run out of tokens.
Phapi
This middleware is a Phapi package used by the Phapi Framework. The middleware are also PSR-7 compliant and implements the Phapi Middleware Contract.
License
Rate Limit Middleware is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.
phapi/middleware-rate-limit 适用场景与选型建议
phapi/middleware-rate-limit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rate limit」 「middleware」 「limit」 「Rate」 「phapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phapi/middleware-rate-limit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phapi/middleware-rate-limit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phapi/middleware-rate-limit 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-14