Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Docker advertised address and routing logic in Neo4jConnectionPool #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading