-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathConnectionPool.php
More file actions
147 lines (126 loc) · 4.8 KB
/
Copy pathConnectionPool.php
File metadata and controls
147 lines (126 loc) · 4.8 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
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\Bolt;
use Generator;
use Laudis\Neo4j\BoltFactory;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Laudis\Neo4j\Contracts\ConnectionInterface;
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
use Laudis\Neo4j\Contracts\SemaphoreInterface;
use Laudis\Neo4j\Databags\ConnectionRequestData;
use Laudis\Neo4j\Databags\DriverConfiguration;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\Exception\ConnectionPoolException;
use Psr\Http\Message\UriInterface;
use function shuffle;
/**
* @implements ConnectionPoolInterface<BoltConnection>
*/
final class ConnectionPool implements ConnectionPoolInterface
{
/** @var list<BoltConnection> */
private array $activeConnections = [];
public function __construct(
private readonly SemaphoreInterface $semaphore,
private readonly BoltFactory $factory,
private readonly ConnectionRequestData $data,
private readonly ?Neo4jLogger $logger,
private readonly float $acquireConnectionTimeout,
) {
}
public static function create(
UriInterface $uri,
AuthenticateInterface $auth,
DriverConfiguration $conf,
SemaphoreInterface $semaphore,
): self {
return new self(
$semaphore,
BoltFactory::create($conf->getLogger(), $conf->getSocketType()),
new ConnectionRequestData(
$uri->getHost(),
$uri,
$auth,
$conf->getUserAgent(),
$conf->getSslConfiguration(),
$conf->getSocketTimeoutSecondsExplicit()
),
$conf->getLogger(),
$conf->getAcquireConnectionTimeout()
);
}
public function acquire(SessionConfiguration $config): Generator
{
/**
* @var Generator<int, float, bool, BoltConnection>
*/
return (function () use ($config) {
$connection = $this->reuseConnectionIfPossible($config);
if ($connection !== null) {
return $connection;
}
$generator = $this->semaphore->wait();
// If the generator is valid, it means we are waiting to acquire a new connection.
// This means we can use this time to check if we can reuse a connection or should throw a timeout exception.
while ($generator->valid()) {
$waitTime = $generator->current();
if ($waitTime <= $this->acquireConnectionTimeout) {
yield $waitTime;
$connection = $this->reuseConnectionIfPossible($config);
if ($connection !== null) {
return $connection;
}
$generator->next();
} else {
throw new ConnectionPoolException('Connection acquire timeout reached: '.($waitTime ?? 0.0));
}
}
$connection = $this->reuseConnectionIfPossible($config);
if ($connection !== null) {
return $connection;
}
$connection = $this->factory->createConnection($this->data, $config);
$this->activeConnections[] = $connection;
return $connection;
})();
}
public function release(ConnectionInterface $connection): void
{
// Return a permit only — keep the connection in {@see $activeConnections} so it stays
// pooled for reuse and is still closed by {@see close()}. Removing it here orphaned
// sockets (no GOODBYE on driver close), which breaks TestKit stubs after errors.
$this->semaphore->post();
}
public function getLogger(): ?Neo4jLogger
{
return $this->logger;
}
private function reuseConnectionIfPossible(SessionConfiguration $config): ?BoltConnection
{
// Ensure random connection reuse before picking one.
shuffle($this->activeConnections);
foreach ($this->activeConnections as $activeConnection) {
// We prefer a connection that is just ready
if ($activeConnection->getServerState() === 'READY' && $this->factory->canReuseConnection($activeConnection, $config)) {
return $this->factory->reuseConnection($activeConnection, $config);
}
}
return null;
}
public function close(): void
{
foreach ($this->activeConnections as $activeConnection) {
$activeConnection->close();
}
$this->activeConnections = [];
}
}