StickyRoundRobinSelector.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Elasticsearch\ConnectionPool\Selectors;
  3. use Elasticsearch\Connections\ConnectionInterface;
  4. /**
  5. * Class StickyRoundRobinSelector
  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 StickyRoundRobinSelector implements SelectorInterface
  14. {
  15. /**
  16. * @var int
  17. */
  18. private $current = 0;
  19. /**
  20. * @var int
  21. */
  22. private $currentCounter = 0;
  23. /**
  24. * Use current connection unless it is dead, otherwise round-robin
  25. *
  26. * @param ConnectionInterface[] $connections Array of connections to choose from
  27. *
  28. * @return ConnectionInterface
  29. */
  30. public function select($connections)
  31. {
  32. /** @var ConnectionInterface[] $connections */
  33. if ($connections[$this->current]->isAlive()) {
  34. return $connections[$this->current];
  35. }
  36. $this->currentCounter += 1;
  37. $this->current = $this->currentCounter % count($connections);
  38. return $connections[$this->current];
  39. }
  40. }