2019-05-15 11:12:49 +08:00
|
|
|
# 令牌桶限流器
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
## Installation
|
2019-05-15 11:12:49 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
composer require hyperf/rate-limit
|
|
|
|
```
|
|
|
|
## 默认配置
|
|
|
|
|
|
|
|
| 配置 | 默认值 | 备注 |
|
|
|
|
|:--------------:|:------:|:-------------------:|
|
|
|
|
| create | 1 | 每秒生成令牌数 |
|
|
|
|
| consume | 1 | 每次请求消耗令牌数 |
|
|
|
|
| capacity | 2 | 令牌桶最大容量 |
|
|
|
|
| limitCallback | NULL | 触发限流时回调方法 |
|
2019-12-12 16:24:04 +08:00
|
|
|
| key | NULL | 生成令牌桶的key |
|
2019-05-15 11:27:21 +08:00
|
|
|
| waitTimeout | 3 | 排队超时时间 |
|
2019-05-15 11:12:49 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
return [
|
|
|
|
'create' => 1,
|
|
|
|
'consume' => 1,
|
|
|
|
'capacity' => 2,
|
|
|
|
'limitCallback' => null,
|
|
|
|
'key' => null,
|
|
|
|
'waitTimeout' => 3,
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|
|
|
|
## 使用限流器
|
|
|
|
|
|
|
|
组件提供 `Hyperf\RateLimit\Annotation\RateLimit` 注解,作用于类、类方法,可以覆盖配置文件。 例如,
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
|
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
|
|
|
use Hyperf\RateLimit\Annotation\RateLimit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Controller(prefix="rate-limit")
|
|
|
|
*/
|
|
|
|
class RateLimitController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @RequestMapping(path="test")
|
|
|
|
* @RateLimit(create=1, capacity=3)
|
|
|
|
*/
|
|
|
|
public function test()
|
|
|
|
{
|
|
|
|
return ["QPS 1, 峰值3"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @RequestMapping(path="test2")
|
|
|
|
* @RateLimit(create=2, consume=2, capacity=4)
|
|
|
|
*/
|
|
|
|
public function test2()
|
|
|
|
{
|
|
|
|
return ["QPS 2, 峰值2"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2019-06-20 11:31:26 +08:00
|
|
|
配置优先级 `方法注解 > 类注解 > 配置文件 > 默认配置`
|
2019-05-15 11:12:49 +08:00
|
|
|
|
|
|
|
## 触发限流
|
2019-05-27 16:05:00 +08:00
|
|
|
当限流被触发时, 默认会抛出 `Hyperf\RateLimit\Exception\RateLimitException` 异常
|
2019-05-15 11:12:49 +08:00
|
|
|
|
2020-06-22 09:50:09 +08:00
|
|
|
可以通过[异常处理](en/exception-handler.md)或者配置 `limitCallback` 限流回调处理。
|
2019-06-20 11:31:26 +08:00
|
|
|
|
|
|
|
例如:
|
2019-05-15 11:12:49 +08:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
|
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
|
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
|
|
|
use Hyperf\RateLimit\Annotation\RateLimit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Controller(prefix="rate-limit")
|
2019-12-12 16:24:04 +08:00
|
|
|
* @RateLimit(limitCallback={RateLimitController::class, 'limitCallback'})
|
2019-05-15 11:12:49 +08:00
|
|
|
*/
|
|
|
|
class RateLimitController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @RequestMapping(path="test")
|
|
|
|
* @RateLimit(create=1, capacity=3)
|
|
|
|
*/
|
|
|
|
public function test()
|
|
|
|
{
|
|
|
|
return ["QPS 1, 峰值3"];
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:31:26 +08:00
|
|
|
public static function limitCallback(float $seconds, ProceedingJoinPoint $proceedingJoinPoint)
|
2019-05-15 11:12:49 +08:00
|
|
|
{
|
2019-06-20 11:31:26 +08:00
|
|
|
// $seconds 下次生成Token 的间隔, 单位为秒
|
|
|
|
// $proceedingJoinPoint 此次请求执行的切入点
|
|
|
|
// 可以通过调用 `$proceedingJoinPoint->process()` 继续执行或者自行处理
|
|
|
|
return $proceedingJoinPoint->process();
|
2019-05-15 11:12:49 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 16:24:04 +08:00
|
|
|
```
|