diff --git a/composer.json b/composer.json index 2ffda6d7..d7319fba 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "amphp/byte-stream": "^1.6", "amphp/hpack": "^3", "amphp/http": "^1.6", - "amphp/socket": "^1", + "amphp/socket": "^1.3", "amphp/sync": "^1.3", "league/uri": "^6 | ^7", "psr/http-message": "^1 | ^2" diff --git a/src/Connection/ConnectionLimitingPool.php b/src/Connection/ConnectionLimitingPool.php index 7c64299c..71c51c3f 100644 --- a/src/Connection/ConnectionLimitingPool.php +++ b/src/Connection/ConnectionLimitingPool.php @@ -47,6 +47,19 @@ private static function formatUri(Request $request): string return $scheme . '://' . $authority; } + private static function hashCallable(callable $callable): string + { + if (\is_object($callable)) { + return (string) \spl_object_id($callable); + } + + if (\is_array($callable)) { + return (\is_object($callable[0]) ? \spl_object_id($callable[0]) : $callable[0]) . '::' . $callable[1]; + } + + return $callable; + } + /** @var int */ private $connectionLimit; @@ -68,6 +81,9 @@ private static function formatUri(Request $request): string /** @var bool[] */ private $waitForPriorConnection = []; + /** @var array */ + private $connectCallbacks = []; + /** @var int */ private $totalConnectionAttempts = 0; @@ -90,6 +106,7 @@ private function __construct(int $connectionLimit, ?ConnectionFactory $connectio public function __clone() { $this->connections = []; + $this->connectCallbacks = []; $this->totalConnectionAttempts = 0; $this->totalStreamRequests = 0; $this->openConnectionCount = 0; @@ -117,6 +134,11 @@ public function getStream(Request $request, CancellationToken $cancellation): Pr $uri = self::formatUri($request); + if ($connectCallback = $request->getConnectCallback()) { + // Requests that don't have the same connect callback must not share connections + $uri .= ' @ ' . self::hashCallable($connectCallback); + } + // Using new Coroutine avoids a bug on PHP < 7.4, see #265 /** @@ -231,6 +253,8 @@ private function getStreamFor(string $uri, Request $request, CancellationToken $ $promiseId = \spl_object_id($connectionPromise); $this->connections[$uri] = $this->connections[$uri] ?? new \ArrayObject; $this->connections[$uri][$promiseId] = $connectionPromise; + // Keeps the callback alive so that its id in the key cannot be reused by another callback + $this->connectCallbacks[$uri] = $request->getConnectCallback(); $connectionPromise->onResolve(function (?\Throwable $exception, ?Connection $connection) use ( &$deferred, @@ -360,7 +384,7 @@ private function dropConnection(string $uri, ?int $connectionId, int $promiseId) } if ($this->connections[$uri]->count() === 0) { - unset($this->connections[$uri], $this->waitForPriorConnection[$uri]); + unset($this->connections[$uri], $this->waitForPriorConnection[$uri], $this->connectCallbacks[$uri]); } } } diff --git a/src/Connection/DefaultConnectionFactory.php b/src/Connection/DefaultConnectionFactory.php index 9a08dcfa..6d4906c1 100644 --- a/src/Connection/DefaultConnectionFactory.php +++ b/src/Connection/DefaultConnectionFactory.php @@ -16,6 +16,7 @@ use Amp\Socket\ConnectContext; use Amp\Socket\Connector; use Amp\Socket\EncryptableSocket; +use Amp\Socket\SocketAddress; use Amp\TimeoutCancellationToken; use function Amp\call; use function Amp\Socket\connector; @@ -106,6 +107,18 @@ public function create( $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 (SocketAddress $address) use ($contextCallback, $connectCallback): \Generator { + yield call($contextCallback, $address); + yield call($connectCallback, $address); + }; + } + + $connectContext = $connectContext->withConnectCallback($connectCallback); + } + try { /** @var EncryptableSocket $socket */ $socket = yield $connector->connect( diff --git a/src/Request.php b/src/Request.php index 46ea2aa9..051b74ac 100644 --- a/src/Request.php +++ b/src/Request.php @@ -54,6 +54,9 @@ private static function clone($value) /** @var int */ private $tcpConnectTimeout = 10000; + /** @var callable|null */ + private $connectCallback; + /** @var int */ private $tlsHandshakeTimeout = 10000; @@ -368,6 +371,28 @@ public function setTcpConnectTimeout(int $tcpConnectTimeout): void $this->tcpConnectTimeout = $tcpConnectTimeout; } + /** + * Registers a callback invoked with the IP address of each connection attempt, before the connection is opened. + * + * The callback can throw a {@see \Amp\Socket\ConnectException} to skip the address. Any other exception aborts the + * request. The callback can also return a promise. + * Pooled connections are only shared between requests that use the same callback. + * + * @param callable|null $connectCallback Receives a {@see \Amp\Socket\SocketAddress}. + */ + public function setConnectCallback(?callable $connectCallback): void + { + $this->connectCallback = $connectCallback; + } + + /** + * @return callable|null + */ + public function getConnectCallback(): ?callable + { + return $this->connectCallback; + } + /** * @return int Timeout in milliseconds for the TLS handshake. */ diff --git a/test/ClientHttpBinIntegrationTest.php b/test/ClientHttpBinIntegrationTest.php index 3d9142de..929e9059 100644 --- a/test/ClientHttpBinIntegrationTest.php +++ b/test/ClientHttpBinIntegrationTest.php @@ -572,6 +572,77 @@ public function testRequestCancellation(): \Generator yield $response->getBody()->buffer(); } + public function testConnectCallback(): \Generator + { + $this->givenServer(static function () { + return new ServerResponse(200); + }); + + $addresses = []; + $request = $this->createRequest(); + $request->setConnectCallback(static function (Socket\SocketAddress $address) use (&$addresses) { + $addresses[] = $address->toString(); + }); + + /** @var Response $response */ + $response = yield $this->executeRequest($request); + + $this->assertSame(200, $response->getStatus()); + $this->assertSame([$this->socket->getAddress()->toString()], $addresses); + } + + public function testConnectCallbackCanRejectAnAddress(): \Generator + { + $exception = new Socket\ConnectException('Forbidden address'); + + $request = $this->createRequest(); + $request->setConnectCallback(static function () use ($exception) { + throw $exception; + }); + + try { + yield $this->executeRequest($request); + $this->fail(UnprocessedRequestException::class . ' expected'); + } catch (UnprocessedRequestException $e) { + $this->assertSame($exception, $e->getPrevious()->getPrevious()); + } + } + + public function testConnectionsOfAFreedConnectCallbackAreNotReused(): \Generator + { + $this->givenServer(static function () { + return new ServerResponse(200); + }); + + $callback = static function () { + }; + + $request = $this->createRequest(); + $request->setConnectCallback($callback); + + /** @var Response $response */ + $response = yield $this->executeRequest($request); + yield $response->getBody()->buffer(); + + yield delay(0); + \gc_collect_cycles(); + + // A new closure gets the object id of the previous one if that one has been freed + unset($request, $response, $callback); + $calls = 0; + $callback = static function () use (&$calls) { + ++$calls; + }; + + $request = $this->createRequest(); + $request->setConnectCallback($callback); + + $response = yield $this->executeRequest($request); + yield $response->getBody()->buffer(); + + $this->assertSame(1, $calls); + } + public function testContentLengthBodyMismatchWithTooManyBytesSimple(): \Generator { $this->expectException(InvalidRequestException::class); diff --git a/test/Connection/ConnectionLimitingPoolTest.php b/test/Connection/ConnectionLimitingPoolTest.php index 1994d793..a61ecbb5 100644 --- a/test/Connection/ConnectionLimitingPoolTest.php +++ b/test/Connection/ConnectionLimitingPoolTest.php @@ -112,6 +112,32 @@ public function testConnectionBecomingAvailableWhileConnecting(): \Generator yield [$client->request($request), $client->request($request)]; } + public function testConnectionsAreOnlySharedWithTheSameConnectCallback(): \Generator + { + $factory = $this->createMock(ConnectionFactory::class); + $factory->expects($this->exactly(3)) + ->method('create') + ->willReturnCallback(function (Request $request): Promise { + return new Success($this->createMockConnection($request)); + }); + + $client = (new HttpClientBuilder) + ->usingPool(ConnectionLimitingPool::byAuthority(1, $factory)) + ->build(); + + $callbackA = static function () { + }; + $callbackB = static function () { + }; + + foreach ([null, $callbackA, $callbackA, $callbackB, null] as $callback) { + $request = new Request('http://localhost'); + $request->setConnectCallback($callback); + + yield $client->request($request); + } + } + private function createMockClosableConnection(Request $request): Promise { $content = 'open'; diff --git a/test/Connection/DefaultConnectionFactoryTest.php b/test/Connection/DefaultConnectionFactoryTest.php new file mode 100644 index 00000000..19fb57e1 --- /dev/null +++ b/test/Connection/DefaultConnectionFactoryTest.php @@ -0,0 +1,102 @@ +getAddress()->toString(); + + $request = new Request('http://' . $serverAddress . '/'); + $request->setConnectCallback(static function (Socket\SocketAddress $address) { + throw new \RuntimeException('Blocked ' . $address->toString()); + }); + + try { + yield (new HttpClientBuilder)->retry(0)->build()->request($request); + $this->fail('The request should have been blocked'); + } catch (\RuntimeException $e) { + $this->assertSame('Blocked ' . $serverAddress, $e->getMessage()); + } + + try { + yield Promise\timeout($server->accept(), 100); + $this->fail('No connection should have been opened'); + } catch (TimeoutException $e) { + $server->close(); + } + } + + public function testConnectCallbackOfTheConnectContextIsKept(): \Generator + { + $callback = static function () { + }; + + /** @var Socket\ConnectContext $connectContext */ + $connectContext = yield $this->getConnectContext( + new Request('http://localhost/'), + (new Socket\ConnectContext)->withConnectCallback($callback) + ); + + $this->assertSame($callback, $connectContext->getConnectCallback()); + } + + public function testConnectCallbackIsAddedToTheOneOfTheConnectContext(): \Generator + { + $calls = []; + + $request = new Request('http://localhost/'); + $request->setConnectCallback(static function (Socket\SocketAddress $address) use (&$calls) { + $calls[] = 'request ' . $address->toString(); + }); + + /** @var Socket\ConnectContext $connectContext */ + $connectContext = yield $this->getConnectContext( + $request, + (new Socket\ConnectContext)->withConnectCallback(static function (Socket\SocketAddress $address) use (&$calls) { + $calls[] = 'context ' . $address->toString(); + }) + ); + + yield call($connectContext->getConnectCallback(), new Socket\SocketAddress('127.0.0.1', 80)); + + $this->assertSame(['context 127.0.0.1:80', 'request 127.0.0.1:80'], $calls); + } + + private function getConnectContext(Request $request, Socket\ConnectContext $connectContext): Promise + { + return call(function () use ($request, $connectContext) { + $passedConnectContext = null; + + $connector = $this->createMock(Socket\Connector::class); + $connector->expects($this->once()) + ->method('connect') + ->willReturnCallback(static function (string $uri, Socket\ConnectContext $connectContext) use (&$passedConnectContext): Promise { + $passedConnectContext = $connectContext; + + return new Failure(new Socket\ConnectException('Connection refused')); + }); + + try { + yield (new DefaultConnectionFactory($connector, $connectContext))->create($request, new NullCancellationToken); + $this->fail('The connection should have failed'); + } catch (UnprocessedRequestException $e) { + // The connector always fails + } + + return $passedConnectContext; + }); + } +} diff --git a/test/RequestTest.php b/test/RequestTest.php index daf80aba..84b64534 100644 --- a/test/RequestTest.php +++ b/test/RequestTest.php @@ -270,6 +270,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 () { + }; + $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());