-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedRoundRobinThrottler.php
81 lines (65 loc) · 2.63 KB
/
WeightedRoundRobinThrottler.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
<?php
declare(strict_types=1);
namespace Orangesoft\Throttler;
use Orangesoft\Throttler\Collection\CollectionInterface;
use Orangesoft\Throttler\Collection\NodeInterface;
use Orangesoft\Throttler\Counter\CounterInterface;
final class WeightedRoundRobinThrottler implements ThrottlerInterface
{
/**
* @var array<string, int>
*/
private array $gcdWeight = [];
/**
* @var array<string, int>
*/
private array $maxWeight = [];
/**
* @var array<string, int>
*/
private array $currentWeight = [];
public function __construct(
private CounterInterface $counter,
) {
}
/**
* @param array<string, mixed> $context
*/
public function pick(CollectionInterface $collection, array $context = []): NodeInterface
{
if ($collection->isEmpty()) {
throw new \RuntimeException('Collection of nodes mustn\'t be empty.'); // @codeCoverageIgnore
}
if (isset($context['counter']) && !\is_string($context['counter'])) {
throw new \RuntimeException(\sprintf('The parameter "counter" must be as a string, %s given.', get_debug_type($context['counter']))); // @codeCoverageIgnore
}
$counter = $context['counter'] ?? spl_object_hash($collection);
if (!isset($this->gcdWeight[$counter]) || !isset($this->maxWeight[$counter]) || !isset($this->currentWeight[$counter])) {
$this->gcdWeight[$counter] = 0;
$this->maxWeight[$counter] = 0;
$this->currentWeight[$counter] = 0;
}
if (0 === $this->gcdWeight[$counter] || 0 === $this->maxWeight[$counter] || 0 === $this->currentWeight[$counter]) {
foreach ($collection as $node) {
if (0 === $node->getWeight()) {
throw new \RuntimeException('All nodes in the collection must be weighted.'); // @codeCoverageIgnore
}
$this->gcdWeight[$counter] = gcd($this->gcdWeight[$counter], $node->getWeight());
$this->maxWeight[$counter] = max($this->maxWeight[$counter], $node->getWeight());
}
}
while (true) {
$key = $this->counter->next($counter) % \count($collection);
if (0 == $key) {
$this->currentWeight[$counter] -= $this->gcdWeight[$counter];
if (0 >= $this->currentWeight[$counter]) {
$this->currentWeight[$counter] = $this->maxWeight[$counter];
}
}
$node = $collection->get($key);
if ($node->getWeight() >= $this->currentWeight[$counter]) {
return $node;
}
}
}
}