-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrequencyRandomThrottler.php
37 lines (30 loc) · 1.11 KB
/
FrequencyRandomThrottler.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
<?php
declare(strict_types=1);
namespace Orangesoft\Throttler;
use Orangesoft\Throttler\Collection\CollectionInterface;
use Orangesoft\Throttler\Collection\NodeInterface;
use Orangesoft\Throttler\Collection\Sort\Desc;
final class FrequencyRandomThrottler implements ThrottlerInterface
{
public function __construct(
private float $threshold = 0.2,
private float $frequency = 0.8,
) {
}
/**
* @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
}
$sorted = $collection->sort(new Desc());
$total = \count($sorted);
$lowerKey = (int) ceil($this->threshold * $total);
$higherKey = $lowerKey + (1 < $total ? 1 : 0);
$probability = mt_rand() / mt_getrandmax();
$key = $this->frequency >= $probability ? mt_rand(1, $lowerKey) : mt_rand($higherKey, $total);
return $sorted->get($key - 1);
}
}