Skip to content

Commit d605f08

Browse files
committed
代码优化
1 parent dcc8f0f commit d605f08

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/Throttle.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use think\Response;
1919
use think\Session;
2020
use TypeError;
21-
use function sprintf;
2221

2322
/**
2423
* 访问频率限制中间件
@@ -27,6 +26,7 @@
2726
*/
2827
class Throttle
2928
{
29+
const WAIT = '__WAIT__';
3030
/**
3131
* 默认配置参数
3232
* @var array
@@ -38,7 +38,7 @@ class Throttle
3838
'visit_rate' => '', // 节流频率, 空字符串表示不限制 eg: '', '10/m', '20/h', '300/d'
3939
'visit_enable_show_rate_limit' => true, // 在响应体中设置速率限制的头部信息
4040
'visit_fail_code' => 429, // 访问受限时返回的 http 状态码,当没有 visit_fail_response 时生效
41-
'visit_fail_text' => 'Too many requests, try again after __WAIT__ seconds.', // 访问受限时访问的文本信息
41+
'visit_fail_text' => 'Too many requests, try again after '. self::WAIT . ' seconds.', // 访问受限时访问的文本信息
4242
'visit_fail_response' => null, // 访问受限时的响应信息闭包回调
4343
'driver_name' => CounterFixed::class, // 限流算法驱动
4444
];
@@ -175,13 +175,14 @@ protected function getCacheKey(Request $request, string|bool|Closure $key, strin
175175
if ($key === true) {
176176
$key = $request->ip();
177177
} elseif (is_string($key) && str_contains($key, '__')) {
178-
$key = str_replace(['__CONTROLLER__', '__ACTION__', '__IP__', '__SESSION__'],
178+
$key = str_replace([RateLimitAnnotation::CONTROLLER, RateLimitAnnotation::ACTION, RateLimitAnnotation::IP,
179+
RateLimitAnnotation::SESSION],
179180
[$request->controller(), $request->action(), $request->ip(), $this->session->getId()],
180181
$key);
181182
}
182183

183184
if ($annotation) {
184-
// 注解需要以实际方法作为前缀
185+
// 注解方式的需添加以实际方法作为前缀
185186
$key = $request->controller() . $request->action() . $key;
186187
}
187188

@@ -262,14 +263,14 @@ protected function allowRequestByConfig(Request $request): bool
262263
*/
263264
public function buildLimitException(int $wait_seconds, Request $request): HttpResponseException
264265
{
265-
$visitFail = $this->config['visit_fail_response'] ?? null;
266+
$visitFail = $this->config['visit_fail_response'];
266267
if ($visitFail instanceof Closure) {
267268
$response = Container::getInstance()->invokeFunction($visitFail, [$this, $request, $wait_seconds]);
268269
if (!$response instanceof Response) {
269-
throw new TypeError(sprintf('The closure must return %s instance', Response::class));
270+
throw new TypeError('The closure must return ' . Response::class . ' instance');
270271
}
271272
} else {
272-
$content = str_replace('__WAIT__', (string)$wait_seconds, $this->getFailMessage());
273+
$content = str_replace(self::WAIT, (string)$wait_seconds, $this->getFailMessage());
273274
$response = Response::create($content)->code($this->config['visit_fail_code']);
274275
}
275276
if ($this->config['visit_enable_show_rate_limit']) {

src/annotation/RateLimit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class RateLimit
1313
const AUTO = true;
1414
const IP = '__IP__';
1515
const SESSION = '__SESSION__';
16+
const CONTROLLER = '__CONTROLLER__';
17+
const ACTION = '__ACTION__';
1618

1719
public function __construct(public string $rate,
1820
public string|bool|Closure $key = RateLimit::AUTO,

src/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'visit_enable_show_rate_limit' => true,
2929
// 访问受限时返回的响应
3030
'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds) {
31-
$content = str_replace('__WAIT__', (string)$wait_seconds, $throttle->getFailMessage());
31+
$content = str_replace(Throttle::WAIT, (string)$wait_seconds, $throttle->getFailMessage());
3232
return Response::create($content)->code(429);
3333
},
3434
];

0 commit comments

Comments
 (0)