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 16 17declare(strict_types = 1); 18 19namespace Elasticsearch\ConnectionPool\Selectors; 20 21use Elasticsearch\Connections\ConnectionInterface; 22 23class StickyRoundRobinSelector implements SelectorInterface 24{ 25 /** 26 * @var int 27 */ 28 private $current = 0; 29 30 /** 31 * @var int 32 */ 33 private $currentCounter = 0; 34 35 /** 36 * Use current connection unless it is dead, otherwise round-robin 37 * 38 * @param ConnectionInterface[] $connections Array of connections to choose from 39 */ 40 public function select(array $connections): ConnectionInterface 41 { 42 /** 43 * @var ConnectionInterface[] $connections 44*/ 45 if ($connections[$this->current]->isAlive()) { 46 return $connections[$this->current]; 47 } 48 49 $this->currentCounter += 1; 50 $this->current = $this->currentCounter % count($connections); 51 52 return $connections[$this->current]; 53 } 54} 55