WeightedRoundRobin.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use MathPHP\Algebra;
  14. class WeightedRoundRobin extends AbstractLoadBalancer
  15. {
  16. private int $lastNode = 0;
  17. private int $currentWeight = 0;
  18. /**
  19. * Select an item via the load balancer.
  20. */
  21. public function select(array ...$parameters): Node
  22. {
  23. $count = count($this->nodes);
  24. if ($count <= 0) {
  25. throw new NoNodesAvailableException('Cannot select any node from load balancer.');
  26. }
  27. $maxWeight = $this->maxWeight($this->nodes);
  28. while (true) {
  29. $this->lastNode = ($this->lastNode + 1) % $count;
  30. if ($this->lastNode === 0) {
  31. $this->currentWeight = $this->currentWeight - $this->gcd($this->nodes);
  32. if ($this->currentWeight <= 0) {
  33. $this->currentWeight = $maxWeight;
  34. if ($this->currentWeight == 0) {
  35. // Degrade to random algorithm.
  36. return $this->nodes[array_rand($this->nodes)];
  37. }
  38. }
  39. }
  40. /** @var Node $node */
  41. $node = $this->nodes[$this->lastNode];
  42. if ($node->weight >= $this->currentWeight) {
  43. return $node;
  44. }
  45. }
  46. }
  47. /**
  48. * Calculate the max weight of nodes.
  49. */
  50. private function maxWeight(iterable $nodes): int
  51. {
  52. $max = null;
  53. foreach ($nodes as $node) {
  54. if (! $node instanceof Node) {
  55. continue;
  56. }
  57. if ($max === null) {
  58. $max = $node->weight;
  59. } else {
  60. $max = max($max, $node->weight);
  61. }
  62. }
  63. return $max;
  64. }
  65. /**
  66. * Calculate the gcd of nodes.
  67. */
  68. private function gcd(iterable $nodes): int
  69. {
  70. $x = $y = null;
  71. foreach ($nodes as $node) {
  72. if (! $node instanceof Node) {
  73. continue;
  74. }
  75. if ($x === null) {
  76. $x = $node->weight;
  77. continue;
  78. }
  79. $y = $node->weight;
  80. $x = Algebra::gcd($x, $y);
  81. }
  82. return $x;
  83. }
  84. }