Random.php 723 B

123456789101112131415161718192021222324252627282930
  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 Random extends AbstractLoadBalancer
  14. {
  15. /**
  16. * Select an item via the load balancer.
  17. */
  18. public function select(array ...$parameters): Node
  19. {
  20. if (empty($this->nodes)) {
  21. throw new NoNodesAvailableException('Cannot select any node from load balancer.');
  22. }
  23. $key = array_rand($this->nodes);
  24. return $this->nodes[$key];
  25. }
  26. }