Skip to content

[HttpClient] Honor "max_duration" when replacing requests with async decorators #46382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2022
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/Response/AmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
$info['upload_content_length'] = -1.0;
$info['download_content_length'] = -1.0;
$info['user_data'] = $options['user_data'];
$info['max_duration'] = $options['max_duration'];
$info['debug'] = '';

$onProgress = $options['on_progress'] ?? static function () {};
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpClient/Response/AsyncContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpClient\Chunk\DataChunk;
use Symfony\Component\HttpClient\Chunk\LastChunk;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\ChunkInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -152,13 +153,18 @@ public function getResponse(): ResponseInterface
*/
public function replaceRequest(string $method, string $url, array $options = []): ResponseInterface
{
$this->info['previous_info'][] = $this->response->getInfo();
$this->info['previous_info'][] = $info = $this->response->getInfo();
if (null !== $onProgress = $options['on_progress'] ?? null) {
$thisInfo = &$this->info;
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
$onProgress($dlNow, $dlSize, $thisInfo + $info);
};
}
if (0 < ($info['max_duration'] ?? 0) && 0 < ($info['total_time'] ?? 0)) {
if (0 >= $options['max_duration'] = $info['max_duration'] - $info['total_time']) {
throw new TransportException(sprintf('Max duration was reached for "%s".', $info['url']));
}
}

return $this->response = $this->client->request($method, $url, ['buffer' => false] + $options);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpClient/Response/AsyncResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function __construct(HttpClientInterface $client, string $method, string
if (\array_key_exists('user_data', $options)) {
$this->info['user_data'] = $options['user_data'];
}
if (\array_key_exists('max_duration', $options)) {
$this->info['max_duration'] = $options['max_duration'];
}
}

public function getStatusCode(): int
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
$this->timeout = $options['timeout'] ?? null;
$this->info['http_method'] = $method;
$this->info['user_data'] = $options['user_data'] ?? null;
$this->info['max_duration'] = $options['max_duration'] ?? null;
$this->info['start_time'] = $this->info['start_time'] ?? microtime(true);
$info = &$this->info;
$headers = &$this->headers;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static function fromRequest(string $method, string $url, array $options,
$response->info['http_method'] = $method;
$response->info['http_code'] = 0;
$response->info['user_data'] = $options['user_data'] ?? null;
$response->info['max_duration'] = $options['max_duration'] ?? null;
$response->info['url'] = $url;

if ($mock instanceof self) {
Expand Down Expand Up @@ -285,6 +286,7 @@ private static function readResponse(self $response, array $options, ResponseInt
$response->info = [
'start_time' => $response->info['start_time'],
'user_data' => $response->info['user_data'],
'max_duration' => $response->info['max_duration'],
'http_code' => $response->info['http_code'],
] + $info + $response->info;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
$this->buffer = fopen('php://temp', 'w+');

$info['user_data'] = $options['user_data'];
$info['max_duration'] = $options['max_duration'];
++$multi->responseCount;

$this->initializer = static function (self $response) {
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpClient\AsyncDecoratorTrait;
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\AsyncResponse;
Expand Down Expand Up @@ -339,4 +340,28 @@ public function request(string $method, string $url, array $options = []): Respo
$this->expectExceptionMessage('Instance of "Symfony\Component\HttpClient\Response\NativeResponse" is already consumed and cannot be managed by "Symfony\Component\HttpClient\Response\AsyncResponse". A decorated client should not call any of the response\'s methods in its "request()" method.');
$response->getStatusCode();
}

public function testMaxDuration()
{
$sawFirst = false;
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use (&$sawFirst) {
try {
if (!$chunk->isFirst() || !$sawFirst) {
$sawFirst = $sawFirst || $chunk->isFirst();
yield $chunk;
}
} catch (TransportExceptionInterface $e) {
$context->getResponse()->cancel();
$context->replaceRequest('GET', 'http://localhost:8057/timeout-body', ['timeout' => 0.4]);
}
});

$response = $client->request('GET', 'http://localhost:8057/timeout-body', ['max_duration' => 0.75, 'timeout' => 0.4]);

$this->assertSame(0.75, $response->getInfo('max_duration'));

$this->expectException(TransportException::class);
$this->expectExceptionMessage('Max duration was reached for "http://localhost:8057/timeout-body".');
$response->getContent();
}
}