WeightedRandom.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\LoadBalancer;
  12. use Hyperf\LoadBalancer\Exception\NoNodesAvailableException;
  13. class WeightedRandom extends AbstractLoadBalancer
  14. {
  15. /**
  16. * Select an item via the load balancer.
  17. */
  18. public function select(array ...$parameters): Node
  19. {
  20. $totalWeight = 0;
  21. $isSameWeight = true;
  22. $lastWeight = null;
  23. $nodes = $this->nodes;
  24. foreach ($nodes as $node) {
  25. if (! $node instanceof Node) {
  26. continue;
  27. }
  28. $weight = $node->weight;
  29. $totalWeight += $weight;
  30. if ($lastWeight !== null && $isSameWeight && $weight !== $lastWeight) {
  31. $isSameWeight = false;
  32. }
  33. $lastWeight = $weight;
  34. }
  35. if ($totalWeight > 0 && ! $isSameWeight) {
  36. $offset = mt_rand(0, $totalWeight - 1);
  37. foreach ($nodes as $node) {
  38. $offset -= $node->weight;
  39. if ($offset < 0) {
  40. return $node;
  41. }
  42. }
  43. }
  44. if (! $nodes) {
  45. throw new NoNodesAvailableException('Cannot select any node from load balancer.');
  46. }
  47. return $nodes[array_rand($nodes)];
  48. }
  49. }