11<?php
2+
23namespace Imi \RateLimit \Aspect ;
34
4- use Imi \Aop \PointCutType ;
5- use Imi \Bean \BeanFactory ;
6- use Imi \Aop \AroundJoinPoint ;
75use Imi \Aop \Annotation \Around ;
86use Imi \Aop \Annotation \Aspect ;
97use Imi \Aop \Annotation \PointCut ;
10- use Imi \RateLimit \Annotation \WorkerLimit ;
8+ use Imi \Aop \AroundJoinPoint ;
9+ use Imi \Aop \PointCutType ;
1110use Imi \Bean \Annotation \AnnotationManager ;
11+ use Imi \Bean \BeanFactory ;
1212use Imi \RateLimit \Annotation \BlockingConsumer ;
13+ use Imi \RateLimit \Annotation \WorkerLimit ;
1314use 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}
0 commit comments