RoundRobin.php 822 B

12345678910111213141516171819202122232425262728293031323334
  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 RoundRobin extends AbstractLoadBalancer
  14. {
  15. private static int $current = 0;
  16. /**
  17. * Select an item via the load balancer.
  18. */
  19. public function select(array ...$parameters): Node
  20. {
  21. $count = count($this->nodes);
  22. if ($count <= 0) {
  23. throw new NoNodesAvailableException('Cannot select any node from load balancer.');
  24. }
  25. $item = $this->nodes[self::$current % $count];
  26. ++self::$current;
  27. return $item;
  28. }
  29. }