Skip to content

Commit f27475f

Browse files
committed
updated connectionpool to work with new factory methods
1 parent f61a491 commit f27475f

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

src/Bolt/BoltDriver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
final class BoltDriver implements DriverInterface
4343
{
4444
private UriInterface $parsedUrl;
45-
private SingleBoltConnectionPool $pool;
45+
private ConnectionPool $pool;
4646
private FormatterInterface $formatter;
4747

4848
/**
@@ -52,7 +52,7 @@ final class BoltDriver implements DriverInterface
5252
*/
5353
public function __construct(
5454
UriInterface $parsedUrl,
55-
SingleBoltConnectionPool $pool,
55+
ConnectionPool $pool,
5656
FormatterInterface $formatter
5757
) {
5858
$this->parsedUrl = $parsedUrl;
@@ -98,14 +98,14 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
9898
if ($formatter !== null) {
9999
return new self(
100100
$uri,
101-
new SingleBoltConnectionPool($uri, $configuration, $authenticate),
101+
new ConnectionPool($uri, $configuration, $authenticate),
102102
$formatter
103103
);
104104
}
105105

106106
return new self(
107107
$uri,
108-
new SingleBoltConnectionPool($uri, $configuration, $authenticate),
108+
new ConnectionPool($uri, $configuration, $authenticate),
109109
OGMFormatter::create(),
110110
);
111111
}

src/Bolt/SingleBoltConnectionPool.php renamed to src/Bolt/ConnectionPool.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,46 @@
1111

1212
namespace Laudis\Neo4j\Bolt;
1313

14-
use Bolt\protocol\V3;
15-
use function explode;
1614
use Generator;
17-
use Laudis\Neo4j\BoltFactory;
18-
use Laudis\Neo4j\Common\ConnectionConfiguration;
1915
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2016
use Laudis\Neo4j\Contracts\ConnectionFactoryInterface;
2117
use Laudis\Neo4j\Contracts\ConnectionInterface;
2218
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2319
use Laudis\Neo4j\Contracts\SemaphoreInterface;
24-
use Laudis\Neo4j\Databags\DatabaseInfo;
2520
use Laudis\Neo4j\Databags\SessionConfiguration;
26-
use Laudis\Neo4j\Enum\ConnectionProtocol;
21+
use Laudis\Neo4j\Databags\SslConfiguration;
22+
use function method_exists;
2723
use function microtime;
2824
use function shuffle;
2925

3026
/**
31-
* @implements ConnectionPoolInterface<V3>
27+
* @template T
28+
* @implements ConnectionPoolInterface<T>
3229
*/
33-
class SingleBoltConnectionPool implements ConnectionPoolInterface
30+
class ConnectionPool implements ConnectionPoolInterface
3431
{
3532
private SemaphoreInterface $semaphore;
3633

37-
/** @var list<BoltConnection> */
34+
/** @var list<ConnectionInterface<T>> */
3835
private array $activeConnections = [];
3936
private AuthenticateInterface $auth;
37+
/** @var ConnectionFactoryInterface<T> */
4038
private ConnectionFactoryInterface $factory;
39+
private string $userAgent;
40+
private SslConfiguration $sslConfiguration;
4141

42-
public function __construct(AuthenticateInterface $auth, SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory)
42+
/**
43+
* @param ConnectionFactoryInterface<T> $factory
44+
*/
45+
public function __construct(AuthenticateInterface $auth, string $userAgent, SslConfiguration $sslConfiguration, SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory)
4346
{
4447
$this->semaphore = $semaphore;
4548
$this->auth = $auth;
4649
$this->factory = $factory;
50+
$this->userAgent = $userAgent;
51+
$this->sslConfiguration = $sslConfiguration;
4752
}
4853

49-
/**
50-
* @return Generator<
51-
* int,
52-
* float,
53-
* bool,
54-
* BoltConnection|null
55-
* >
56-
*/
5754
public function acquire(SessionConfiguration $config): Generator
5855
{
5956
$generator = $this->semaphore->wait();
@@ -68,19 +65,19 @@ public function acquire(SessionConfiguration $config): Generator
6865
return null;
6966
}
7067

71-
$connection = $this->returnAnyAvailableConnection();
68+
$connection = $this->returnAnyAvailableConnection($config);
7269
if ($connection !== null) {
7370
return $connection;
7471
}
7572
}
7673

77-
return $this->returnAnyAvailableConnection() ?? $this->factory->createConnection($this->auth, $config);
74+
return $this->returnAnyAvailableConnection($config) ??
75+
$this->factory->createConnection($this->userAgent, $this->sslConfiguration, $config, $this->auth);
7876
}
7977

8078
public function release(ConnectionInterface $connection): void
8179
{
8280
$this->semaphore->post();
83-
$connection->close();
8481

8582
foreach ($this->activeConnections as $i => $activeConnection) {
8683
if ($connection === $activeConnection) {
@@ -91,7 +88,10 @@ public function release(ConnectionInterface $connection): void
9188
}
9289
}
9390

94-
private function returnAnyAvailableConnection(string $encryptionLevel): ?BoltConnection
91+
/**
92+
* @return ConnectionInterface<T>|null
93+
*/
94+
private function returnAnyAvailableConnection(SessionConfiguration $config): ?ConnectionInterface
9595
{
9696
$streamingConnection = null;
9797
$requiresReconnectConnection = null;
@@ -101,8 +101,8 @@ private function returnAnyAvailableConnection(string $encryptionLevel): ?BoltCon
101101
foreach ($this->activeConnections as $activeConnection) {
102102
// We prefer a connection that is just ready
103103
if ($activeConnection->getServerState() === 'READY') {
104-
if ($this->factory->canReuseConnection($activeConnection)) {
105-
return $this->factory->reuseConnection($activeConnection);
104+
if ($this->factory->canReuseConnection($activeConnection, $this->userAgent, $this->sslConfiguration, $this->auth)) {
105+
return $this->factory->reuseConnection($activeConnection, $config);
106106
} else {
107107
$requiresReconnectConnection = $activeConnection;
108108
}
@@ -116,23 +116,25 @@ private function returnAnyAvailableConnection(string $encryptionLevel): ?BoltCon
116116
// https://github.com/neo4j-php/neo4j-php-client/issues/146
117117
// NOTE: we cannot work with TX_STREAMING as we cannot force the transaction to implicitly close.
118118
if ($streamingConnection === null && $activeConnection->getServerState() === 'STREAMING') {
119-
if ($this->factory->canReuseConnection($activeConnection)) {
119+
if ($this->factory->canReuseConnection($activeConnection, $this->userAgent, $this->sslConfiguration, $this->auth)) {
120120
$streamingConnection = $activeConnection;
121-
$streamingConnection->consumeResults(); // State should now be ready
121+
if (method_exists($streamingConnection, 'consumeResults')) {
122+
$streamingConnection->consumeResults(); // State should now be ready
123+
}
122124
} else {
123125
$requiresReconnectConnection = $activeConnection;
124126
}
125127
}
126128
}
127129

128130
if ($streamingConnection) {
129-
return $this->factory->reuseConnection($streamingConnection);
131+
return $this->factory->reuseConnection($streamingConnection, $config);
130132
}
131133

132134
if ($requiresReconnectConnection) {
133135
$this->release($requiresReconnectConnection);
134136

135-
return $this->factory->createConnection($this->auth);
137+
return $this->factory->createConnection($this->userAgent, $this->sslConfiguration, $config, $this->auth);
136138
}
137139

138140
return null;

src/Bolt/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class Session implements SessionInterface
6565

6666
/**
6767
* @param FormatterInterface<ResultFormat> $formatter
68-
* @param SingleBoltConnectionPool|Neo4jConnectionPool $pool
68+
* @param ConnectionPool|Neo4jConnectionPool $pool
6969
*
7070
* @psalm-mutation-free
7171
*/

src/BoltFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function createConnection(
5858
AuthenticateInterface $auth
5959
): ConnectionInterface {
6060
[$connection, $encryptionLevel] = $this->connectionFactory->create($sslConfig);
61-
[$protocol, $authResponse] = $this->protocolFactory->createProtocol($connection, $userAgent);
61+
[$protocol, $authResponse] = $this->protocolFactory->createProtocol($connection, $auth, $userAgent);
6262

6363
$sessionConfig = new ConnectionConfiguration(
6464
$authResponse['server'],

src/Contracts/ConnectionInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public function getServerAddress(): UriInterface;
6262
*/
6363
public function getServerVersion(): string;
6464

65+
/**
66+
* Returns the assumed server state.
67+
*
68+
* @psalm-mutation-free
69+
*/
70+
public function getServerState(): string;
71+
6572
/**
6673
* Returns the protocol used to connect to the server.
6774
*

0 commit comments

Comments
 (0)