vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elasticsearch PHP client
  4.  *
  5.  * @link      https://github.com/elastic/elasticsearch-php/
  6.  * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
  7.  * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  8.  * @license   https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
  9.  *
  10.  * Licensed to Elasticsearch B.V under one or more agreements.
  11.  * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
  12.  * the GNU Lesser General Public License, Version 2.1, at your option.
  13.  * See the LICENSE file in the project root for more information.
  14.  */
  15. declare(strict_types 1);
  16. namespace Elasticsearch\ConnectionPool;
  17. use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
  18. use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
  19. use Elasticsearch\Connections\Connection;
  20. use Elasticsearch\Connections\ConnectionInterface;
  21. use Elasticsearch\Connections\ConnectionFactoryInterface;
  22. class StaticNoPingConnectionPool extends AbstractConnectionPool implements ConnectionPoolInterface
  23. {
  24.     /**
  25.      * @var int
  26.      */
  27.     private $pingTimeout    60;
  28.     /**
  29.      * @var int
  30.      */
  31.     private $maxPingTimeout 3600;
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function __construct($connectionsSelectorInterface $selectorConnectionFactoryInterface $factory$connectionPoolParams)
  36.     {
  37.         parent::__construct($connections$selector$factory$connectionPoolParams);
  38.     }
  39.     public function nextConnection(bool $force false): ConnectionInterface
  40.     {
  41.         $total count($this->connections);
  42.         while ($total--) {
  43.             /**
  44.  * @var Connection $connection
  45. */
  46.             $connection $this->selector->select($this->connections);
  47.             if ($connection->isAlive() === true) {
  48.                 return $connection;
  49.             }
  50.             if ($this->readyToRevive($connection) === true) {
  51.                 return $connection;
  52.             }
  53.         }
  54.         throw new NoNodesAvailableException("No alive nodes found in your cluster");
  55.     }
  56.     public function scheduleCheck(): void
  57.     {
  58.     }
  59.     private function readyToRevive(Connection $connection): bool
  60.     {
  61.         $timeout min(
  62.             $this->pingTimeout pow(2$connection->getPingFailures()),
  63.             $this->maxPingTimeout
  64.         );
  65.         if ($connection->getLastPing() + $timeout time()) {
  66.             return true;
  67.         } else {
  68.             return false;
  69.         }
  70.     }
  71. }