RoundRobinSelector.php 946 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Elasticsearch\ConnectionPool\Selectors;
  3. use Elasticsearch\Connections\ConnectionInterface;
  4. /**
  5. * Class RoundRobinSelector
  6. *
  7. * @category Elasticsearch
  8. * @package Elasticsearch\ConnectionPool\Selectors\ConnectionPool
  9. * @author Zachary Tong <zach@elastic.co>
  10. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  11. * @link http://elastic.co
  12. */
  13. class RoundRobinSelector implements SelectorInterface
  14. {
  15. /**
  16. * @var int
  17. */
  18. private $current = 0;
  19. /**
  20. * Select the next connection in the sequence
  21. *
  22. * @param ConnectionInterface[] $connections an array of ConnectionInterface instances to choose from
  23. *
  24. * @return \Elasticsearch\Connections\ConnectionInterface
  25. */
  26. public function select($connections)
  27. {
  28. $returnConnection = $connections[$this->current % count($connections)];
  29. $this->current += 1;
  30. return $returnConnection;
  31. }
  32. }