Skip to content

Commit

Permalink
Refactoring of HTTP/2 connection processor
Browse files Browse the repository at this point in the history
Reduced complexity of sending request body. Fixed unsubscribing from cancellation token if sending request body fails. Simplified Response object creation. Eliminated idle connection ping (was broken before, could restore if deemed necessary, some servers disconnect on client ping).
  • Loading branch information
trowski committed May 18, 2022
1 parent eb41f59 commit c6833e3
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 414 deletions.
2 changes: 1 addition & 1 deletion src/Connection/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ interface ConnectionFactory
* Additionally, the factory may invoke {@see EventListener::startDnsResolution()} and
* {@see EventListener::completeDnsResolution()}, but is not required to implement such granular events.
*/
public function create(Request $request, Cancellation $cancellationToken): Connection;
public function create(Request $request, Cancellation $cancellation): Connection;
}
14 changes: 7 additions & 7 deletions src/Connection/DefaultConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(?Socket\SocketConnector $connector = null, ?ConnectC

public function create(
Request $request,
Cancellation $cancellationToken
Cancellation $cancellation
): Connection {
foreach ($request->getEventListeners() as $eventListener) {
$eventListener->startConnectionCreation($request);
Expand Down Expand Up @@ -103,15 +103,15 @@ public function create(
$socket = $connector->connect(
'tcp://' . $authority,
$connectContext->withConnectTimeout($request->getTcpConnectTimeout()),
$cancellationToken
$cancellation
);
} catch (Socket\ConnectException $e) {
throw new UnprocessedRequestException(
new SocketException(\sprintf("Connection to '%s' failed", $authority), 0, $e)
);
} catch (CancelledException $e) {
// In case of a user cancellation request, throw the expected exception
$cancellationToken->throwIfRequested();
$cancellation->throwIfRequested();

// Otherwise we ran into a timeout of our TimeoutCancellation
throw new UnprocessedRequestException(new TimeoutException(\sprintf(
Expand All @@ -138,7 +138,7 @@ public function create(
}

$tlsCancellation = new CompositeCancellation(
$cancellationToken,
$cancellation,
new TimeoutCancellation($request->getTlsHandshakeTimeout())
);

Expand All @@ -159,7 +159,7 @@ public function create(
$socket->close();

// In case of a user cancellation request, throw the expected exception
$cancellationToken->throwIfRequested();
$cancellation->throwIfRequested();

// Otherwise we ran into a timeout of our TimeoutCancellation
throw new UnprocessedRequestException(new TimeoutException(\sprintf(
Expand All @@ -182,7 +182,7 @@ public function create(

if ($tlsInfo->getApplicationLayerProtocol() === 'h2') {
$http2Connection = new Http2Connection($socket);
$http2Connection->initialize();
$http2Connection->initialize($cancellation);

foreach ($request->getEventListeners() as $eventListener) {
$eventListener->completeConnectionCreation($request);
Expand All @@ -195,7 +195,7 @@ public function create(
// Treat the presence of only HTTP/2 as prior knowledge, see https://http2.github.io/http2-spec/#known-http
if ($request->getProtocolVersions() === ['2']) {
$http2Connection = new Http2Connection($socket);
$http2Connection->initialize();
$http2Connection->initialize($cancellation);

foreach ($request->getEventListeners() as $eventListener) {
$eventListener->completeConnectionCreation($request);
Expand Down
5 changes: 3 additions & 2 deletions src/Connection/Http2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Amp\Socket\EncryptableSocket;
use Amp\Socket\SocketAddress;
use Amp\Socket\TlsInfo;
use Amp\TimeoutCancellation;

final class Http2Connection implements Connection
{
Expand All @@ -36,9 +37,9 @@ public function getProtocolVersions(): array
return self::PROTOCOL_VERSIONS;
}

public function initialize(): void
public function initialize(?Cancellation $cancellation = null): void
{
$this->processor->initialize();
$this->processor->initialize($cancellation ?? new TimeoutCancellation(5));
}

public function getStream(Request $request): ?Stream
Expand Down
Loading

0 comments on commit c6833e3

Please sign in to comment.