Skip to content

Commit 5e03b40

Browse files
committed
Refactoring of HTTP/2 connection processor
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).
1 parent eb41f59 commit 5e03b40

File tree

6 files changed

+244
-415
lines changed

6 files changed

+244
-415
lines changed

src/Connection/ConnectionFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ interface ConnectionFactory
1717
* Additionally, the factory may invoke {@see EventListener::startDnsResolution()} and
1818
* {@see EventListener::completeDnsResolution()}, but is not required to implement such granular events.
1919
*/
20-
public function create(Request $request, Cancellation $cancellationToken): Connection;
20+
public function create(Request $request, Cancellation $cancellation): Connection;
2121
}

src/Connection/DefaultConnectionFactory.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(?Socket\SocketConnector $connector = null, ?ConnectC
2929

3030
public function create(
3131
Request $request,
32-
Cancellation $cancellationToken
32+
Cancellation $cancellation
3333
): Connection {
3434
foreach ($request->getEventListeners() as $eventListener) {
3535
$eventListener->startConnectionCreation($request);
@@ -103,15 +103,15 @@ public function create(
103103
$socket = $connector->connect(
104104
'tcp://' . $authority,
105105
$connectContext->withConnectTimeout($request->getTcpConnectTimeout()),
106-
$cancellationToken
106+
$cancellation
107107
);
108108
} catch (Socket\ConnectException $e) {
109109
throw new UnprocessedRequestException(
110110
new SocketException(\sprintf("Connection to '%s' failed", $authority), 0, $e)
111111
);
112112
} catch (CancelledException $e) {
113113
// In case of a user cancellation request, throw the expected exception
114-
$cancellationToken->throwIfRequested();
114+
$cancellation->throwIfRequested();
115115

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

140140
$tlsCancellation = new CompositeCancellation(
141-
$cancellationToken,
141+
$cancellation,
142142
new TimeoutCancellation($request->getTlsHandshakeTimeout())
143143
);
144144

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

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

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

183183
if ($tlsInfo->getApplicationLayerProtocol() === 'h2') {
184184
$http2Connection = new Http2Connection($socket);
185-
$http2Connection->initialize();
185+
$http2Connection->initialize($cancellation);
186186

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

200200
foreach ($request->getEventListeners() as $eventListener) {
201201
$eventListener->completeConnectionCreation($request);

src/Connection/Http2Connection.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Amp\Socket\EncryptableSocket;
1212
use Amp\Socket\SocketAddress;
1313
use Amp\Socket\TlsInfo;
14+
use Amp\TimeoutCancellation;
1415

1516
final class Http2Connection implements Connection
1617
{
@@ -36,9 +37,9 @@ public function getProtocolVersions(): array
3637
return self::PROTOCOL_VERSIONS;
3738
}
3839

39-
public function initialize(): void
40+
public function initialize(?Cancellation $cancellation = null): void
4041
{
41-
$this->processor->initialize();
42+
$this->processor->initialize($cancellation ?? new TimeoutCancellation(5));
4243
}
4344

4445
public function getStream(Request $request): ?Stream

0 commit comments

Comments
 (0)