Repository navigation
Expand file tree
/
Copy pathPool.php
More file actions
173 lines (145 loc) · 5.25 KB
/
Copy pathPool.php
File metadata and controls
173 lines (145 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Pool;
use Hyperf\Contract\ConnectionInterface;
use Hyperf\Contract\FrequencyInterface;
use Hyperf\Contract\PoolInterface;
use Hyperf\Contract\PoolOptionInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Throwable;
use function Hyperf\Support\make;
abstract class Pool implements PoolInterface
{
protected Channel $channel;
protected PoolOptionInterface $option;
protected int $currentConnections = 0;
protected null|FrequencyInterface|LowFrequencyInterface $frequency = null;
public function __construct(protected ContainerInterface $container, array $config = [])
{
$this->initOption($config);
$this->channel = make(Channel::class, ['size' => $this->option->getMaxConnections()]);
}
public function get(): ConnectionInterface
{
$connection = $this->getConnection();
try {
if ($this->frequency instanceof FrequencyInterface) {
$this->frequency->hit();
}
if ($this->frequency instanceof LowFrequencyInterface) {
if ($this->frequency->isLowFrequency()) {
$this->flush();
}
}
} catch (Throwable $exception) {
if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
$logger->error((string) $exception);
}
}
return $connection;
}
public function release(ConnectionInterface $connection): void
{
$this->channel->push($connection);
}
public function flush(): void
{
$num = $this->getConnectionsInChannel();
if ($num > 0) {
while ($this->currentConnections > $this->option->getMinConnections() && $conn = $this->channel->pop(0.001)) {
try {
$conn->close();
} catch (Throwable $exception) {
if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
$logger->error((string) $exception);
}
} finally {
--$this->currentConnections;
--$num;
}
if ($num <= 0) {
// Ignore connections queued during flushing.
break;
}
}
}
}
public function flushOne(bool $must = false): void
{
$num = $this->getConnectionsInChannel();
if ($num > 0 && $conn = $this->channel->pop(0.001)) {
if ($must || ! $conn->check()) {
try {
$conn->close();
} catch (Throwable $exception) {
if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
$logger->error((string) $exception);
}
} finally {
--$this->currentConnections;
}
} else {
$this->release($conn);
}
}
}
public function flushAll(): void
{
while ($this->getConnectionsInChannel() > 0) {
$this->flushOne(true);
}
}
public function getCurrentConnections(): int
{
return $this->currentConnections;
}
public function getOption(): PoolOptionInterface
{
return $this->option;
}
public function getConnectionsInChannel(): int
{
return $this->channel->length();
}
protected function initOption(array $options = []): void
{
$this->option = make(PoolOption::class, [
'minConnections' => $options['min_connections'] ?? 1,
'maxConnections' => $options['max_connections'] ?? 10,
'connectTimeout' => $options['connect_timeout'] ?? 10.0,
'waitTimeout' => $options['wait_timeout'] ?? 3.0,
'heartbeat' => $options['heartbeat'] ?? -1,
'maxIdleTime' => $options['max_idle_time'] ?? 60.0,
'events' => $options['events'] ?? [],
]);
}
abstract protected function createConnection(): ConnectionInterface;
private function getConnection(): ConnectionInterface
{
$num = $this->getConnectionsInChannel();
try {
if ($num === 0 && $this->currentConnections < $this->option->getMaxConnections()) {
++$this->currentConnections;
return $this->createConnection();
}
} catch (Throwable $throwable) {
--$this->currentConnections;
throw $throwable;
}
$connection = $this->channel->pop($this->option->getWaitTimeout());
if (! $connection instanceof ConnectionInterface) {
throw new RuntimeException('Connection pool exhausted. Cannot establish new connection before wait_timeout.');
}
return $connection;
}
}