-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
18 changed files
with
294 additions
and
281 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection; | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\Response; | ||
use Amp\Promise; | ||
use Amp\Socket\SocketAddress; | ||
use Amp\Socket\TlsInfo; | ||
|
||
interface Connection | ||
{ | ||
/** | ||
* @param Request $request | ||
* @param CancellationToken|null $token | ||
* | ||
* @return Promise<Response> | ||
*/ | ||
public function request(Request $request, ?CancellationToken $token = null): Promise; | ||
|
||
public function isBusy(): bool; | ||
|
||
public function isClosed(): bool; | ||
|
||
public function close(): Promise; | ||
|
||
public function getLocalAddress(): SocketAddress; | ||
|
||
public function getRemoteAddress(): SocketAddress; | ||
|
||
public function getTlsInfo(): ?TlsInfo; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection; | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Http\Client\Request; | ||
use Amp\Promise; | ||
|
||
interface ConnectionPool | ||
{ | ||
/** | ||
* @param Request $request | ||
* @param CancellationToken $token | ||
* | ||
* @return Promise<Connection> | ||
*/ | ||
public function getConnection(Request $request, CancellationToken $token): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection; | ||
|
||
use Amp\ByteStream\StreamException; | ||
use Amp\CancellationToken; | ||
use Amp\CancelledException; | ||
use Amp\Http\Client\Internal\CombinedCancellationToken; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\SocketException; | ||
use Amp\Http\Client\TimeoutException; | ||
use Amp\Promise; | ||
use Amp\Socket\ClientTlsContext; | ||
use Amp\Socket\ConnectContext; | ||
use Amp\Socket\Connector; | ||
use Amp\Socket; | ||
use Amp\Socket\EncryptableSocket; | ||
use Amp\TimeoutCancellationToken; | ||
use function Amp\call; | ||
|
||
final class DefaultConnectionPool implements ConnectionPool | ||
{ | ||
private const APPLICATION_LAYER_PROTOCOLS = ['http/1.1']; | ||
|
||
/** @var Connector */ | ||
private $connector; | ||
|
||
/** @var Connection[][] */ | ||
private $connections = []; | ||
|
||
public function __construct(?Connector $connector = null) | ||
{ | ||
$this->connector = $connector ?? Socket\connector(); | ||
} | ||
|
||
public function getConnection(Request $request, CancellationToken $cancellation): Promise | ||
{ | ||
return call(function () use ($request, $cancellation) { | ||
$isHttps = $request->getUri()->getScheme() === 'https'; | ||
$defaultPort = $isHttps ? 443 : 80; | ||
|
||
$authority = $request->getUri()->getHost() . ':' . ($request->getUri()->getPort() ?: $defaultPort); | ||
|
||
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); | ||
} | ||
|
||
try { | ||
$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("Connection to '%s' timed out, took longer than " . $request->getTcpConnectTimeout() . ' ms', $authority)); // don't pass $e | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
if (!isset($this->connections[$authority])) { | ||
$this->connections[$authority] = []; | ||
} | ||
|
||
$connection = new Http1Connection($socket); | ||
$this->connections[$authority][] = $connection; | ||
|
||
return $connection; | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.