StaticNoPingConnectionPool.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Elasticsearch\ConnectionPool;
  3. use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
  4. use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
  5. use Elasticsearch\Connections\Connection;
  6. use Elasticsearch\Connections\ConnectionFactoryInterface;
  7. class StaticNoPingConnectionPool extends AbstractConnectionPool implements ConnectionPoolInterface
  8. {
  9. /**
  10. * @var int
  11. */
  12. private $pingTimeout = 60;
  13. /**
  14. * @var int
  15. */
  16. private $maxPingTimeout = 3600;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
  21. {
  22. parent::__construct($connections, $selector, $factory, $connectionPoolParams);
  23. }
  24. /**
  25. * @param bool $force
  26. *
  27. * @return Connection
  28. * @throws \Elasticsearch\Common\Exceptions\NoNodesAvailableException
  29. */
  30. public function nextConnection($force = false)
  31. {
  32. $total = count($this->connections);
  33. while ($total--) {
  34. /** @var Connection $connection */
  35. $connection = $this->selector->select($this->connections);
  36. if ($connection->isAlive() === true) {
  37. return $connection;
  38. }
  39. if ($this->readyToRevive($connection) === true) {
  40. return $connection;
  41. }
  42. }
  43. throw new NoNodesAvailableException("No alive nodes found in your cluster");
  44. }
  45. public function scheduleCheck()
  46. {
  47. }
  48. /**
  49. * @param \Elasticsearch\Connections\Connection $connection
  50. *
  51. * @return bool
  52. */
  53. private function readyToRevive(Connection $connection)
  54. {
  55. $timeout = min(
  56. $this->pingTimeout * pow(2, $connection->getPingFailures()),
  57. $this->maxPingTimeout
  58. );
  59. if ($connection->getLastPing() + $timeout < time()) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. }