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;
20
21use Elasticsearch\Common\Exceptions\InvalidArgumentException;
22use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
23use Elasticsearch\Connections\ConnectionFactoryInterface;
24use Elasticsearch\Connections\ConnectionInterface;
25
26abstract class AbstractConnectionPool implements ConnectionPoolInterface
27{
28    /**
29     * Array of connections
30     *
31     * @var ConnectionInterface[]
32     */
33    protected $connections;
34
35    /**
36     * Array of initial seed connections
37     *
38     * @var ConnectionInterface[]
39     */
40    protected $seedConnections;
41
42    /**
43     * Selector object, used to select a connection on each request
44     *
45     * @var SelectorInterface
46     */
47    protected $selector;
48
49    /**
50     * @var array
51     */
52    protected $connectionPoolParams;
53
54    /**
55     * @var \Elasticsearch\Connections\ConnectionFactory
56     */
57    protected $connectionFactory;
58
59    /**
60     * Constructor
61     *
62     * @param ConnectionInterface[]      $connections          The Connections to choose from
63     * @param SelectorInterface          $selector             A Selector instance to perform the selection logic for the available connections
64     * @param ConnectionFactoryInterface $factory              ConnectionFactory instance
65     * @param array                      $connectionPoolParams
66     */
67    public function __construct(array $connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, array $connectionPoolParams)
68    {
69        $paramList = array('connections', 'selector', 'connectionPoolParams');
70        foreach ($paramList as $param) {
71            if (isset($$param) === false) {
72                throw new InvalidArgumentException('`' . $param . '` parameter must not be null');
73            }
74        }
75
76        if (isset($connectionPoolParams['randomizeHosts']) === true
77            && $connectionPoolParams['randomizeHosts'] === true
78        ) {
79            shuffle($connections);
80        }
81
82        $this->connections          = $connections;
83        $this->seedConnections      = $connections;
84        $this->selector             = $selector;
85        $this->connectionPoolParams = $connectionPoolParams;
86        $this->connectionFactory    = $factory;
87    }
88
89    abstract public function nextConnection(bool $force = false): ConnectionInterface;
90
91    abstract public function scheduleCheck(): void;
92}
93