sunsgne/rate-limit

1.1.2 2022-07-06 05:05 UTC

This package is auto-updated.

Last update: 2024-09-06 09:19:17 UTC


README

sunsgne

sunsgne/rate-limit

🐬 PHP 实现的 webman 服务 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']));
}

参数示例

测试说明

不包含数据库查询

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

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

数据库查询

进程数24,moreRateLimit

进程数24,RateLimit

更新记录

1.1.3 - 2022-07-06

新增
  • redis 使用 lua 脚本加锁