13
13
14
14
namespace Laudis \Neo4j \Neo4j ;
15
15
16
- use Laudis \Neo4j \Contracts \AddressResolverInterface ;
17
- use RuntimeException ;
18
16
use function array_slice ;
19
17
use function array_unique ;
20
18
use Bolt \error \MessageException ;
25
23
use function count ;
26
24
use Exception ;
27
25
use Generator ;
26
+ use function implode ;
28
27
use Laudis \Neo4j \Bolt \BoltConnection ;
29
28
use Laudis \Neo4j \Bolt \Connection ;
30
29
use Laudis \Neo4j \Bolt \ConnectionPool ;
33
32
use Laudis \Neo4j \Common \GeneratorHelper ;
34
33
use Laudis \Neo4j \Common \SemaphoreFactory ;
35
34
use Laudis \Neo4j \Common \Uri ;
35
+ use Laudis \Neo4j \Contracts \AddressResolverInterface ;
36
36
use Laudis \Neo4j \Contracts \AuthenticateInterface ;
37
37
use Laudis \Neo4j \Contracts \ConnectionInterface ;
38
38
use Laudis \Neo4j \Contracts \ConnectionPoolInterface ;
43
43
use Laudis \Neo4j \Databags \SessionConfiguration ;
44
44
use Laudis \Neo4j \Enum \AccessMode ;
45
45
use Laudis \Neo4j \Enum \RoutingRoles ;
46
- use function implode ;
47
- use function sprintf ;
48
46
use const PHP_INT_MAX ;
49
47
use Psr \Http \Message \UriInterface ;
50
48
use Psr \SimpleCache \CacheInterface ;
51
49
use function random_int ;
50
+ use RuntimeException ;
51
+ use function sprintf ;
52
52
use function str_replace ;
53
53
use function str_starts_with ;
54
+ use Throwable ;
54
55
use function time ;
55
56
56
57
/**
62
63
*/
63
64
final class Neo4jConnectionPool implements ConnectionPoolInterface
64
65
{
65
- /** @psalm-readonly */
66
- private BoltConnectionPool $ pool ;
67
66
private AddressResolverInterface $ resolver ;
68
67
/** @var array<string, ConnectionPool> */
69
68
private static array $ pools = [];
@@ -75,15 +74,16 @@ final class Neo4jConnectionPool implements ConnectionPoolInterface
75
74
/**
76
75
* @psalm-mutation-free
77
76
*/
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 )
79
78
{
80
79
$ this ->semaphore = $ semaphore ;
81
80
$ this ->factory = $ factory ;
82
81
$ this ->data = $ data ;
83
82
$ this ->cache = $ cache ;
83
+ $ this ->resolver = $ resolver ;
84
84
}
85
85
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
87
87
{
88
88
$ semaphore = SemaphoreFactory::getInstance ()->create ($ uri , $ conf );
89
89
@@ -96,7 +96,8 @@ public static function create(UriInterface $uri, AuthenticateInterface $auth, Dr
96
96
$ conf ->getUserAgent (),
97
97
$ conf ->getSslConfiguration ()
98
98
),
99
- Cache::getInstance ()
99
+ Cache::getInstance (),
100
+ $ resolver
100
101
);
101
102
}
102
103
@@ -129,17 +130,23 @@ public function acquire(SessionConfiguration $config): Generator
129
130
$ triedAddresses = [];
130
131
131
132
if ($ table == null ) {
132
- $ addresses = $ this ->resolver ->getAddresses ($ this ->data ->getUri ());
133
+ $ addresses = $ this ->resolver ->getAddresses (( string ) $ this ->data ->getUri ());
133
134
foreach ($ addresses as $ address ) {
134
135
$ 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 {
137
138
/** @var BoltConnection $connection */
138
139
$ 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
142
144
}
145
+
146
+ $ this ->cache ->set ($ key , $ table , $ table ->getTtl ());
147
+ $ pool ->release ($ connection );
148
+
149
+ break ;
143
150
}
144
151
}
145
152
0 commit comments