sunsgne / rate-limit
限制
1.1.2
2022-07-06 05:05 UTC
Requires
- php: >=7.4
- ext-json: *
- illuminate/redis: ^8.2.0
- php-di/php-di: ^6.3
Requires (Dev)
- phpunit/phpunit: ~7.0|~8.0|~9.0
- workerman/webman: ^1.0
README
sunsgne/rate-limit
🐬 PHP 实现的 webman 服务 HTTP-API 当前限流中间件 🐬
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'])); }
参数示例
测试说明
不包含数据库查询
进程数
24
,moreRateLimit
,无业务逻辑
进程数
24
,RateLimit
,无业务逻辑
数据库查询
进程数
24
,moreRateLimit
进程数
24
,RateLimit
更新记录
1.1.3 - 2022-07-06
新增
- redis 使用
lua
脚本加锁