-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
RateLimitAnnotationAspect.php
139 lines (120 loc) 路 3.97 KB
/
RateLimitAnnotationAspect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\RateLimit\Aspect;
use bandwidthThrottle\tokenBucket\storage\StorageException;
use bandwidthThrottle\tokenBucket\TokenBucket;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AroundInterface;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\RateLimit\Annotation\RateLimit;
use Hyperf\RateLimit\Exception\RateLimitException;
use Hyperf\RateLimit\Handler\RateLimitHandler;
use Swoole\Coroutine;
/**
* @Aspect
*/
class RateLimitAnnotationAspect implements AroundInterface
{
public $classes = [];
public $annotations = [
RateLimit::class,
];
/**
* @var array
*/
private $annotationProperty;
/**
* @var array
*/
private $config;
/**
* @var RequestInterface
*/
private $request;
/**
* @var RateLimitHandler
*/
private $rateLimitHandler;
/**
* @var array<string, TokenBucket>
*/
private $buckets = [];
public function __construct(ConfigInterface $config, RequestInterface $request, RateLimitHandler $rateLimitHandler)
{
$this->annotationProperty = get_object_vars(new RateLimit());
$this->config = $config->get('rate-limit', []);
$this->request = $request;
$this->rateLimitHandler = $rateLimitHandler;
}
/**
* @throws RateLimitException limit but without handle
* @throws StorageException when the storage driver bootstrap failed
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$annotation = $this->getWeightingAnnotation($this->getAnnotations($proceedingJoinPoint));
$bucketKey = $annotation->key;
if (is_callable($bucketKey)) {
$bucketKey = $bucketKey($proceedingJoinPoint);
}
if (! $bucketKey) {
$bucketKey = $this->request->getUri()->getPath();
}
if (isset($this->buckets[$bucketKey])) {
$bucket = $this->buckets[$bucketKey];
} else {
$bucket = $this->rateLimitHandler->build($bucketKey, $annotation->create, $annotation->capacity, $annotation->waitTimeout);
$this->buckets[$bucketKey] = $bucket;
}
$maxTime = microtime(true) + $annotation->waitTimeout;
$seconds = 0;
while (true) {
try {
if ($bucket->consume($annotation->consume ?? 1, $seconds)) {
return $proceedingJoinPoint->process();
}
} catch (StorageException $exception) {
}
if (microtime(true) + $seconds > $maxTime) {
break;
}
Coroutine::sleep($seconds > 0.001 ? $seconds : 0.001);
}
if (! $annotation->limitCallback || ! is_callable($annotation->limitCallback)) {
throw new RateLimitException('Service Unavailable.', 503);
}
return call_user_func($annotation->limitCallback, $seconds, $proceedingJoinPoint);
}
/**
* @param RateLimit[] $annotations
*/
public function getWeightingAnnotation(array $annotations): RateLimit
{
$property = array_merge($this->annotationProperty, $this->config);
foreach ($annotations as $annotation) {
if (! $annotation) {
continue;
}
$property = array_merge($property, array_filter(get_object_vars($annotation)));
}
return new RateLimit($property);
}
public function getAnnotations(ProceedingJoinPoint $proceedingJoinPoint): array
{
$metadata = $proceedingJoinPoint->getAnnotationMetadata();
return [
$metadata->class[RateLimit::class] ?? null,
$metadata->method[RateLimit::class] ?? null,
];
}
}