1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\GuzzleHttp\Middleware; |
4
|
|
|
|
5
|
|
|
use BenTools\GuzzleHttp\Middleware\Storage\Adapter\ArrayAdapter; |
6
|
|
|
use BenTools\GuzzleHttp\Middleware\Storage\Counter; |
7
|
|
|
use BenTools\GuzzleHttp\Middleware\Storage\ThrottleStorageInterface; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
class ThrottleMiddleware |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ThrottleConfiguration[] |
14
|
|
|
*/ |
15
|
|
|
private $configurations = []; |
16
|
|
|
/** |
17
|
|
|
* @var ThrottleStorageInterface |
18
|
|
|
*/ |
19
|
|
|
private $storage; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ThrottleMiddleware constructor. |
23
|
|
|
* @param ThrottleStorageInterface $storage |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ThrottleStorageInterface $storage = null) |
26
|
|
|
{ |
27
|
|
|
$this->storage = $storage ?? new ArrayAdapter(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ThrottleConfiguration $configuration |
32
|
|
|
*/ |
33
|
|
|
public function registerConfiguration(ThrottleConfiguration $configuration) |
34
|
|
|
{ |
35
|
|
|
$this->configurations[$configuration->getStorageKey()] = $configuration; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function __invoke(callable $handler) |
40
|
|
|
{ |
41
|
|
|
return function (RequestInterface $request, array $options) use ($handler) { |
42
|
|
|
foreach ($this->configurations as $configuration) { |
43
|
|
|
if ($configuration->matchRequest($request)) { |
44
|
|
|
$this->processConfiguration($configuration); |
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
return $handler($request, $options); |
49
|
|
|
}; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function processConfiguration(ThrottleConfiguration $configuration) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$counter = $this->storage->getCounter($configuration->getStorageKey()); |
56
|
|
|
} catch (\TypeError $e) { |
|
|
|
|
57
|
|
|
$counter = new Counter($configuration->getDuration()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$counter->isExpired()) { |
61
|
|
|
if ($counter->count() >= $configuration->getMaxRequests()) { |
62
|
|
|
$microDuration = $configuration->getDuration() * 1000000; |
63
|
|
|
usleep(random_int(min(50000, $microDuration), ($microDuration))); // Add some randomness to help shared storage |
64
|
|
|
$this->processConfiguration($configuration); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
$counter->reset(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$counter->increment(); |
72
|
|
|
$this->storage->saveCounter($configuration->getStorageKey(), $counter, $configuration->getDuration()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.