Skip to content

Commit 4ab872b

Browse files
committed
used route message where possible
1 parent 897cc49 commit 4ab872b

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

src/Enum/ConnectionProtocol.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ final class ConnectionProtocol extends TypedEnum
3232
private const BOLT_V40 = 'bolt-v40';
3333
private const BOLT_V41 = 'bolt-v41';
3434
private const BOLT_V42 = 'bolt-v42';
35-
private const BOLT_V43 = 'bolt-43';
35+
private const BOLT_V43 = 'bolt-v43';
3636
private const HTTP = 'http';
3737

3838
public static function determineBoltVersion(Bolt $bolt): self
3939
{
4040
switch ($bolt->getProtocolVersion()) {
41-
case 3.0:
41+
case 3:
4242
$tbr = self::BOLT_V3();
4343
break;
4444
case 4.0:
@@ -58,4 +58,21 @@ public static function determineBoltVersion(Bolt $bolt): self
5858

5959
return $tbr;
6060
}
61+
62+
public function compare(ConnectionProtocol $protocol): int
63+
{
64+
$x = 0;
65+
$y = 0;
66+
foreach (array_values(self::getAllInstances()) as $index => $instance) {
67+
if ($instance === $this) {
68+
$x = $index;
69+
}
70+
71+
if ($instance === $protocol) {
72+
$y = $index;
73+
}
74+
}
75+
76+
return $x - $y;
77+
}
6178
}

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Bolt\Bolt;
1717
use Bolt\connection\StreamSocket;
1818
use Exception;
19+
use Laudis\Neo4j\Bolt\BoltConnectionPool;
20+
use Laudis\Neo4j\Enum\ConnectionProtocol;
1921
use function explode;
2022
use Laudis\Neo4j\Bolt\BoltDriver;
2123
use Laudis\Neo4j\Common\TransactionHelper;
@@ -40,6 +42,12 @@
4042
final class Neo4jConnectionPool implements ConnectionPoolInterface
4143
{
4244
private ?RoutingTable $table = null;
45+
private BoltConnectionPool $pool;
46+
47+
public function __construct(BoltConnectionPool $pool)
48+
{
49+
$this->pool = $pool;
50+
}
4351

4452
/**
4553
* @throws Exception
@@ -51,7 +59,8 @@ public function acquire(
5159
string $userAgent,
5260
SessionConfiguration $config
5361
): ConnectionInterface {
54-
$table = $this->routingTable($uri, $authenticate);
62+
$connection = $this->pool->acquire($uri, $authenticate, $socketTimeout, $userAgent, $config);
63+
$table = $this->routingTable($connection);
5564
$server = $this->getNextServer($table, $config->getAccessMode());
5665

5766
$socket = new StreamSocket($server->getHost(), $server->getPort() ?? 7687, $socketTimeout);
@@ -76,26 +85,28 @@ private function getNextServer(RoutingTable $table, AccessMode $mode): Uri
7685
}
7786

7887
/**
79-
* @param BasicDriver $driver
88+
* @param ConnectionInterface<Bolt> $driver
8089
*
8190
* @throws Exception
8291
*/
83-
private function routingTable(UriInterface $uri, AuthenticateInterface $authenticate): RoutingTable
92+
private function routingTable(ConnectionInterface $connection): RoutingTable
8493
{
8594
if ($this->table === null || $this->table->getTtl() < time()) {
86-
$session = BoltDriver::create($uri, null, $authenticate)->createSession();
87-
$row = $session->run(<<<'CYPHER'
88-
CALL dbms.components() YIELD versions UNWIND versions AS version RETURN version
89-
CYPHER
90-
)->first();
91-
/** @var string */
92-
$version = $row->get('version');
93-
94-
if (str_starts_with($version, '3')) {
95-
$response = $session->run('CALL dbms.cluster.overview()');
96-
97-
/** @var iterable<array{addresses: list<string>, role:string}> $values */
98-
$values = [];
95+
$bolt = $connection->getImplementation();
96+
$protocol = $connection->getProtocol();
97+
if ($protocol->compare(ConnectionProtocol::BOLT_V43()) >= 0) {
98+
$route = $bolt->route()['rt'];
99+
['servers' => $servers, 'ttl' => $ttl ] = $route;
100+
} elseif ($protocol->compare(ConnectionProtocol::BOLT_V40()) >= 0) {
101+
$bolt->run('CALL dbms.routing.getRoutingTable({context: []})');
102+
['servers' => $servers, 'ttl' => $ttl] = $bolt->pullAll();
103+
} else {
104+
$bolt->run('CALL dbms.cluster.overview()');
105+
$response = $bolt->pullAll();
106+
107+
/** @var iterable<array{addresses: list<string>, role:string}> $servers */
108+
$servers = [];
109+
$ttl = time() + 3600;
99110
foreach ($response as $server) {
100111
/** @var CypherList<string> $addresses */
101112
$addresses = $server->get('addresses');
@@ -105,19 +116,10 @@ private function routingTable(UriInterface $uri, AuthenticateInterface $authenti
105116
*
106117
* @var array{addresses: list<string>, role:string}
107118
*/
108-
$values[] = ['addresses' => $addresses->toArray(), 'role' => $server->get('role')];
119+
$servers[] = ['addresses' => $addresses->toArray(), 'role' => $server->get('role')];
109120
}
110-
111-
$this->table = new RoutingTable($values, time() + 3600);
112-
} else {
113-
$response = $session->run('CALL dbms.routing.getRoutingTable({context: []})')->first();
114-
/** @var iterable<array{addresses: list<string>, role:string}> $values */
115-
$values = $response->get('servers');
116-
/** @var int $ttl */
117-
$ttl = $response->get('ttl');
118-
119-
$this->table = new RoutingTable($values, time() + $ttl);
120121
}
122+
$this->table = new RoutingTable($servers, $ttl);
121123
}
122124

123125
return $this->table;

src/Neo4j/Neo4jDriver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Bolt\Bolt;
1717
use Exception;
18+
use Laudis\Neo4j\Bolt\BoltConnectionPool;
1819
use function is_string;
1920
use Laudis\Neo4j\Authentication\Authenticate;
2021
use Laudis\Neo4j\Bolt\Session;
@@ -92,7 +93,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
9293
return new self(
9394
$uri,
9495
$authenticate ?? Authenticate::fromUrl(),
95-
new Neo4jConnectionPool(),
96+
new Neo4jConnectionPool(new BoltConnectionPool()),
9697
$configuration ?? DriverConfiguration::default(),
9798
$formatter,
9899
$socketTimeout
@@ -102,7 +103,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
102103
return new self(
103104
$uri,
104105
$authenticate ?? Authenticate::fromUrl(),
105-
new Neo4jConnectionPool(),
106+
new Neo4jConnectionPool(new BoltConnectionPool()),
106107
$configuration ?? DriverConfiguration::default(),
107108
OGMFormatter::create(),
108109
$socketTimeout

0 commit comments

Comments
 (0)