yansongda/rate-limit-bundle
Composer 安装命令:
composer require yansongda/rate-limit-bundle
包简介
symfony 限流
README 文档
README
主要提供了路由限流功能,限流的参数可由自定义函数动态进行控制
运行环境
- php 7.1+
- symfony 4
- composer
- redis
功能
- 静态限流
- 动态限流
- 根据 ip + 路由 进行限制
安装
composer require yansongda/rate-limit-bundle -vvv
概要
根据 IP + 路由名 进行确定特定的客户端,通过 redis 进行限流记录。
使用
静态限流
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; use Yansongda\RateLimitBundle\Annotation\Throttle; class HomeController extends AbstractController { /** * 静态限流: 同一个 IP 访问 test 路由,60 秒内只能访问 2 次. * * @author yansongda <me@yansongda.cn> * * @Route("/test", name="test") * @Throttle(limit=2, period=60) * * @return mixed */ public function testAction() { return JsonResponse::create(['code' => 0]); } }
动态限流
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; use Yansongda\RateLimitBundle\Annotation\Throttle; class HomeController extends AbstractController { /** * 动态限流:具体的 limit 及 period 参数由 custom 返回. * * @author yansongda <me@yansongda.cn> * * @Route("/test", name="token") * @Throttle(limit=2, period=60, custom={"App\Throttles\CustomLimitPeriod", "token"}) * * @return mixed */ public function tokenAction() { return JsonResponse::create(['code' => 0]); } }
<?php namespace App\Throttles; use Symfony\Component\HttpFoundation\Request; class CustomLimitPeriod { /** * 自定义限流策略. * * @author yansongda <me@yansongda.cn> * @param Request $request 此参数回调时自动载入 * * @return array */ public function token(Request $request) { // 返回的数据中,第一个为 limit,第二个为 period,必须为 int 类型。如果 limit 返回 -1 则无限制 return [20, 60]; } }
配置
下面是默认的配置信息。如果需要更改,在 config 的 packages 目录下新建一个 yml 文件,然后复制以下内容更改即可。
yansongda_rate_limit: # 是否开启 enable: true # snc_redis 客户端 redis_client: default # 是否展示 headers display_headers: true # headers 的 key headers: limit: X-RateLimit-Limit remaining: X-RateLimit-Remaining reset: X-RateLimit-Reset # response 内容,如果 exception 不为 null,则默认抛出写入的 exception response: message: 'Out Of Limit' code: 429 exception: null
yansongda/rate-limit-bundle 适用场景与选型建议
yansongda/rate-limit-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「throttle」 「rate-limit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yansongda/rate-limit-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yansongda/rate-limit-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yansongda/rate-limit-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
hyperf cache counter rate limiter
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-10