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

Store if an H2 connection was made #247

Merged
merged 1 commit into from
Jan 9, 2020
Merged
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
25 changes: 16 additions & 9 deletions src/Connection/UnlimitedConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ final class UnlimitedConnectionPool implements ConnectionPool
/** @var Promise[][] */
private $connections = [];

/** @var bool[] */
private $waitForPriorConnection = [];

/** @var int */
private $totalConnectionAttempts = 0;

Expand Down Expand Up @@ -76,15 +79,15 @@ public function getStream(Request $request, CancellationToken $cancellation): Pr
}

$authority = $host . ':' . $port;
$key = $scheme . '://' . $authority;
$uri = $scheme . '://' . $authority;

$connections = $this->connections[$key] ?? new \ArrayObject;
$connections = $this->connections[$uri] ?? new \ArrayObject;

foreach ($connections as $connectionPromise) {
\assert($connectionPromise instanceof Promise);

try {
if ($isHttps) {
if ($isHttps && ($this->waitForPriorConnection[$uri] ?? true)) {
// Wait for first successful connection if using a secure connection (maybe we can use HTTP/2).
$connection = yield $connectionPromise;
} else {
Expand Down Expand Up @@ -117,23 +120,27 @@ public function getStream(Request $request, CancellationToken $cancellation): Pr
$connectionPromise = $this->connectionFactory->create($request, $cancellation);

$hash = \spl_object_hash($connectionPromise);
$this->connections[$key] = $this->connections[$key] ?? new \ArrayObject;
$this->connections[$key][$hash] = $connectionPromise;
$this->connections[$uri] = $this->connections[$uri] ?? new \ArrayObject;
$this->connections[$uri][$hash] = $connectionPromise;

try {
$connection = yield $connectionPromise;
$this->openConnectionCount++;

\assert($connection instanceof Connection);
} catch (\Throwable $exception) {
$this->dropConnection($key, $hash);
$this->dropConnection($uri, $hash);

throw $exception;
}

$connection->onClose(function () use ($key, $hash): void {
if ($isHttps) {
$this->waitForPriorConnection[$uri] = \in_array('2', $connection->getProtocolVersions());
}

$connection->onClose(function () use ($uri, $hash): void {
$this->openConnectionCount--;
$this->dropConnection($key, $hash);
$this->dropConnection($uri, $hash);
});

$stream = yield $connection->getStream($request);
Expand All @@ -149,7 +156,7 @@ private function dropConnection(string $uri, string $connectionHash): void
unset($this->connections[$uri][$connectionHash]);

if (empty($this->connections[$uri])) {
unset($this->connections[$uri]);
unset($this->connections[$uri], $this->waitForPriorConnection[$uri]);
}
}
}