1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Manticoresearch\Connection; |
4
|
|
|
|
5
|
|
|
use Manticoresearch\Connection; |
6
|
|
|
use Manticoresearch\Connection\Strategy\SelectorInterface; |
7
|
|
|
use Manticoresearch\Exceptions\ConnectionException; |
8
|
|
|
use Manticoresearch\Exceptions\NoMoreNodesException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ConnectionPool |
12
|
|
|
* @package Manticoresearch\Connection |
13
|
|
|
*/ |
14
|
|
|
class ConnectionPool |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $connections; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var SelectorInterface |
23
|
|
|
*/ |
24
|
|
|
public $strategy; |
25
|
|
|
|
26
|
|
|
public $retries; |
27
|
|
|
|
28
|
|
|
public $retries_attempts = 0; |
29
|
|
|
|
30
|
|
|
public $retries_info = []; |
31
|
|
|
|
32
|
|
|
public function __construct(array $connections, SelectorInterface $strategy, int $retries) |
33
|
|
|
{ |
34
|
|
|
$this->connections = $connections; |
35
|
|
|
$this->strategy = $strategy; |
36
|
|
|
$this->retries = $retries; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getConnections(): array |
43
|
|
|
{ |
44
|
|
|
return $this->connections; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $connections |
49
|
|
|
*/ |
50
|
|
|
public function setConnections(array $connections) |
51
|
|
|
{ |
52
|
|
|
$this->connections = $connections; |
53
|
|
|
} |
54
|
|
|
public function getConnection(): Connection |
55
|
|
|
{ |
56
|
|
|
$this->retries_attempts++; |
57
|
|
|
$connection = $this->strategy->getConnection($this->connections); |
58
|
|
|
if ($this->retries_attempts <= $this->retries) { |
59
|
|
|
$this->retries_info[] = [ |
60
|
|
|
'host' => $connection->getHost(), |
61
|
|
|
'port' => $connection->getPort(), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
if ($connection->isAlive()) { |
65
|
|
|
return $connection; |
66
|
|
|
} |
67
|
|
|
if ($this->retries_attempts < $this->retries) { |
68
|
|
|
return $connection; |
69
|
|
|
} |
70
|
|
|
$exMsg = 'After %d retr%s to %d node%s, connection has failed. No more retries left.'; |
71
|
|
|
$exMsg .= "\nRetries made:\n"; |
72
|
|
|
foreach ($this->retries_info as $i => $info) { |
73
|
|
|
$i++; |
74
|
|
|
$exMsg .= " $i. to {$info['host']}:{$info['port']}\n"; |
75
|
|
|
} |
76
|
|
|
$connCount = count($this->connections); |
77
|
|
|
throw new NoMoreNodesException( |
78
|
|
|
sprintf($exMsg, $this->retries, $this->retries > 1 ? 'ies' : 'y', $connCount, $connCount > 1 ? 's' : '') |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function hasConnections(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->retries_attempts < $this->retries; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return SelectorInterface |
89
|
|
|
*/ |
90
|
|
|
public function getStrategy(): SelectorInterface |
91
|
|
|
{ |
92
|
|
|
return $this->strategy; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param SelectorInterface $strategy |
97
|
|
|
*/ |
98
|
|
|
public function setStrategy(SelectorInterface $strategy) |
99
|
|
|
{ |
100
|
|
|
$this->strategy = $strategy; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|