Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Connection/ConnectionLimitingPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down
60 changes: 60 additions & 0 deletions test/Connection/ConnectionLimitingPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,82 @@
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;
use Amp\Http\Client\Response;
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;
use function Amp\delay;

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();
Expand Down
Loading