Skip to content

Commit 5e49536

Browse files
committed
reworked http connections
1 parent 3c18f1d commit 5e49536

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

src/Http/HttpConnectionPool.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function json_encode;
1818
use Laudis\Neo4j\Common\ConnectionConfiguration;
1919
use Laudis\Neo4j\Common\Resolvable;
20+
use Laudis\Neo4j\Common\Uri;
2021
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2122
use Laudis\Neo4j\Contracts\ConnectionInterface;
2223
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
@@ -49,41 +50,44 @@ final class HttpConnectionPool implements ConnectionPoolInterface
4950
* @psalm-readonly
5051
*/
5152
private Resolvable $streamFactory;
52-
private UriInterface $uri;
5353
private AuthenticateInterface $auth;
5454
private string $userAgent;
55+
/** @var Resolvable<string> */
56+
private Resolvable $tsxUrl;
5557

5658
/**
5759
* @param Resolvable<StreamFactoryInterface> $streamFactory
5860
* @param Resolvable<RequestFactory> $requestFactory
5961
* @param Resolvable<ClientInterface> $client
62+
* @param Resolvable<string> $tsxUrl
6063
*
6164
* @psalm-mutation-free
6265
*/
6366
public function __construct(
6467
Resolvable $client,
6568
Resolvable $requestFactory,
6669
Resolvable $streamFactory,
67-
UriInterface $uri,
6870
AuthenticateInterface $auth,
69-
string $userAgent
71+
string $userAgent,
72+
Resolvable $tsxUrl
7073
) {
7174
$this->client = $client;
7275
$this->requestFactory = $requestFactory;
7376
$this->streamFactory = $streamFactory;
74-
$this->uri = $uri;
7577
$this->auth = $auth;
7678
$this->userAgent = $userAgent;
79+
$this->tsxUrl = $tsxUrl;
7780
}
7881

7982
public function acquire(SessionConfiguration $config): Generator
8083
{
8184
yield 0.0;
8285

83-
$request = $this->requestFactory->resolve()->createRequest('POST', $this->uri);
86+
$uri = Uri::create($this->tsxUrl->resolve());
87+
$request = $this->requestFactory->resolve()->createRequest('POST', $uri);
8488

8589
$path = $request->getUri()->getPath().'/commit';
86-
$uri = $this->uri->withPath($path);
90+
$uri = $uri->withPath($path);
8791
$request = $request->withUri($uri);
8892

8993
$body = json_encode([

src/Http/HttpDriver.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,57 +111,40 @@ public function createSession(?SessionConfiguration $config = null): SessionInte
111111
$config = $config->merge(SessionConfiguration::fromUri($this->uri));
112112
$streamFactoryResolve = $this->streamFactory();
113113

114+
$tsxUrl = $this->tsxUrl($config);
115+
114116
return new HttpSession(
115117
$streamFactoryResolve,
116-
$this->getHttpConnectionPool(),
118+
$this->getHttpConnectionPool($tsxUrl),
117119
$config,
118120
$this->formatter,
119121
$factory,
120-
Resolvable::once($this->key.':tsxUrl', function () use ($config, $factory) {
121-
$database = $config->getDatabase() ?? 'neo4j';
122-
$request = $factory->resolve()->createRequest('GET', $this->uri);
123-
$client = $this->config->getHttpPsrBindings()->getClient();
124-
125-
$response = $client->sendRequest($request);
126-
127-
$discovery = HttpHelper::interpretResponse($response);
128-
/** @var string|null */
129-
$version = $discovery->neo4j_version ?? null;
130-
131-
if ($version === null) {
132-
/** @var string */
133-
$uri = $discovery->data;
134-
$request = $request->withUri(Uri::create($uri));
135-
$discovery = HttpHelper::interpretResponse($client->sendRequest($request));
136-
}
137-
138-
/** @var string */
139-
$tsx = $discovery->transaction;
140-
141-
return str_replace('{databaseName}', $database, $tsx);
142-
}),
122+
$tsxUrl,
143123
$this->auth,
144124
$this->config->getUserAgent()
145125
);
146126
}
147127

148128
public function verifyConnectivity(?SessionConfiguration $config = null): bool
149129
{
150-
return $this->getHttpConnectionPool()->canConnect($this->uri, $this->auth);
130+
return $this->getHttpConnectionPool($this->tsxUrl($config ?? SessionConfiguration::default()))
131+
->canConnect($this->uri, $this->auth);
151132
}
152133

153134
/**
135+
* @param Resolvable<string> $tsxUrl
136+
*
154137
* @psalm-mutation-free
155138
*/
156-
private function getHttpConnectionPool(): HttpConnectionPool
139+
private function getHttpConnectionPool(Resolvable $tsxUrl): HttpConnectionPool
157140
{
158141
return new HttpConnectionPool(
159142
Resolvable::once($this->key.':client', fn () => $this->config->getHttpPsrBindings()->getClient()),
160143
$this->resolvableFactory(),
161144
$this->streamFactory(),
162-
$this->uri,
163145
$this->auth,
164-
$this->config->getUserAgent()
146+
$this->config->getUserAgent(),
147+
$tsxUrl
165148
);
166149
}
167150

@@ -188,4 +171,36 @@ private function streamFactory(): Resolvable
188171
{
189172
return Resolvable::once($this->key.':streamFactory', fn () => $this->config->getHttpPsrBindings()->getStreamFactory());
190173
}
174+
175+
/**
176+
* @return Resolvable<string>
177+
*
178+
* @psalm-mutation-free
179+
*/
180+
private function tsxUrl(SessionConfiguration $config): Resolvable
181+
{
182+
return Resolvable::once($this->key.':tsxUrl', function () use ($config) {
183+
$database = $config->getDatabase() ?? 'neo4j';
184+
$request = $this->resolvableFactory()->resolve()->createRequest('GET', $this->uri);
185+
$client = $this->config->getHttpPsrBindings()->getClient();
186+
187+
$response = $client->sendRequest($request);
188+
189+
$discovery = HttpHelper::interpretResponse($response);
190+
/** @var string|null */
191+
$version = $discovery->neo4j_version ?? null;
192+
193+
if ($version === null) {
194+
/** @var string */
195+
$uri = $discovery->data;
196+
$request = $request->withUri(Uri::create($uri));
197+
$discovery = HttpHelper::interpretResponse($client->sendRequest($request));
198+
}
199+
200+
/** @var string */
201+
$tsx = $discovery->transaction;
202+
203+
return str_replace('{databaseName}', $database, $tsx);
204+
});
205+
}
191206
}

src/Http/HttpSession.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ final class HttpSession implements SessionInterface
6969
* @var Resolvable<string>
7070
*/
7171
private Resolvable $uri;
72-
/** @psalm-readonly */
73-
private AuthenticateInterface $auth;
74-
/** @psalm-readonly */
75-
private string $userAgent;
7672

7773
/**
7874
* @psalm-mutation-free
@@ -98,8 +94,6 @@ public function __construct(
9894
$this->formatter = $formatter;
9995
$this->requestFactory = $requestFactory;
10096
$this->uri = $uri;
101-
$this->auth = $auth;
102-
$this->userAgent = $userAgent;
10397
}
10498

10599
/**

0 commit comments

Comments
 (0)