-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ final class DefaultConnectionPool implements ConnectionPool | |
/** @var Connector */ | ||
private $connector; | ||
|
||
/** @var Connection[][] */ | ||
/** @var Promise[][] */ | ||
private $connections = []; | ||
|
||
public function __construct(?Connector $connector = null) | ||
|
@@ -36,73 +36,106 @@ public function __construct(?Connector $connector = null) | |
public function getConnection(Request $request, CancellationToken $cancellation): Promise | ||
{ | ||
return call(function () use ($request, $cancellation) { | ||
$isHttps = $request->getUri()->getScheme() === 'https'; | ||
$uri = $request->getUri(); | ||
$isHttps = $uri->getScheme() === 'https'; | ||
$defaultPort = $isHttps ? 443 : 80; | ||
|
||
$authority = $request->getUri()->getHost() . ':' . ($request->getUri()->getPort() ?: $defaultPort); | ||
$authority = $uri->getHost() . ':' . ($uri->getPort() ?: $defaultPort); | ||
$key = $uri->getScheme() . '://' . $authority; | ||
|
||
if (!empty($this->connections[$key])) { | ||
foreach ($this->connections[$key] as $index => $connection) { | ||
if ($connection instanceof Promise) { | ||
$connection = yield $connection; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
trowski
Author
Member
|
||
} | ||
|
||
if (isset($this->connections[$authority])) { | ||
foreach ($this->connections[$authority] as $connection) { | ||
\assert($connection instanceof Connection); | ||
|
||
if (!$connection->isBusy()) { | ||
return $connection; | ||
} | ||
} | ||
} | ||
|
||
$connectContext = new ConnectContext; | ||
|
||
if ($isHttps) { | ||
$tlsContext = ($connectContext->getTlsContext() ?? new ClientTlsContext($request->getUri()->getHost())) | ||
->withApplicationLayerProtocols(self::APPLICATION_LAYER_PROTOCOLS) | ||
->withPeerCapturing(); | ||
|
||
$connectContext = $connectContext->withTlsContext($tlsContext); | ||
++$index; | ||
} else { | ||
$this->connections[$key] = []; | ||
$index = 0; | ||
} | ||
|
||
try { | ||
$checkoutCancellationToken = new CombinedCancellationToken($cancellation, new TimeoutCancellationToken($request->getTcpConnectTimeout())); | ||
$promise = $this->connections[$key][$index] = call(function () use ($request, $isHttps, $authority, $cancellation, $key, $index) { | ||
$connectContext = new ConnectContext; | ||
|
||
/** @var EncryptableSocket $socket */ | ||
$socket = yield $this->connector->connect('tcp://' . $authority, $connectContext, $checkoutCancellationToken); | ||
} catch (Socket\ConnectException $e) { | ||
throw new SocketException(\sprintf("Connection to '%s' failed", $authority), 0, $e); | ||
} catch (CancelledException $e) { | ||
// In case of a user cancellation request, throw the expected exception | ||
$cancellation->throwIfRequested(); | ||
if ($isHttps) { | ||
$tlsContext = ($connectContext->getTlsContext() ?? new ClientTlsContext($request->getUri()->getHost())) | ||
->withApplicationLayerProtocols(self::APPLICATION_LAYER_PROTOCOLS) | ||
->withPeerCapturing(); | ||
|
||
// Otherwise we ran into a timeout of our TimeoutCancellationToken | ||
throw new TimeoutException(\sprintf("Connection to '%s' timed out, took longer than " . $request->getTcpConnectTimeout() . ' ms', $authority)); // don't pass $e | ||
} | ||
$connectContext = $connectContext->withTlsContext($tlsContext); | ||
} | ||
|
||
if ($isHttps) { | ||
try { | ||
$tlsState = $socket->getTlsState(); | ||
if ($tlsState === EncryptableSocket::TLS_STATE_DISABLED) { | ||
$tlsCancellationToken = new CombinedCancellationToken($cancellation, new TimeoutCancellationToken($request->getTlsHandshakeTimeout())); | ||
yield $socket->setupTls($tlsCancellationToken); | ||
} elseif ($tlsState !== EncryptableSocket::TLS_STATE_ENABLED) { | ||
throw new SocketException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState . ')'); | ||
} | ||
} catch (StreamException $exception) { | ||
throw new SocketException(\sprintf("Connection to '%s' closed during TLS handshake", $authority), 0, $exception); | ||
$checkoutCancellationToken = new CombinedCancellationToken($cancellation, new TimeoutCancellationToken($request->getTcpConnectTimeout())); | ||
|
||
/** @var EncryptableSocket $socket */ | ||
$socket = yield $this->connector->connect('tcp://' . $authority, $connectContext, $checkoutCancellationToken); | ||
} catch (Socket\ConnectException $e) { | ||
throw new SocketException(\sprintf("Connection to '%s' failed", $authority), 0, $e); | ||
} catch (CancelledException $e) { | ||
// In case of a user cancellation request, throw the expected exception | ||
$cancellation->throwIfRequested(); | ||
|
||
// Otherwise we ran into a timeout of our TimeoutCancellationToken | ||
throw new TimeoutException(\sprintf("TLS handshake with '%s' @ '%s' timed out, took longer than " . $request->getTlsHandshakeTimeout() . ' ms', $authority, $socket->getRemoteAddress()->toString())); // don't pass $e | ||
throw new TimeoutException(\sprintf("Connection to '%s' timed out, took longer than " . $request->getTcpConnectTimeout() . ' ms', $authority)); // don't pass $e | ||
} | ||
} | ||
|
||
if (!isset($this->connections[$authority])) { | ||
$this->connections[$authority] = []; | ||
} | ||
if ($isHttps) { | ||
try { | ||
$tlsState = $socket->getTlsState(); | ||
if ($tlsState === EncryptableSocket::TLS_STATE_DISABLED) { | ||
$tlsCancellationToken = new CombinedCancellationToken($cancellation, new TimeoutCancellationToken($request->getTlsHandshakeTimeout())); | ||
yield $socket->setupTls($tlsCancellationToken); | ||
} elseif ($tlsState !== EncryptableSocket::TLS_STATE_ENABLED) { | ||
throw new SocketException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState . ')'); | ||
} | ||
} catch (StreamException $exception) { | ||
throw new SocketException(\sprintf("Connection to '%s' closed during TLS handshake", $authority), 0, $exception); | ||
} catch (CancelledException $e) { | ||
// In case of a user cancellation request, throw the expected exception | ||
$cancellation->throwIfRequested(); | ||
|
||
// Otherwise we ran into a timeout of our TimeoutCancellationToken | ||
throw new TimeoutException(\sprintf("TLS handshake with '%s' @ '%s' timed out, took longer than " . $request->getTlsHandshakeTimeout() . ' ms', $authority, $socket->getRemoteAddress()->toString())); // don't pass $e | ||
} | ||
} | ||
|
||
$connection = new Http1Connection($socket); | ||
|
||
$connections = &$this->connections; | ||
$connection->onClose(static function () use (&$connections, $key, $index) { | ||
unset($connections[$key][$index]); | ||
|
||
if (empty($connections[$key])) { | ||
unset($connections[$key]); | ||
} | ||
}); | ||
|
||
return $connection; | ||
}); | ||
|
||
$promise->onResolve(function (?\Throwable $exception, ?Connection $connection) use ($key, $index): void { | ||
if ($exception) { | ||
unset($this->connections[$key][$index]); | ||
|
||
if (empty($this->connections[$key])) { | ||
unset($this->connections[$key]); | ||
} | ||
return; | ||
} | ||
|
||
$connection = new Http1Connection($socket); | ||
$this->connections[$authority][] = $connection; | ||
$this->connections[$key][$index] = $connection; | ||
}); | ||
|
||
return $connection; | ||
return $promise; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Should we really await the connection here? I guess we have to in order to share a single HTTP/2 connection if HTTP/2 is available.