nasustop/hapi-rate-limit
最新稳定版本:3.0.0
Composer 安装命令:
composer require nasustop/hapi-rate-limit
包简介
README 文档
README
hyperf的限流组件
安装
composer require nasustop/hapi-rate-limit
使用方法
// 进程级限流,每个进程独立的限流服务,受进程重启影响
use Nasustop\HapiRateLimit\Rate\ProcessTokenBucket;
// 服务级限流,多进程共享限流服务,受服务重启影响【推荐使用】
use Nasustop\HapiRateLimit\Rate\MemoryTokenBucket;
// 应用级限流,多服务共享限流服务,不受服务重启影响,但受限于redis服务的性能【推荐使用】
use Nasustop\HapiRateLimit\Rate\RedisTokenBucket;
$processRate = new ProcessTokenBucket(1000, 1000, 1);
if (! $processRate->getToken(1)) {
$response = ApplicationContext::getContainer()->get(ResponseInterface::class);
return $response->json([
'code' => 429,
'msg' => 'To Many Requests.',
]);
}
$memoryRate = new MemoryTokenBucket(1000, 1000, 1);
if (! $memoryRate->getToken(1)) {
$response = ApplicationContext::getContainer()->get(ResponseInterface::class);
return $response->json([
'code' => 429,
'msg' => 'To Many Requests.',
]);
}
$redisRate = new RedisTokenBucket(
redis: ApplicationContext::getContainer()->get(RedisFactory::class)->get('default'),
key: 'hapi_rate_limit',
capacity: 1000,
rate: 1000,
interval: 1
);
if (! $redisRate->getToken(1)) {
$response = ApplicationContext::getContainer()->get(ResponseInterface::class);
return $response->json([
'code' => 429,
'msg' => 'To Many Requests.',
]);
}
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-27