diff --git a/src/Connection/ConnectionLimitingPool.php b/src/Connection/ConnectionLimitingPool.php index 1bc38ea7..66f3bbcb 100644 --- a/src/Connection/ConnectionLimitingPool.php +++ b/src/Connection/ConnectionLimitingPool.php @@ -159,13 +159,15 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance try { if ($isHttps && ($this->waitForPriorConnection[$uri] ?? true)) { // Wait for first successful connection if using a secure connection (maybe we can use HTTP/2). - $connection = $connectionFuture->await(); + $connection = $connectionFuture->await($cancellation); } elseif ($connectionFuture->isComplete()) { - $connection = $connectionFuture->await(); + $connection = $connectionFuture->await($cancellation); } else { continue; } } catch (\Exception $exception) { + $cancellation->throwIfRequested(); + continue; // Ignore cancellations and errors of other requests. } @@ -195,7 +197,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance break; } - $connection = $futureFromDeferred->await(); + $connection = $futureFromDeferred->await($cancellation); \assert($connection instanceof Connection); @@ -248,7 +250,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance try { // Await both new connection future and deferred to reuse an existing connection. - $connection = Future\awaitFirst([$connectionFuture, $futureFromDeferred]); + $connection = Future\awaitFirst([$connectionFuture, $futureFromDeferred], $cancellation); } catch (CompositeException $exception) { [$exception] = $exception->getReasons(); // The first reason is why the connection failed. throw $exception; @@ -262,7 +264,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance if ($stream === null) { // Potentially reused connection did not have an available stream for the given request. - $connection = $connectionFuture->await(); // Wait for new connection request instead. + $connection = $connectionFuture->await($cancellation); // Wait for new connection request instead. $stream = $this->getStreamFromConnection($connection, $request); diff --git a/test/Connection/ConnectionLimitingPoolTest.php b/test/Connection/ConnectionLimitingPoolTest.php index 70e9e449..3f6707b0 100644 --- a/test/Connection/ConnectionLimitingPoolTest.php +++ b/test/Connection/ConnectionLimitingPoolTest.php @@ -3,6 +3,9 @@ namespace Amp\Http\Client\Connection; use Amp\ByteStream\ReadableBuffer; +use Amp\Cancellation; +use Amp\CancelledException; +use Amp\DeferredFuture; use Amp\Future; use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Client\Request; @@ -10,8 +13,10 @@ use Amp\Http\Client\SocketException; use Amp\Http\Client\Support\LocalHttpBin; use Amp\Http\Client\Trailers; +use Amp\NullCancellation; use Amp\PHPUnit\AsyncTestCase; use Amp\Socket\InternetAddress; +use Amp\TimeoutCancellation; use PHPUnit\Framework\MockObject\MockObject; use Revolt\EventLoop; use function Amp\async; @@ -19,6 +24,61 @@ class ConnectionLimitingPoolTest extends AsyncTestCase { + public function testCancellationWhileWaitingForConnectionOfAnotherRequest(): void + { + $this->setTimeout(1); + + $deferred = new DeferredFuture(); + + $factory = new class($deferred) implements ConnectionFactory { + public function __construct(private readonly DeferredFuture $deferred) + { + } + + public function create(Request $request, Cancellation $cancellation): Connection + { + return $this->deferred->getFuture()->await(); + } + }; + + $pool = ConnectionLimitingPool::byAuthority(\PHP_INT_MAX, $factory); + + $first = async($pool->getStream(...), new Request('https://example.com/'), new NullCancellation()); + $first->ignore(); + + delay(0); + + $keepLoopAlive = EventLoop::delay(0.5, static fn () => null); + + try { + $this->expectException(CancelledException::class); + + $pool->getStream(new Request('https://example.com/'), new TimeoutCancellation(0.05)); + } finally { + EventLoop::cancel($keepLoopAlive); + $deferred->error(new SocketException('Connection failed')); + } + } + + public function testEstablishedConnectionIsCountedAsOpen(): void + { + $request = new Request('http://localhost'); + + $factory = $this->createMock(ConnectionFactory::class); + $factory->expects(self::once()) + ->method('create') + ->willReturn($this->createMockConnection($request)); + + $pool = ConnectionLimitingPool::byAuthority(1, $factory); + + $pool->getStream($request, new NullCancellation()); + + delay(0); + + self::assertSame(1, $pool->getTotalConnectionAttempts()); + self::assertSame(1, $pool->getOpenConnectionCount()); + } + public function testSingleConnection(): void { $httpBin = LocalHttpBin::startInsecureServer();