2021-04-18 17:12:52 +08:00
# Token bucket rate limiter
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
```
2021-04-18 17:12:52 +08:00
## Configuration
2019-05-15 11:12:49 +08:00
2021-04-18 17:12:52 +08:00
### Publish config
2019-05-15 11:12:49 +08:00
2021-04-18 17:12:52 +08:00
```bash
php bin/hyperf.php vendor:publish hyperf/rate-limit
2019-05-15 11:12:49 +08:00
```
2021-04-18 17:12:52 +08:00
### Config description
2019-05-15 11:12:49 +08:00
2021-04-18 17:12:52 +08:00
| config item | default | remark |
|:--------------:|:-------:|:---------------------:|
| create | 1 | Number of tokens generated per second |
| consume | 1 | Number of tokens consumed per request |
| capacity | 2 | Maximum capacity of token bucket |
| limitCallback | `[]` | Callback method when current limit is triggered |
| waitTimeout | 1 | timeout in wait queue |
## Usage
The component provides `Hyperf\RateLimit\Annotation\RateLimit` annotation, which acts on classes and class methods, and can override configuration files. For example:
2019-05-15 11:12:49 +08:00
```php
< ?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\RateLimit\Annotation\RateLimit;
2022-01-13 12:09:04 +08:00
#[Controller(prefix: "rate-limit")]
2019-05-15 11:12:49 +08:00
class RateLimitController
{
2022-01-13 12:09:04 +08:00
#[RequestMapping(path: "test")]
#[RateLimit(create: 1, capacity: 3)]
2019-05-15 11:12:49 +08:00
public function test()
{
2021-04-18 17:12:52 +08:00
return ["QPS 1, Peek3"];
2019-05-15 11:12:49 +08:00
}
2022-01-13 12:09:04 +08:00
#[RequestMapping(path: "test2")]
#[RateLimit(create: 2, consume: 2, capacity: 4)]
2019-05-15 11:12:49 +08:00
public function test2()
{
2021-04-18 17:12:52 +08:00
return ["QPS 2, Peek2"];
2019-05-15 11:12:49 +08:00
}
}
```
2021-04-18 17:12:52 +08:00
Configuration priority `Method Annotation > Class Annotation > Configuration File > Default Configuration`
2019-05-15 11:12:49 +08:00
2021-04-18 17:12:52 +08:00
## Trigger current limit
When the current limit is triggered, the `Hyperf\RateLimit\Exception\RateLimitException` will be thrown by default.
2019-05-15 11:12:49 +08:00
2021-04-18 17:12:52 +08:00
You can use [Exception Handler ](en/exception-handler.md ) or configure `limitCallback` to handle the current limit callback.
2019-06-20 11:31:26 +08:00
2021-04-18 17:12:52 +08:00
For example:
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;
2022-01-13 12:09:04 +08:00
#[Controller(prefix: "rate-limit")]
#[RateLimit(limitCallback: {RateLimitController::class, "limitCallback"})]
2019-05-15 11:12:49 +08:00
class RateLimitController
{
2022-01-13 12:09:04 +08:00
#[RequestMapping(path: "test")]
#[RateLimit(create: 1, capacity: 3)]
2019-05-15 11:12:49 +08:00
public function test()
{
2021-04-18 17:12:52 +08:00
return ["QPS 1, Peek3"];
2019-05-15 11:12:49 +08:00
}
2019-06-20 11:31:26 +08:00
public static function limitCallback(float $seconds, ProceedingJoinPoint $proceedingJoinPoint)
2019-05-15 11:12:49 +08:00
{
2021-04-18 17:12:52 +08:00
// $seconds Token generation time interval, in seconds
// $proceedingJoinPoint The entry point for the execution of this request
// You can handle it by yourself, or continue its execution by calling `$proceedingJoinPoint->process()`
2019-06-20 11:31:26 +08:00
return $proceedingJoinPoint->process();
2019-05-15 11:12:49 +08:00
}
}
2021-04-18 17:12:52 +08:00
```