Skip to content

Commit f383fe3

Browse files
committed
可对要限制的请求类型进行自定义设置
1 parent 5b646ad commit f383fe3

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
```
33
composer require topthink/think-throttle
44
```
5-
安装后会自动为项目生成 `conf/throttle.php` 配置文件,默认配置不会限制访问频率
5+
安装后会自动为项目生成 `conf/throttle.php` 配置文件,安装后组件不会自动启用,需要手动设置
66

77
### 开启
88
组件以中间件的方式进行工作,因此它的开启与其他中间件一样,例如在全局中间件中使用 `app/middleware.php` :
@@ -18,14 +18,18 @@ return [
1818
<?php
1919
// 中间件配置
2020
return [
21+
// 缓存键前缀,防止键值与其他应用冲突
22+
'prefix' => 'throttle_',
2123
// 缓存的键,true 表示使用来源ip
2224
'key' => true,
23-
// 设置访问频率,此处指的是允许每分钟请求10次。默认值 null 表示不限制
24-
'visit_rate' => '10/m',
25+
// 要被限制的请求类型, eg: GET POST PUT DELETE HEAD
26+
'visit_method' => ['GET'],
27+
// 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。值 null 表示不限制, eg: null 10/m 20/h 300/d 200/300
28+
'visit_rate' => '100/m',
2529
// 访问受限时返回的http状态码
2630
'visit_fail_code' => 429,
2731
// 访问受限时访问的文本信息
28-
'visit_fail_text' => '访问频率受到限制,请稍等__WAIT__秒再试',
32+
'visit_fail_text' => '访问频率受到限制,请稍等__WAIT__秒再试',
2933
];
3034
```
3135

@@ -36,7 +40,7 @@ return [
3640
其中 `key` 用来设置缓存键的;而 `visit_rate` 用来设置访问频率,单位可以是秒,分,时,天,例如:`1/s`, `10/m`, `98/h`, `100/d` , 也可以是 `100/600` (600 秒内最多 100 次请求)。
3741

3842
### 灵活定制
39-
示例一:针对用户个体做限制, `key` 的值可以设为函数,该函数返回新的缓存键值,例如:
43+
示例一:针对用户个体做限制, `key` 的值可以设为函数,该函数返回新的缓存键值(需要Session支持),例如:
4044
```
4145
'key' => function($throttle, $request) {
4246
$user_id = $request->session->get('user_id');
@@ -65,7 +69,11 @@ PS:此示例需要本中间件在路由中间件后启用,这样预设的替
6569
```
6670

6771
## 更新日志
68-
版本 1.1.x 的配置形式完全兼容版本 1.0.x 内容,可以无缝升级。
72+
版本 1.2.x 的配置形式完全兼容版本 1.1.x 内容,可以无缝升级。
73+
74+
### 1.2.x 更新
75+
- 可对要限制的请求类型进行自定义设置
76+
- 默认配置请求频率设置 '100/m'
6977

7078
### 1.1.x 更新
7179
- 添加漏桶限流算法, 令牌桶算法, 计数固定窗口, 滑动窗口共四种限流策略;

src/Throttle.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Throttle
2828
public static $default_config = [
2929
'prefix' => 'throttle_', // 缓存键前缀,防止键与其他应用冲突
3030
'key' => true, // 节流规则 true为自动规则
31+
'visit_method' => ['GET', 'HEAD'], // 要被限制的请求类型
3132
'visit_rate' => null, // 节流频率 null 表示不限制 eg: 10/m 20/h 300/d
3233
'visit_fail_code' => 429, // 访问受限时返回的http状态码
3334
'visit_fail_text' => 'Too Many Requests', // 访问受限时访问的文本信息
@@ -69,11 +70,16 @@ public function __construct(Cache $cache, Config $config)
6970

7071
/**
7172
* 请求是否允许
72-
* @param $request
73+
* @param Request $request
7374
* @return bool
7475
*/
7576
protected function allowRequest($request)
7677
{
78+
// 若请求类型不在限制内
79+
if (!in_array($request->method(), $this->config['visit_method'])) {
80+
return true;
81+
}
82+
7783
$key = $this->getCacheKey($request);
7884
if (null === $key) {
7985
return true;

src/config.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
'prefix' => 'throttle_',
88
// 缓存的键,true 表示使用来源ip
99
'key' => true,
10-
// 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。默认值 null 表示不限制, eg: 10/m 20/h 300/d 200/300
11-
'visit_rate' => null,
10+
// 要被限制的请求类型, eg: GET POST PUT DELETE HEAD 等
11+
'visit_method' => ['GET', 'HEAD'],
12+
// 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。值 null 表示不限制, eg: null 10/m 20/h 300/d 200/300
13+
'visit_rate' => '100/m',
1214
// 访问受限时返回的http状态码
1315
'visit_fail_code' => 429,
1416
// 访问受限时访问的文本信息

0 commit comments

Comments
 (0)