Skip to content

Commit 689639d

Browse files
committed
对请求方式进行精细化限流
1 parent 78d0d1b commit 689639d

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/RateLimiter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class RateLimiter
2424
public static function traffic()
2525
{
2626
$config = config('plugin.tinywan.limit-traffic.app.limit');
27+
// 新增:判断当前请求方法是否在配置的限流方法内
28+
if (isset($config['limit_method']) && is_array($config['limit_method'])) {
29+
if (!in_array(strtoupper(request()->method()), $config['limit_method'])) {
30+
// 当前请求方法不需要限流,直接返回 false 表示不过限
31+
return false;
32+
}
33+
}
2734
$scriptSha = Redis::get(self::LIMIT_TRAFFIC_SCRIPT_SHA);
2835
if (!$scriptSha) {
2936
$script = <<<luascript

src/config/plugin/tinywan/limit-traffic/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'enable' => true,
55
'limit' => [
66
'limit_key' => 'none', // none:以ip地址为限流键,'get.id','header.X-Token','post.user_id',也可以自定义限流键
7+
'limit_method' => ['GET'], // 只限 GET
78
'limit' => 10, // 请求次数
89
'window_time' => 60, // 窗口时间,单位:秒
910
'status' => 429, // HTTP 状态码

0 commit comments

Comments
 (0)