Skip to content

Commit 178b0c1

Browse files
committed
fixed static analysis issues after merge
1 parent 9313eea commit 178b0c1

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace Laudis\Neo4j\Neo4j;
1515

16-
use Laudis\Neo4j\Contracts\AddressResolverInterface;
17-
use RuntimeException;
1816
use function array_slice;
1917
use function array_unique;
2018
use Bolt\error\MessageException;
@@ -25,6 +23,7 @@
2523
use function count;
2624
use Exception;
2725
use Generator;
26+
use function implode;
2827
use Laudis\Neo4j\Bolt\BoltConnection;
2928
use Laudis\Neo4j\Bolt\Connection;
3029
use Laudis\Neo4j\Bolt\ConnectionPool;
@@ -33,6 +32,7 @@
3332
use Laudis\Neo4j\Common\GeneratorHelper;
3433
use Laudis\Neo4j\Common\SemaphoreFactory;
3534
use Laudis\Neo4j\Common\Uri;
35+
use Laudis\Neo4j\Contracts\AddressResolverInterface;
3636
use Laudis\Neo4j\Contracts\AuthenticateInterface;
3737
use Laudis\Neo4j\Contracts\ConnectionInterface;
3838
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
@@ -43,14 +43,15 @@
4343
use Laudis\Neo4j\Databags\SessionConfiguration;
4444
use Laudis\Neo4j\Enum\AccessMode;
4545
use Laudis\Neo4j\Enum\RoutingRoles;
46-
use function implode;
47-
use function sprintf;
4846
use const PHP_INT_MAX;
4947
use Psr\Http\Message\UriInterface;
5048
use Psr\SimpleCache\CacheInterface;
5149
use function random_int;
50+
use RuntimeException;
51+
use function sprintf;
5252
use function str_replace;
5353
use function str_starts_with;
54+
use Throwable;
5455
use function time;
5556

5657
/**
@@ -62,8 +63,6 @@
6263
*/
6364
final class Neo4jConnectionPool implements ConnectionPoolInterface
6465
{
65-
/** @psalm-readonly */
66-
private BoltConnectionPool $pool;
6766
private AddressResolverInterface $resolver;
6867
/** @var array<string, ConnectionPool> */
6968
private static array $pools = [];
@@ -75,15 +74,16 @@ final class Neo4jConnectionPool implements ConnectionPoolInterface
7574
/**
7675
* @psalm-mutation-free
7776
*/
78-
public function __construct(SemaphoreInterface $semaphore, BoltFactory $factory, ConnectionRequestData $data, CacheInterface $cache)
77+
public function __construct(SemaphoreInterface $semaphore, BoltFactory $factory, ConnectionRequestData $data, CacheInterface $cache, AddressResolverInterface $resolver)
7978
{
8079
$this->semaphore = $semaphore;
8180
$this->factory = $factory;
8281
$this->data = $data;
8382
$this->cache = $cache;
83+
$this->resolver = $resolver;
8484
}
8585

86-
public static function create(UriInterface $uri, AuthenticateInterface $auth, DriverConfiguration $conf): self
86+
public static function create(UriInterface $uri, AuthenticateInterface $auth, DriverConfiguration $conf, AddressResolverInterface $resolver): self
8787
{
8888
$semaphore = SemaphoreFactory::getInstance()->create($uri, $conf);
8989

@@ -96,7 +96,8 @@ public static function create(UriInterface $uri, AuthenticateInterface $auth, Dr
9696
$conf->getUserAgent(),
9797
$conf->getSslConfiguration()
9898
),
99-
Cache::getInstance()
99+
Cache::getInstance(),
100+
$resolver
100101
);
101102
}
102103

@@ -129,17 +130,23 @@ public function acquire(SessionConfiguration $config): Generator
129130
$triedAddresses = [];
130131

131132
if ($table == null) {
132-
$addresses = $this->resolver->getAddresses($this->data->getUri());
133+
$addresses = $this->resolver->getAddresses((string) $this->data->getUri());
133134
foreach ($addresses as $address) {
134135
$triedAddresses[] = $address;
135-
$pool = $this->createOrGetPool($address);
136-
if ($this->pool->canConnect($this->data->getUri()->withHost($address), $this->data->getAuth())) {
136+
$pool = $this->createOrGetPool(Uri::create($address));
137+
try {
137138
/** @var BoltConnection $connection */
138139
$connection = GeneratorHelper::getReturnFromGenerator($pool->acquire($config));
139-
$table = $this->routingTable($connection, $config);
140-
$this->cache->set($key, $table, $table->getTtl());
141-
$pool->release($connection);
140+
$table = $this->routingTable($connection, $config);
141+
} catch (Throwable $e) {
142+
// todo - once client side logging is implemented it must be conveyed here.
143+
continue; // We continue if something is wrong with the current server
142144
}
145+
146+
$this->cache->set($key, $table, $table->getTtl());
147+
$pool->release($connection);
148+
149+
break;
143150
}
144151
}
145152

src/Neo4j/Neo4jDriver.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
use function is_string;
1818
use Laudis\Neo4j\Authentication\Authenticate;
1919
use Laudis\Neo4j\Bolt\Session;
20-
use Laudis\Neo4j\Common\GeneratorHelper;
2120
use Laudis\Neo4j\Common\DNSAddressResolver;
21+
use Laudis\Neo4j\Common\GeneratorHelper;
2222
use Laudis\Neo4j\Common\Uri;
23+
use Laudis\Neo4j\Contracts\AddressResolverInterface;
2324
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2425
use Laudis\Neo4j\Contracts\DriverInterface;
2526
use Laudis\Neo4j\Contracts\FormatterInterface;
@@ -74,19 +75,20 @@ public function __construct(
7475
*
7576
* @psalm-suppress MixedReturnTypeCoercion
7677
*/
77-
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, FormatterInterface $formatter = null): self
78+
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, FormatterInterface $formatter = null, ?AddressResolverInterface $resolver = null): self
7879
{
7980
if (is_string($uri)) {
8081
$uri = Uri::create($uri);
8182
}
8283

8384
$configuration ??= DriverConfiguration::default();
8485
$authenticate ??= Authenticate::fromUrl($uri);
86+
$resolver ??= new DNSAddressResolver();
8587

8688
/** @psalm-suppress InvalidArgument */
8789
return new self(
8890
$uri,
89-
Neo4jConnectionPool::create($uri, $authenticate, $configuration),
91+
Neo4jConnectionPool::create($uri, $authenticate, $configuration, $resolver),
9092
$formatter ?? OGMFormatter::create(),
9193
);
9294
}

tests/Integration/ComplexQueryTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
use Bolt\error\MessageException;
1717
use Generator;
18-
use Laudis\Neo4j\Basic\Driver;
19-
use Throwable;
2018
use function getenv;
2119
use InvalidArgumentException;
2220
use Laudis\Neo4j\ClientBuilder;
@@ -33,6 +31,7 @@
3331
use Laudis\Neo4j\Types\CypherMap;
3432
use Laudis\Neo4j\Types\Node;
3533
use function str_starts_with;
34+
use Throwable;
3635

3736
/**
3837
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter

0 commit comments

Comments
 (0)