8000 feature #32807 [HttpClient] add "max_duration" option (fancyweb) · symfony/http-client-contracts@b803d46 · GitHub
[go: up one dir, main page]

Skip to content

Commit b803d46

Browse files
feature #32807 [HttpClient] add "max_duration" option (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] add "max_duration" option | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#32765 | License | MIT | Doc PR | symfony/symfony-docs#12073 Commits ------- a4178f1369 [HttpClient] add "max_duration" option
2 parents 0e35283 + 43e2bd1 commit b803d46

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

HttpClientInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ interface HttpClientInterface
5353
'proxy' => null, // string - by default, the proxy-related env vars handled by curl SHOULD be honored
5454
'no_proxy' => null, // string - a comma separated list of hosts that do not require a proxy to be reached
5555
'timeout' => null, // float - the idle timeout - defaults to ini_get('default_socket_timeout')
56+
'max_duration' => 0, // float - the maximum execution time for the request+response as a whole;
57+
// a value lower than or equal to 0 means it is unlimited
5658
'bindto' => '0', // string - the interface or the local socket to bind to
5759
'verify_peer' => true, // see https://php.net/context.ssl for the following options
5860
'verify_host' => true,

Test/Fixtures/web/index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@
132132
header('Content-Encoding: gzip');
133133
echo str_repeat('-', 1000);
134134
exit;
135+
136+
case '/max-duration':
137+
ignore_user_abort(false);
138+
while (true) {
139+
echo '<1>';
140+
@ob_flush();
141+
flush();
142+
usleep(500);
143+
}
144+
exit;
135145
}
136146

137147
header('Content-Type: application/json', true);

Test/HttpClientTestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,25 @@ public function testGzipBroken()
777777
$this->expectException(TransportExceptionInterface::class);
778778
$response->getContent();
779779
}
780+
781+
public function testMaxDuration()
782+
{
783+
$client = $this->getHttpClient(__FUNCTION__);
784+
$response = $client->request('GET', 'http://localhost:8057/max-duration', [
785+
'max_duration' => 0.1,
786+
]);
787+
788+
$start = microtime(true);
789+
790+
try {
791+
$response->getContent();
792+
} catch (TransportExceptionInterface $e) {
793+
$this->addToAssertionCount(1);
794+
}
795+
796+
$duration = microtime(true) - $start;
797+
798+
$this->assertGreaterThanOrEqual(0.1, $duration);
799+
$this->assertLessThan(0.2, $duration);
800+
}
780801
}

0 commit comments

Comments
 (0)
0