Skip to content

Commit f8dbddc

Browse files
committed
rewired the conneciton logic
1 parent 0b1c2aa commit f8dbddc

11 files changed

+200
-113
lines changed

src/Bolt/BoltConnection.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
use Laudis\Neo4j\Enum\ConnectionProtocol;
2929
use Laudis\Neo4j\Types\CypherList;
3030
use Psr\Http\Message\UriInterface;
31-
use RuntimeException;
3231
use function str_starts_with;
3332
use WeakReference;
3433

3534
/**
36-
* @implements ConnectionInterface<array{0: V3, 1: IConnection}>
35+
* @implements ConnectionInterface<array{0: V3, 1: Connection}>
3736
*
3837
* @psalm-import-type BoltMeta from FormatterInterface
3938
*/
@@ -59,44 +58,30 @@ final class BoltConnection implements ConnectionInterface
5958
*/
6059
private array $subscribedResults = [];
6160
private AuthenticateInterface $auth;
62-
private IConnection $connection;
63-
private string $encryptionLevel;
61+
private Connection $connection;
6462
private string $userAgent;
6563

64+
public function getImplementation()
65+
{
66+
return [$this->boltProtocol, $this->connection];
67+
}
68+
6669
/**
6770
* @psalm-mutation-free
6871
*/
69-
public function __construct(V3 $protocol, IConnection $connection, ConnectionConfiguration $config, AuthenticateInterface $auth, string $encryptionLevel, string $userAgent)
72+
public function __construct(V3 $protocol, Connection $connection, AuthenticateInterface $auth, string $userAgent, ConnectionConfiguration $config)
7073
{
7174
$this->boltProtocol = $protocol;
72-
$this->config = $config;
7375
$this->serverState = 'READY';
7476
$this->auth = $auth;
7577
$this->connection = $connection;
76-
$this->encryptionLevel = $encryptionLevel;
7778
$this->userAgent = $userAgent;
79+
$this->config = $config;
7880
}
7981

80-
/**
81-
* @psalm-mutation-free
82-
*
83-
* @return array{0: V3, 1: IConnection}
84-
*/
85-
public function getImplementation(): array
86-
{
87-
if (!$this->isOpen()) {
88-
throw new RuntimeException('Connection is closed');
89-
}
90-
91-
return [$this->boltProtocol, $this->connection];
92-
}
93-
94-
/**
95-
* Encryption level can be either '', 's' or 'ssc', which stand for 'no encryption', 'full encryption' and 'self-signed encryption' respectively.
96-
*/
9782
public function getEncryptionLevel(): string
9883
{
99-
return $this->encryptionLevel;
84+
return $this->connection->getEncryptionLevel();
10085
}
10186

10287
/**
@@ -338,7 +323,7 @@ public function rollback(): void
338323

339324
public function protocol(): V3
340325
{
341-
return $this->getImplementation()[0];
326+
return $this->boltProtocol;
342327
}
343328

344329
/**

src/Bolt/Connection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
namespace Laudis\Neo4j\Bolt;
1515

1616
use Bolt\connection\IConnection;
17+
use Bolt\protocol\AProtocol;
1718

1819
class Connection
1920
{
2021
private IConnection $connection;
21-
/** ''|'s'|'ssc' */
22+
/** @var ''|'s'|'ssc' */
2223
private string $ssl;
24+
private ?AProtocol $protocol = null;
2325

2426
/**
25-
* @param IConnection $connection
2627
* @param ''|'s'|'ssc' $ssl
2728
*/
2829
public function __construct(IConnection $connection, string $ssl)
@@ -31,9 +32,9 @@ public function __construct(IConnection $connection, string $ssl)
3132
$this->ssl = $ssl;
3233
}
3334

34-
public function connect(): bool
35+
public function getIConnection(): IConnection
3536
{
36-
return $this->connection->connect();
37+
return $this->connection;
3738
}
3839

3940
public function write(string $buffer): void

src/Bolt/ConnectionPool.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Neo4j PHP Client and Driver package.
57
*
@@ -17,8 +19,11 @@
1719
use Laudis\Neo4j\Contracts\ConnectionInterface;
1820
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
1921
use Laudis\Neo4j\Contracts\SemaphoreInterface;
22+
use Laudis\Neo4j\Databags\ConnectionRequestData;
2023
use Laudis\Neo4j\Databags\SessionConfiguration;
2124
use Laudis\Neo4j\Databags\SslConfiguration;
25+
use Psr\Http\Message\UriInterface;
26+
2227
use function method_exists;
2328
use function microtime;
2429
use function shuffle;
@@ -27,28 +32,24 @@
2732
* @template T
2833
* @implements ConnectionPoolInterface<T>
2934
*/
30-
class ConnectionPool implements ConnectionPoolInterface
35+
final class ConnectionPool implements ConnectionPoolInterface
3136
{
3237
private SemaphoreInterface $semaphore;
3338

3439
/** @var list<ConnectionInterface<T>> */
3540
private array $activeConnections = [];
36-
private AuthenticateInterface $auth;
3741
/** @var ConnectionFactoryInterface<T> */
3842
private ConnectionFactoryInterface $factory;
39-
private string $userAgent;
40-
private SslConfiguration $sslConfiguration;
43+
private ConnectionRequestData $data;
4144

4245
/**
4346
* @param ConnectionFactoryInterface<T> $factory
4447
*/
45-
public function __construct(AuthenticateInterface $auth, string $userAgent, SslConfiguration $sslConfiguration, SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory)
48+
public function __construct(SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory, ConnectionRequestData $data)
4649
{
4750
$this->semaphore = $semaphore;
48-
$this->auth = $auth;
4951
$this->factory = $factory;
50-
$this->userAgent = $userAgent;
51-
$this->sslConfiguration = $sslConfiguration;
52+
$this->data = $data;
5253
}
5354

5455
public function acquire(SessionConfiguration $config): Generator
@@ -72,7 +73,7 @@ public function acquire(SessionConfiguration $config): Generator
7273
}
7374

7475
return $this->returnAnyAvailableConnection($config) ??
75-
$this->factory->createConnection($this->userAgent, $this->sslConfiguration, $config, $this->auth);
76+
$this->factory->createConnection($this->data, $config);
7677
}
7778

7879
public function release(ConnectionInterface $connection): void
@@ -101,7 +102,7 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
101102
foreach ($this->activeConnections as $activeConnection) {
102103
// We prefer a connection that is just ready
103104
if ($activeConnection->getServerState() === 'READY') {
104-
if ($this->factory->canReuseConnection($activeConnection, $this->userAgent, $this->sslConfiguration, $this->auth)) {
105+
if ($this->factory->canReuseConnection($activeConnection, $this->data)) {
105106
return $this->factory->reuseConnection($activeConnection, $config);
106107
} else {
107108
$requiresReconnectConnection = $activeConnection;
@@ -116,7 +117,7 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
116117
// https://github.com/neo4j-php/neo4j-php-client/issues/146
117118
// NOTE: we cannot work with TX_STREAMING as we cannot force the transaction to implicitly close.
118119
if ($streamingConnection === null && $activeConnection->getServerState() === 'STREAMING') {
119-
if ($this->factory->canReuseConnection($activeConnection, $this->userAgent, $this->sslConfiguration, $this->auth)) {
120+
if ($this->factory->canReuseConnection($activeConnection, $this->data)) {
120121
$streamingConnection = $activeConnection;
121122
if (method_exists($streamingConnection, 'consumeResults')) {
122123
$streamingConnection->consumeResults(); // State should now be ready
@@ -134,7 +135,7 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
134135
if ($requiresReconnectConnection) {
135136
$this->release($requiresReconnectConnection);
136137

137-
return $this->factory->createConnection($this->userAgent, $this->sslConfiguration, $config, $this->auth);
138+
return $this->factory->createConnection($this->data, $config);
138139
}
139140

140141
return null;

src/Bolt/UriConfiguration.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
final class UriConfiguration
88
{
99
private string $host;
10-
private int $port;
10+
private ?int $port;
1111
private string $sslLevel;
1212
private array $sslConfiguration;
13-
private int $timeout;
13+
private ?float $timeout;
1414

15-
public function __construct(string $host, int $port, string $sslLevel, array $sslConfiguration, int $timeout)
15+
public function __construct(string $host, ?int $port, string $sslLevel, array $sslConfiguration, ?float $timeout)
1616
{
1717
$this->host = $host;
1818
$this->port = $port;
@@ -21,42 +21,30 @@ public function __construct(string $host, int $port, string $sslLevel, array $ss
2121
$this->timeout = $timeout;
2222
}
2323

24-
/**
25-
* @return string
26-
*/
2724
public function getHost(): string
2825
{
2926
return $this->host;
3027
}
3128

32-
/**
33-
* @return int
34-
*/
35-
public function getPort(): int
29+
public function getPort(): ?int
3630
{
3731
return $this->port;
3832
}
3933

4034
/**
41-
* @return string
35+
* @return 's'|'ssc'|''
4236
*/
4337
public function getSslLevel(): string
4438
{
4539
return $this->sslLevel;
4640
}
4741

48-
/**
49-
* @return array
50-
*/
5142
public function getSslConfiguration(): array
5243
{
5344
return $this->sslConfiguration;
5445
}
5546

56-
/**
57-
* @return int
58-
*/
59-
public function getTimeout(): int
47+
public function getTimeout(): ?float
6048
{
6149
return $this->timeout;
6250
}

0 commit comments

Comments
 (0)