Skip to content
Closed
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 25 additions & 1 deletion src/Connection/ConnectionLimitingPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -68,6 +81,9 @@ private static function formatUri(Request $request): string
/** @var bool[] */
private $waitForPriorConnection = [];

/** @var array<string, callable|null> */
private $connectCallbacks = [];

/** @var int */
private $totalConnectionAttempts = 0;

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

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]);
}
}
}
13 changes: 13 additions & 0 deletions src/Connection/DefaultConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private static function clone($value)
/** @var int */
private $tcpConnectTimeout = 10000;

/** @var callable|null */
private $connectCallback;

/** @var int */
private $tlsHandshakeTimeout = 10000;

Expand Down Expand Up @@ -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.
*/
Expand Down
71 changes: 71 additions & 0 deletions test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions test/Connection/ConnectionLimitingPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
102 changes: 102 additions & 0 deletions test/Connection/DefaultConnectionFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Amp\Http\Client\Connection;

use Amp\Failure;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\NullCancellationToken;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\Socket;
use Amp\TimeoutException;
use function Amp\call;

class DefaultConnectionFactoryTest extends AsyncTestCase
{
public function testConnectCallbackIsCalledBeforeConnecting(): \Generator
{
$server = Socket\Server::listen('127.0.0.1:0');
$serverAddress = $server->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;
});
}
}
18 changes: 18 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading