Skip to content

Commit 508197b

Browse files
committed
同步
1 parent c4af789 commit 508197b

File tree

8 files changed

+171
-91
lines changed

8 files changed

+171
-91
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
<?php
2+
23
namespace Imi\RateLimit\Annotation;
34

45
use Imi\Bean\Annotation\Base;
56
use Imi\Bean\Annotation\Parser;
67

78
/**
8-
* 阻塞等待消费
9-
*
9+
* 阻塞等待消费.
10+
*
1011
* 当触发限流时,自动阻塞(协程挂起)等待
11-
*
12+
*
1213
* @Annotation
1314
* @Target("METHOD")
1415
* @Parser("\Imi\Bean\Parser\NullParser")
1516
*/
1617
class BlockingConsumer extends Base
1718
{
1819
/**
19-
* 只传一个参数时的参数名
20+
* 只传一个参数时的参数名.
21+
*
2022
* @var string
2123
*/
2224
protected $defaultFieldName = 'timeout';
2325

2426
/**
25-
* 超时时间,单位:秒
26-
*
27+
* 超时时间,单位:秒.
28+
*
2729
* 为 null 不限制
2830
*
2931
* @var int|null
3032
*/
3133
public $timeout;
32-
33-
}
34+
}

src/Annotation/RateLimit.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
11
<?php
2+
23
namespace Imi\RateLimit\Annotation;
34

45
use Imi\Bean\Annotation\Base;
56
use Imi\Bean\Annotation\Parser;
67

78
/**
8-
* 限流器注解
9+
* 限流器注解.
10+
*
911
* @Annotation
1012
* @Target("METHOD")
1113
* @Parser("\Imi\Bean\Parser\NullParser")
1214
*/
1315
class RateLimit extends Base
1416
{
1517
/**
16-
* 限流器名称
18+
* 限流器名称.
1719
*
1820
* @var string
1921
*/
2022
public $name;
2123

2224
/**
23-
* 总容量
25+
* 总容量.
2426
*
2527
* @var int
2628
*/
2729
public $capacity;
2830

2931
/**
30-
* 单位时间内生成填充的数量
31-
*
32+
* 单位时间内生成填充的数量.
33+
*
3234
* 不设置或为null时,默认值与 $capacity 相同
3335
*
3436
* @var int
3537
*/
3638
public $fill;
3739

3840
/**
39-
* 单位时间,默认为:秒(second)
40-
*
41+
* 单位时间,默认为:秒(second).
42+
*
4143
* 支持:microsecond、millisecond、second、minute、hour、day、week、month、year
4244
*
4345
* @var string
4446
*/
4547
public $unit = 'second';
4648

4749
/**
48-
* 每次扣除数量,默认为1
50+
* 每次扣除数量,默认为1.
4951
*
5052
* @var int
5153
*/
5254
public $deduct = 1;
5355

5456
/**
55-
* 触发限流的回调
57+
* 触发限流的回调.
5658
*
5759
* @var callable
5860
*/
5961
public $callback;
6062

6163
/**
62-
* 连接池名称,留空取默认 redis 连接池
64+
* 连接池名称,留空取默认 redis 连接池.
6365
*
6466
* @var string|null
6567
*/
6668
public $poolName;
67-
68-
}
69+
}

src/Annotation/WorkerLimit.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
<?php
2+
23
namespace Imi\RateLimit\Annotation;
34

45
use Imi\Bean\Annotation\Base;
56
use Imi\Bean\Annotation\Parser;
67

78
/**
8-
* 并发工作数限制注解
9+
* 并发工作数限制注解.
10+
*
911
* @Annotation
1012
* @Target("METHOD")
1113
* @Parser("\Imi\Bean\Parser\NullParser")
1214
*/
1315
class WorkerLimit extends Base
1416
{
1517
/**
16-
* 限流器名称
18+
* 限流器名称.
1719
*
1820
* @var string
1921
*/
2022
public $name;
2123

2224
/**
23-
* 最大同时工作数量
25+
* 最大同时工作数量.
2426
*
2527
* @var int
2628
*/
2729
public $max;
2830

2931
/**
30-
* 工作超时时间,单位:秒,支持小数点精确到毫秒
31-
*
32+
* 工作超时时间,单位:秒,支持小数点精确到毫秒.
33+
*
3234
* 默认为null,则不限制(不推荐不安全,有隐患)
3335
*
3436
* @var float|null
3537
*/
3638
public $timeout;
3739

3840
/**
39-
* 触发限流的回调
41+
* 触发限流的回调.
4042
*
4143
* @var callable
4244
*/
4345
public $callback;
4446

4547
/**
46-
* 连接池名称,留空取默认 redis 连接池
48+
* 连接池名称,留空取默认 redis 连接池.
4749
*
4850
* @var string|null
4951
*/
5052
public $poolName;
51-
52-
}
53+
}

src/Aspect/RateLimitAspect.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
2+
23
namespace Imi\RateLimit\Aspect;
34

4-
use Imi\Aop\PointCutType;
5-
use Imi\Bean\BeanFactory;
6-
use Imi\Aop\AroundJoinPoint;
75
use Imi\Aop\Annotation\Around;
86
use Imi\Aop\Annotation\Aspect;
9-
use Imi\RateLimit\RateLimiter;
107
use Imi\Aop\Annotation\PointCut;
11-
use Imi\RateLimit\Annotation\RateLimit;
8+
use Imi\Aop\AroundJoinPoint;
9+
use Imi\Aop\PointCutType;
1210
use Imi\Bean\Annotation\AnnotationManager;
11+
use Imi\Bean\BeanFactory;
1312
use Imi\RateLimit\Annotation\BlockingConsumer;
13+
use Imi\RateLimit\Annotation\RateLimit;
14+
use Imi\RateLimit\RateLimiter;
1415

1516
/**
1617
* @Aspect
@@ -19,30 +20,34 @@ class RateLimitAspect
1920
{
2021
/**
2122
* 处理限流
23+
*
2224
* @PointCut(
2325
* type=PointCutType::ANNOTATION,
2426
* allow={
2527
* RateLimit::class
2628
* }
2729
* )
2830
* @Around
31+
*
2932
* @return mixed
3033
*/
3134
public function parse(AroundJoinPoint $joinPoint)
3235
{
3336
$className = BeanFactory::getObjectClass($joinPoint->getTarget());
3437
$method = $joinPoint->getMethod();
38+
/** @var RateLimit|null $rateLimit */
3539
$rateLimit = AnnotationManager::getMethodAnnotations($className, $method, RateLimit::class)[0] ?? null;
40+
/** @var BlockingConsumer|null $blockingConsumer */
3641
$blockingConsumer = AnnotationManager::getMethodAnnotations($className, $method, BlockingConsumer::class)[0] ?? null;
37-
if(null === $blockingConsumer)
42+
if (null === $blockingConsumer)
3843
{
3944
$result = RateLimiter::limit($rateLimit->name, $rateLimit->capacity, $rateLimit->callback, $rateLimit->fill, $rateLimit->unit, $rateLimit->deduct, $rateLimit->poolName);
4045
}
4146
else
4247
{
4348
$result = RateLimiter::limitBlock($rateLimit->name, $rateLimit->capacity, $rateLimit->callback, $blockingConsumer->timeout, $rateLimit->fill, $rateLimit->unit, $rateLimit->deduct, $rateLimit->poolName);
4449
}
45-
if(true === $result)
50+
if (true === $result)
4651
{
4752
return $joinPoint->proceed();
4853
}

src/Aspect/WorkerLimitAspect.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
2+
23
namespace Imi\RateLimit\Aspect;
34

4-
use Imi\Aop\PointCutType;
5-
use Imi\Bean\BeanFactory;
6-
use Imi\Aop\AroundJoinPoint;
75
use Imi\Aop\Annotation\Around;
86
use Imi\Aop\Annotation\Aspect;
97
use Imi\Aop\Annotation\PointCut;
10-
use Imi\RateLimit\Annotation\WorkerLimit;
8+
use Imi\Aop\AroundJoinPoint;
9+
use Imi\Aop\PointCutType;
1110
use Imi\Bean\Annotation\AnnotationManager;
11+
use Imi\Bean\BeanFactory;
1212
use Imi\RateLimit\Annotation\BlockingConsumer;
13+
use Imi\RateLimit\Annotation\WorkerLimit;
1314
use Imi\RateLimit\WorkerLimiter;
1415

1516
/**
@@ -19,33 +20,38 @@ class WorkerLimitAspect
1920
{
2021
/**
2122
* 处理工作限流
23+
*
2224
* @PointCut(
2325
* type=PointCutType::ANNOTATION,
2426
* allow={
2527
* WorkerLimit::class
2628
* }
2729
* )
2830
* @Around
31+
*
2932
* @return mixed
3033
*/
3134
public function parse(AroundJoinPoint $joinPoint)
3235
{
3336
$className = BeanFactory::getObjectClass($joinPoint->getTarget());
3437
$method = $joinPoint->getMethod();
38+
/** @var WorkerLimit|null $workerLimit */
3539
$workerLimit = AnnotationManager::getMethodAnnotations($className, $method, WorkerLimit::class)[0] ?? null;
40+
/** @var BlockingConsumer|null $blockingConsumer */
3641
$blockingConsumer = AnnotationManager::getMethodAnnotations($className, $method, BlockingConsumer::class)[0] ?? null;
37-
if(null === $blockingConsumer)
42+
if (null === $blockingConsumer)
3843
{
39-
$result = WorkerLimiter::call(function() use($joinPoint){
44+
$result = WorkerLimiter::call(function () use ($joinPoint) {
4045
return $joinPoint->proceed();
4146
}, $workerLimit->name, $workerLimit->max, $workerLimit->timeout, $workerLimit->callback, $workerLimit->poolName);
4247
}
4348
else
4449
{
45-
$result = WorkerLimiter::callBlock(function() use($joinPoint){
50+
$result = WorkerLimiter::callBlock(function () use ($joinPoint) {
4651
return $joinPoint->proceed();
4752
}, $workerLimit->name, $workerLimit->max, $workerLimit->timeout, $blockingConsumer->timeout, $workerLimit->callback, $workerLimit->poolName);
4853
}
54+
4955
return $result;
5056
}
5157
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2+
23
namespace Imi\RateLimit\Exception;
34

45
/**
5-
* 限流异常
6+
* 限流异常.
67
*/
78
class RateLimitException extends \Exception
89
{
9-
10-
}
10+
}

0 commit comments

Comments
 (0)