Skip to content

Commit

Permalink
fix: Docker advertised address and routing logic in Neo4jConnectionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 committed Dec 4, 2024
1 parent 7492476 commit b42e383
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
20 changes: 10 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
16 changes: 4 additions & 12 deletions src/Neo4j/Neo4jConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Laudis\Neo4j\Neo4j;

use function array_unique;

use Bolt\error\ConnectException;

use function count;
Expand All @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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());
Expand All @@ -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 {
Expand Down Expand Up @@ -233,7 +225,7 @@ private function createKey(ConnectionRequestData $data, ?SessionConfiguration $c
[
$data->getUserAgent(),
$uri->getHost(),
$config ? $config->getDatabase() : null,
$config?->getDatabase(),
$uri->getPort() ?? '7687',
]
)
Expand Down

0 comments on commit b42e383

Please sign in to comment.