diff --git a/composer.json b/composer.json index c19794da..f0fb572c 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "amphp/hpack": "^3", "amphp/http": "^2", "amphp/pipeline": "^1", - "amphp/socket": "^2", + "amphp/socket": "^2.5", "amphp/sync": "^2", "league/uri": "^7", "league/uri-components": "^7", diff --git a/src/Connection/ConnectionLimitingPool.php b/src/Connection/ConnectionLimitingPool.php index 1bc38ea7..f01b65e4 100644 --- a/src/Connection/ConnectionLimitingPool.php +++ b/src/Connection/ConnectionLimitingPool.php @@ -65,6 +65,11 @@ private static function formatUri(Request $request): string private int $openConnectionCount = 0; + /** Unlike spl_object_id(), these ids are not reused when a callback is destroyed. */ + private \WeakMap $connectCallbackIds; + + private int $lastConnectCallbackId = 0; + private function __construct(private readonly int $connectionLimit, ?ConnectionFactory $connectionFactory = null) { if ($connectionLimit < 1) { @@ -72,6 +77,7 @@ private function __construct(private readonly int $connectionLimit, ?ConnectionF } $this->connectionFactory = $connectionFactory ?? new DefaultConnectionFactory(); + $this->connectCallbackIds = new \WeakMap(); } public function __clone() @@ -80,6 +86,7 @@ public function __clone() $this->totalConnectionAttempts = 0; $this->totalStreamRequests = 0; $this->openConnectionCount = 0; + $this->connectCallbackIds = new \WeakMap(); } public function getTotalConnectionAttempts(): int @@ -104,6 +111,10 @@ public function getStream(Request $request, Cancellation $cancellation): Stream $uri = self::formatUri($request); + if ($connectCallback = $request->getConnectCallback()) { + $uri .= ' @ ' . ($this->connectCallbackIds[$connectCallback] ??= ++$this->lastConnectCallbackId); + } + [$connection, $stream] = $this->getStreamFor($uri, $request, $cancellation); $connectionId = \spl_object_id($connection); diff --git a/src/Connection/DefaultConnectionFactory.php b/src/Connection/DefaultConnectionFactory.php index b293937f..d8ec1a50 100644 --- a/src/Connection/DefaultConnectionFactory.php +++ b/src/Connection/DefaultConnectionFactory.php @@ -97,6 +97,18 @@ public function create(Request $request, Cancellation $cancellation): Connection $connectContext = $connectContext->withTlsContext($tlsContext); } + if ($connectCallback = $request->getConnectCallback()) { + // The callback of the request must not replace the one of the connect context + if ($contextCallback = $connectContext->getConnectCallback()) { + $connectCallback = static function (Socket\InternetAddress $address) use ($contextCallback, $connectCallback): void { + $contextCallback($address); + $connectCallback($address); + }; + } + + $connectContext = $connectContext->withConnectCallback($connectCallback); + } + try { $socket = $connector->connect( 'tcp://' . $authority, diff --git a/src/Request.php b/src/Request.php index ed000470..f7f86df6 100644 --- a/src/Request.php +++ b/src/Request.php @@ -9,6 +9,8 @@ use Amp\Http\Client\Internal\Phase; use Amp\Http\HttpMessage; use Amp\Http\HttpRequest; +use Amp\Socket\ConnectException; +use Amp\Socket\InternetAddress; use League\Uri; use Psr\Http\Message\UriInterface; use function Amp\async; @@ -34,6 +36,9 @@ final class Request extends HttpRequest private float $tcpConnectTimeout = 10; + /** @var null|\Closure(InternetAddress): void */ + private ?\Closure $connectCallback = null; + private float $tlsHandshakeTimeout = 10; private float $transferTimeout = 10; @@ -367,6 +372,27 @@ public function setTcpConnectTimeout(float $tcpConnectTimeout): void $this->tcpConnectTimeout = \max(0, $tcpConnectTimeout); } + /** + * @return null|\Closure(InternetAddress): void + */ + public function getConnectCallback(): ?\Closure + { + return $this->connectCallback; + } + + /** + * Registers a callback invoked with the IP address of each connection attempt, before the connection is opened. + * + * The callback can throw a {@see ConnectException} to skip the address. Any other exception aborts the request. + * Pooled connections are only shared between requests that use the same callback. + * + * @param null|\Closure(InternetAddress): void $connectCallback + */ + public function setConnectCallback(?\Closure $connectCallback): void + { + $this->connectCallback = $connectCallback; + } + /** * @return float Timeout in seconds for the TLS handshake. */ diff --git a/test/Connection/ConnectionLimitingPoolTest.php b/test/Connection/ConnectionLimitingPoolTest.php index 9a119e79..63049823 100644 --- a/test/Connection/ConnectionLimitingPoolTest.php +++ b/test/Connection/ConnectionLimitingPoolTest.php @@ -224,6 +224,30 @@ public function testWaitingRequestRemovedIfConnectionAttemptFails(): void self::assertSame([], $property->getValue($pool)); } + public function testConnectionsAreOnlySharedWithTheSameConnectCallback(): void + { + $factory = $this->createMock(ConnectionFactory::class); + $factory->expects(self::exactly(3)) + ->method('create') + ->willReturnCallback(fn (Request $request) => $this->createMockConnection($request)); + + $client = (new HttpClientBuilder) + ->usingPool(ConnectionLimitingPool::byAuthority(1, $factory)) + ->build(); + + $callbackA = static function (InternetAddress $address): void { + }; + $callbackB = static function (InternetAddress $address): void { + }; + + foreach ([null, $callbackA, $callbackA, $callbackB, null] as $callback) { + $request = new Request('http://localhost'); + $request->setConnectCallback($callback); + + $client->request($request); + } + } + private function createMockConnection(Request $request): Connection&MockObject { $response = new Response('1.1', 200, null, [], new ReadableBuffer, $request, Future::complete(new Trailers([]))); diff --git a/test/Connection/DefaultConnectionFactoryTest.php b/test/Connection/DefaultConnectionFactoryTest.php new file mode 100644 index 00000000..fed98fb8 --- /dev/null +++ b/test/Connection/DefaultConnectionFactoryTest.php @@ -0,0 +1,76 @@ +getAddress(); + + $request = new Request('http://' . $serverAddress . '/'); + $request->setTransferTimeout(1); + $request->setConnectCallback(static function (InternetAddress $address): void { + throw new \RuntimeException('Blocked ' . $address->toString()); + }); + + try { + (new HttpClientBuilder)->retry(0)->build()->request($request); + self::fail('The request should have been blocked'); + } catch (\RuntimeException $e) { + self::assertSame('Blocked ' . $serverAddress, $e->getMessage()); + } + + try { + $server->accept(new TimeoutCancellation(0.1)); + self::fail('No connection should have been opened'); + } catch (CancelledException) { + $server->close(); + } + } + + public function testConnectCallbackIsAddedToTheOneOfTheConnectContext(): void + { + $calls = []; + + $connector = $this->createMock(Socket\SocketConnector::class); + $connector->expects(self::once()) + ->method('connect') + ->willReturnCallback(static function (string $uri, ConnectContext $context): Socket\Socket { + $context->getConnectCallback()?->__invoke(new InternetAddress('127.0.0.1', 80)); + + throw new Socket\ConnectException('Connection refused'); + }); + + $factory = new DefaultConnectionFactory( + $connector, + (new ConnectContext)->withConnectCallback(static function (InternetAddress $address) use (&$calls): void { + $calls[] = 'context ' . $address->toString(); + }), + ); + + $request = new Request('http://localhost/'); + $request->setConnectCallback(static function (InternetAddress $address) use (&$calls): void { + $calls[] = 'request ' . $address->toString(); + }); + + try { + $factory->create($request, new NullCancellation); + self::fail('The connection should have failed'); + } catch (SocketException) { + self::assertSame(['context 127.0.0.1:80', 'request 127.0.0.1:80'], $calls); + } + } +} diff --git a/test/RequestTest.php b/test/RequestTest.php index 622c63b5..25992ca5 100644 --- a/test/RequestTest.php +++ b/test/RequestTest.php @@ -5,6 +5,7 @@ use Amp\ByteStream\ReadableBuffer; use Amp\Future; use Amp\PHPUnit\AsyncTestCase; +use Amp\Socket\InternetAddress; use function Amp\ByteStream\buffer; class RequestTest extends AsyncTestCase @@ -259,6 +260,24 @@ public function testBodySizeLimit(): void $this->assertSame(100, $request->getBodySizeLimit()); } + public function testConnectCallback(): void + { + $request = new Request('https://amphp.org/'); + + $this->assertNull($request->getConnectCallback()); + + $callback = static function (InternetAddress $address): void { + }; + $request->setConnectCallback($callback); + + $this->assertSame($callback, $request->getConnectCallback()); + $this->assertSame($callback, (clone $request)->getConnectCallback()); + + $request->setConnectCallback(null); + + $this->assertNull($request->getConnectCallback()); + } + public function testIdempotent(): void { $this->assertTrue((new Request('https://localhost/', 'GET'))->isIdempotent());