11
11
* file that was distributed with this source code.
12
12
*/
13
13
14
- namespace Laudis \Neo4j \Network \ Bolt ;
14
+ namespace Laudis \Neo4j \Network ;
15
15
16
16
use Ds \Map ;
17
17
use Ds \Vector ;
18
18
use Exception ;
19
19
use Laudis \Neo4j \ClientBuilder ;
20
20
use Laudis \Neo4j \Contracts \ClientInterface ;
21
+ use Laudis \Neo4j \Contracts \Injections ;
21
22
use Laudis \Neo4j \Contracts \SessionInterface ;
22
23
use Laudis \Neo4j \Contracts \TransactionInterface ;
23
24
use Laudis \Neo4j \Databags \Statement ;
24
25
use Laudis \Neo4j \Enum \RoutingRoles ;
26
+ use Laudis \Neo4j \Network \Bolt \BoltInjections ;
27
+ use Laudis \Neo4j \Network \Http \HttpInjections ;
25
28
use function parse_url ;
26
29
use function preg_match ;
27
30
use function random_int ;
@@ -32,12 +35,16 @@ final class AutoRoutedSession implements SessionInterface
32
35
private SessionInterface $ referenceSession ;
33
36
private ?ClientInterface $ client = null ;
34
37
private ?RoutingTable $ table = null ;
35
- private BoltInjections $ injections ;
38
+ /** @var BoltInjections|HttpInjections */
39
+ private Injections $ injections ;
36
40
private int $ maxLeader = 0 ;
37
41
private int $ maxFollower = 0 ;
38
42
private array $ parsedUrl ;
39
43
40
- public function __construct (SessionInterface $ referenceSession , BoltInjections $ injections , array $ parsedUrl )
44
+ /**
45
+ * @param BoltInjections|HttpInjections $injections
46
+ */
47
+ public function __construct (SessionInterface $ referenceSession , Injections $ injections , array $ parsedUrl )
41
48
{
42
49
$ this ->referenceSession = $ referenceSession ;
43
50
$ this ->injections = $ injections ;
@@ -81,20 +88,20 @@ private function setupClient(): ClientInterface
81
88
$ values = $ response ->get ('servers ' );
82
89
/** @var int $ttl */
83
90
$ ttl = $ response ->get ('ttl ' );
91
+ if ($ this ->injections instanceof HttpInjections) {
92
+ $ values = $ this ->translateTableToHttp ($ values );
93
+ }
84
94
$ this ->table = new RoutingTable ($ values , time () + $ ttl );
85
95
86
96
$ builder = ClientBuilder::create ();
87
97
$ leaders = $ this ->table ->getWithRole (RoutingRoles::LEADER ());
88
98
$ followers = $ this ->table ->getWithRole (RoutingRoles::FOLLOWER ());
89
99
$ injections = $ this ->injections ->withAutoRouting (false );
90
100
91
- foreach ($ leaders as $ i => $ leader ) {
92
- $ builder = $ builder ->addBoltConnection ('leader- ' .$ i , $ this ->rebuildUrl ($ leader ), $ injections );
93
- $ this ->maxLeader = $ i ;
94
- }
95
- foreach ($ followers as $ i => $ follower ) {
96
- $ builder = $ builder ->addBoltConnection ('follower- ' .$ i , $ this ->rebuildUrl ($ follower ), $ injections );
97
- $ this ->maxFollower = $ i ;
101
+ if ($ injections instanceof BoltInjections) {
102
+ $ builder = $ this ->buildBoltConnections ($ leaders , $ builder , $ injections , $ followers );
103
+ } else {
104
+ $ builder = $ this ->buildHttpConnections ($ leaders , $ builder , $ injections , $ followers );
98
105
}
99
106
100
107
$ this ->client = $ builder ->build ();
@@ -183,9 +190,9 @@ public function commitTransaction(TransactionInterface $transaction, iterable $s
183
190
return $ transaction ->commit ($ statements );
184
191
}
185
192
186
- private function rebuildUrl (string $ url ): string
193
+ private function rebuildUrl (array $ parsedUrl ): string
187
194
{
188
- $ parts = array_merge ($ this ->parsedUrl , parse_url ( $ url ) );
195
+ $ parts = array_merge ($ this ->parsedUrl , $ parsedUrl );
189
196
190
197
return (isset ($ parts ['scheme ' ]) ? "{$ parts ['scheme ' ]}: " : '' ).
191
198
((isset ($ parts ['user ' ]) || isset ($ parts ['host ' ])) ? '// ' : '' ).
@@ -198,4 +205,69 @@ private function rebuildUrl(string $url): string
198
205
(isset ($ parts ['query ' ]) ? "? {$ parts ['query ' ]}" : '' ).
199
206
(isset ($ parts ['fragment ' ]) ? "# {$ parts ['fragment ' ]}" : '' );
200
207
}
208
+
209
+ /**
210
+ * @param Vector<string> $leaders
211
+ * @param Vector<string> $followers
212
+ */
213
+ private function buildBoltConnections (
214
+ Vector $ leaders ,
215
+ ClientBuilder $ builder ,
216
+ BoltInjections $ injections ,
217
+ Vector $ followers
218
+ ): ClientBuilder {
219
+ foreach ($ leaders as $ i => $ leader ) {
220
+ $ builder = $ builder ->addBoltConnection ('leader- ' .$ i , $ this ->rebuildUrl (parse_url ($ leader )), $ injections );
221
+ $ this ->maxLeader = $ i ;
222
+ }
223
+ foreach ($ followers as $ i => $ follower ) {
224
+ $ builder = $ builder ->addBoltConnection ('follower- ' .$ i , $ this ->rebuildUrl (parse_url ($ follower )), $ injections );
225
+ $ this ->maxFollower = $ i ;
226
+ }
227
+
228
+ return $ builder ;
229
+ }
230
+
231
+ /**
232
+ * @param Vector<string> $leaders
233
+ * @param Vector<string> $followers
234
+ */
235
+ private function buildHttpConnections (
236
+ Vector $ leaders ,
237
+ ClientBuilder $ builder ,
238
+ HttpInjections $ injections ,
239
+ Vector $ followers
240
+ ): ClientBuilder {
241
+ foreach ($ leaders as $ i => $ leader ) {
242
+ $ builder = $ builder ->addHttpConnection ('leader- ' .$ i , $ this ->rebuildUrl (parse_url ($ leader )), $ injections );
243
+ $ this ->maxLeader = $ i ;
244
+ }
245
+ foreach ($ followers as $ i => $ follower ) {
246
+ $ builder = $ builder ->addHttpConnection ('follower- ' .$ i , $ this ->rebuildUrl (parse_url ($ follower )), $ injections );
247
+ $ this ->maxFollower = $ i ;
248
+ }
249
+
250
+ return $ builder ;
251
+ }
252
+
253
+ /**
254
+ * @param iterable<array{addresses: list<string>, role:string}> $servers
255
+ *
256
+ * @return iterable<array{addresses: list<string>, role:string}>
257
+ */
258
+ private function translateTableToHttp (iterable $ servers ): iterable
259
+ {
260
+ /** @var list<array{addresses: list<string>, role:string}> */
261
+ $ tbr = [];
262
+
263
+ foreach ($ servers as $ server ) {
264
+ $ row = ['addresses ' => [], 'role ' => $ server ['role ' ]];
265
+ foreach ($ server ['addresses ' ] as $ address ) {
266
+ $ row ['addresses ' ][] = $ this ->rebuildUrl (['host ' => parse_url ($ address , PHP_URL_HOST )]);
267
+ }
268
+ $ tbr [] = $ row ;
269
+ }
270
+
271
+ return $ tbr ;
272
+ }
201
273
}
0 commit comments