RoundRobin.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. require_once '../vendor/elastic/transport/src/NodePool/Selector/SelectorInterface.php';
  18. require_once '../vendor/elastic/transport/src/NodePool/Selector/SelectorTrait.php';
  19. class RoundRobin implements SelectorInterface
  20. {
  21. use SelectorTrait;
  22. public function nextNode(): Node
  23. {
  24. if (empty($this->getNodes())) {
  25. $className = substr(__CLASS__, strrpos(__CLASS__, '\\') + 1);
  26. throw new NoNodeAvailableException(sprintf(
  27. "No node available. Please use %s::setNodes() before calling %s::nextNode().",
  28. $className,
  29. $className
  30. ));
  31. }
  32. $node = current($this->nodes);
  33. if (false === next($this->nodes)) {
  34. reset($this->nodes);
  35. }
  36. return $node;
  37. }
  38. }