RoundRobin.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Elastic Transport
  4. *
  5. * @link https://github.com/elastic/elastic-transport-php
  6. * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
  7. * @license https://opensource.org/licenses/MIT MIT License
  8. *
  9. * Licensed to Elasticsearch B.V under one or more agreements.
  10. * Elasticsearch B.V licenses this file to you under the MIT License.
  11. * See the LICENSE file in the project root for more information.
  12. */
  13. declare(strict_types=1);
  14. namespace Elastic\Transport\NodePool\Selector;
  15. use Elastic\Transport\NodePool\Node;
  16. use Elastic\Transport\Exception\NoNodeAvailableException;
  17. class RoundRobin implements SelectorInterface
  18. {
  19. use SelectorTrait;
  20. public function nextNode(): Node
  21. {
  22. if (empty($this->getNodes())) {
  23. $className = substr(__CLASS__, strrpos(__CLASS__, '\\') + 1);
  24. throw new NoNodeAvailableException(sprintf(
  25. "No node available. Please use %s::setNodes() before calling %s::nextNode().",
  26. $className,
  27. $className
  28. ));
  29. }
  30. $node = current($this->nodes);
  31. if (false === next($this->nodes)) {
  32. reset($this->nodes);
  33. }
  34. return $node;
  35. }
  36. }