定制 sunsgne/rate-limit 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sunsgne/rate-limit

Composer 安装命令:

composer require sunsgne/rate-limit

包简介

limit

README 文档

README

sunsgne

sunsgne/rate-limit

🐬 A PHP implementation of current limiting middleware for webman service HTTP-API 🐬

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

webman 服务限流

基于illuminate/redis 的令牌桶限流中间件

安装

composer require sunsgne/rate-limit

使用

webman中使用

$moreRateLimit = Container::get(MoreRateLimitHandler::class);

$capacity       =  60;
$seconds        =  60;
$customerHandle = [
    'class'       => \support\Response::class,
    'constructor' => [
        429,
        array(),
        json_encode(['success' => false, 'msg' => '请求次数太频繁'], 256),
    ],
];


if (false === $moreRateLimit->handle(intval($capacity), intval($seconds))) {
    $newClass = $customerHandle['class'];
    return new $newClass(... \array_values($customerHandle['constructor']));
}
return $handler($request);

直接使用

$moreRateLimit = new \sunsgne\middleware\MoreRateLimit();
$capacity       =  60;
$seconds        =  60;
$customerHandle = [
    'class'       => \support\Response::class,
    'constructor' => [
        429,
        array(),
        json_encode(['success' => false, 'msg' => '请求次数太频繁'], 256),
    ],
];


if (false === $moreRateLimit->handle(intval($capacity), intval($seconds))) {
    $newClass = $customerHandle['class'];
    return new $newClass(... \array_values($customerHandle['constructor']));
}

参数示例

参数名 示例 说明
capacity 60 “桶”可以容纳的请求数
seconds 60 “桶”完全重新装满所需的时间
concurrency true 开启并发限流
cost 1 “桶”此操作使用的令牌数

测试说明

不含数据库查询

进程数24,moreRateLimit,无业务逻辑

并发数 平均值(ms) 中位数(ms) 最小值(ms) 最大值(ms) 吞吐量
101 13 5 4 215 101.1
1001 241 244 49 428 952.4
10001 1600 2140 0 3249 1643.0

进程数24,RateLimit,无业务逻辑

并发数 平均值(ms) 中位数(ms) 最小值(ms) 最大值(ms) 吞吐量
101 7.0 7 6 11 104.1
1001 18 15 4.0 123 983.4
10001 1820 2359 0 3240 1793.0

数据库查询

进程数24,moreRateLimit

并发数 平均值(ms) 中位数(ms) 最小值(ms) 最大值(ms) 吞吐量
101 351 302 4 1019 80.1

进程数24,RateLimit

并发数 平均值(ms) 中位数(ms) 最小值(ms) 最大值(ms) 吞吐量
101 224 51 10 1004 70.1

更新记录

1.1.3 - 2022-07-06

新增
  • redis 使用 lua 脚本加锁

sunsgne/rate-limit 适用场景与选型建议

sunsgne/rate-limit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sunsgne/rate-limit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sunsgne/rate-limit 我们能提供哪些服务?
定制开发 / 二次开发

基于 sunsgne/rate-limit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-18