From b6c39b095027aa4823d6a54cbd4f0c3e764ef85a Mon Sep 17 00:00:00 2001 From: exaby73 Date: Wed, 4 Dec 2024 14:50:13 +0530 Subject: [PATCH] feat: Fixed docker advertised address and routing logic in Neo4jConnectionPool --- docker-compose.yml | 20 ++++++++++---------- src/Neo4j/Neo4jConnectionPool.php | 16 ++++------------ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 91a1ed84..928d1f92 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -64,8 +64,8 @@ services: - "11474:7474" environment: <<: *common-env - NEO4j_server_bolt_advertised_address: localhost:11687 - NEO4j_server_http_advertised_address: localhost:11474 + NEO4J_server_bolt_advertised__address: neo4j:7687 + NEO4J_server_http_advertised__address: neo4j:7474 server1: <<: *common-cluster @@ -76,8 +76,8 @@ services: - "7474:7474" environment: <<: *common-core-env - NEO4j_server_bolt_advertised_address: localhost:7687 - NEO4j_server_http_advertised_address: localhost:7474 + NEO4J_server_bolt_advertised__address: server1:7687 + NEO4J_server_http_advertised__address: server1:7474 server2: <<: *common-cluster @@ -88,8 +88,8 @@ services: - "8474:7474" environment: <<: *common-core-env - NEO4j_server_bolt_advertised_address: localhost:8687 - NEO4j_server_http_advertised_address: localhost:8474 + NEO4J_server_bolt_advertised__address: server2:7687 + NEO4J_server_http_advertised__address: server2:7474 server3: <<: *common-cluster @@ -100,8 +100,8 @@ services: - "9687:7687" environment: <<: *common-core-env - NEO4j_server_bolt_advertised_address: localhost:9687 - NEO4j_server_http_advertised_address: localhost:9474 + NEO4J_server_bolt_advertised__address: server3:7687 + NEO4J_server_http_advertised__address: server3:7474 server4: <<: *common-cluster @@ -113,5 +113,5 @@ services: environment: <<: *common-cluster-env NEO4J_initial_server_mode__constraint: 'SECONDARY' - NEO4j_server_bolt_advertised_address: localhost:10687 - NEO4j_server_http_advertised_address: localhost:10474 + NEO4J_server_bolt_advertised__address: server4:7687 + NEO4J_server_http_advertised__address: server4:7474 diff --git a/src/Neo4j/Neo4jConnectionPool.php b/src/Neo4j/Neo4jConnectionPool.php index 0a725321..e3a064be 100644 --- a/src/Neo4j/Neo4jConnectionPool.php +++ b/src/Neo4j/Neo4jConnectionPool.php @@ -13,8 +13,6 @@ namespace Laudis\Neo4j\Neo4j; -use function array_unique; - use Bolt\error\ConnectException; use function count; @@ -25,7 +23,6 @@ use function implode; use Laudis\Neo4j\Bolt\BoltConnection; -use Laudis\Neo4j\Bolt\Connection; use Laudis\Neo4j\Bolt\ConnectionPool; use Laudis\Neo4j\BoltFactory; use Laudis\Neo4j\Common\Cache; @@ -127,7 +124,7 @@ public function acquire(SessionConfiguration $config): Generator { $key = $this->createKey($this->data, $config); - /** @var RoutingTable|null */ + /** @var RoutingTable|null $table */ $table = $this->cache->get($key); $triedAddresses = []; @@ -164,7 +161,7 @@ public function acquire(SessionConfiguration $config): Generator throw new RuntimeException(sprintf('Cannot connect to host: "%s". Hosts tried: "%s"', $this->data->getUri()->getHost(), implode('", "', $triedAddresses)), previous: $latestError); } - $server = $this->getNextServer($table, $config->getAccessMode()) ?? $this->data->getUri(); + $server = $this->getNextServer($table, $config->getAccessMode()); if ($server->getScheme() === '') { $server = $server->withScheme($this->data->getUri()->getScheme()); @@ -181,13 +178,8 @@ public function getLogger(): ?Neo4jLogger /** * @throws Exception */ - private function getNextServer(RoutingTable $table, AccessMode $mode): ?Uri + private function getNextServer(RoutingTable $table, AccessMode $mode): Uri { - $servers = array_unique($table->getWithRole()); - if (count($servers) === 1) { - return null; - } - if (AccessMode::WRITE() === $mode) { $servers = $table->getWithRole(RoutingRoles::LEADER()); } else { @@ -233,7 +225,7 @@ private function createKey(ConnectionRequestData $data, ?SessionConfiguration $c [ $data->getUserAgent(), $uri->getHost(), - $config ? $config->getDatabase() : null, + $config?->getDatabase(), $uri->getPort() ?? '7687', ] )