1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\ConnectionPool;
23:
24: use OpenSearch\Common\Exceptions\InvalidArgumentException;
25: use OpenSearch\ConnectionPool\Selectors\SelectorInterface;
26: use OpenSearch\Connections\ConnectionFactoryInterface;
27: use OpenSearch\Connections\ConnectionInterface;
28:
29: // @phpstan-ignore classConstant.deprecatedClass
30: @trigger_error(AbstractConnectionPool::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
31:
32: /**
33: * @deprecated in 2.4.0 and will be removed in 3.0.0.
34: */
35: abstract class AbstractConnectionPool implements ConnectionPoolInterface
36: {
37: /**
38: * Array of connections
39: *
40: * @var ConnectionInterface[]
41: */
42: protected $connections;
43:
44: /**
45: * Array of initial seed connections
46: *
47: * @var ConnectionInterface[]
48: */
49: protected $seedConnections;
50:
51: /**
52: * Selector object, used to select a connection on each request
53: *
54: * @var SelectorInterface
55: */
56: protected $selector;
57:
58: /**
59: * @var array<string, mixed>
60: */
61: protected $connectionPoolParams;
62:
63: /**
64: * @var ConnectionFactoryInterface
65: */
66: protected $connectionFactory;
67:
68: /**
69: * Constructor
70: *
71: * @param ConnectionInterface[] $connections The Connections to choose from
72: * @param SelectorInterface $selector A Selector instance to perform the selection logic for the available connections
73: * @param ConnectionFactoryInterface $factory ConnectionFactory instance
74: * @param array<string, mixed> $connectionPoolParams
75: */
76: public function __construct(array $connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, array $connectionPoolParams)
77: {
78: $paramList = array('connections', 'selector', 'connectionPoolParams');
79: foreach ($paramList as $param) {
80: if (isset($$param) === false) {
81: throw new InvalidArgumentException('`' . $param . '` parameter must not be null');
82: }
83: }
84:
85: if (isset($connectionPoolParams['randomizeHosts']) === true
86: && $connectionPoolParams['randomizeHosts'] === true
87: ) {
88: shuffle($connections);
89: }
90:
91: $this->connections = $connections;
92: $this->seedConnections = $connections;
93: $this->selector = $selector;
94: $this->connectionPoolParams = $connectionPoolParams;
95: $this->connectionFactory = $factory;
96: }
97:
98: abstract public function nextConnection(bool $force = false): ConnectionInterface;
99:
100: abstract public function scheduleCheck(): void;
101: }
102: