From a90a6c9c4849a1342ebee66417e1046470b81eaf Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 27 Dec 2019 11:24:52 +0100 Subject: [PATCH] [HttpClient] fix scheduling pending NativeResponse --- .../HttpClient/Internal/NativeClientState.php | 4 -- .../HttpClient/Response/NativeResponse.php | 43 ++++++++++--------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/HttpClient/Internal/NativeClientState.php b/src/Symfony/Component/HttpClient/Internal/NativeClientState.php index e82ce4c853d2..6578929dc546 100644 --- a/src/Symfony/Component/HttpClient/Internal/NativeClientState.php +++ b/src/Symfony/Component/HttpClient/Internal/NativeClientState.php @@ -11,8 +11,6 @@ namespace Symfony\Component\HttpClient\Internal; -use Symfony\Component\HttpClient\Response\NativeResponse; - /** * Internal representation of the native client's state. * @@ -24,8 +22,6 @@ final class NativeClientState extends ClientState { /** @var int */ public $id; - /** @var NativeResponse[] */ - public $pendingResponses = []; /** @var int */ public $maxHostConnections = PHP_INT_MAX; /** @var int */ diff --git a/src/Symfony/Component/HttpClient/Response/NativeResponse.php b/src/Symfony/Component/HttpClient/Response/NativeResponse.php index b8af399a94b1..49e25c51bc03 100644 --- a/src/Symfony/Component/HttpClient/Response/NativeResponse.php +++ b/src/Symfony/Component/HttpClient/Response/NativeResponse.php @@ -209,11 +209,7 @@ private static function schedule(self $response, array &$runningResponses): void $runningResponses[$i] = [$response->multi, []]; } - if (null === $response->remaining) { - $response->multi->pendingResponses[] = $response; - } else { - $runningResponses[$i][1][$response->id] = $response; - } + $runningResponses[$i][1][$response->id] = $response; if (null === $response->buffer) { // Response already completed @@ -315,25 +311,30 @@ private static function perform(NativeClientState $multi, array &$responses = nu return; } - if ($multi->pendingResponses && \count($multi->handles) < $multi->maxHostConnections) { - // Open the next pending request - this is a blocking operation so we do only one of them - /** @var self $response */ - $response = array_shift($multi->pendingResponses); - $response->open(); - $responses[$response->id] = $response; - $multi->sleep = false; - self::perform($response->multi); - - if (null !== $response->handle) { - $multi->handles[] = $response->handle; + // Create empty activity lists to tell ResponseTrait::stream() we still have pending requests + foreach ($responses as $i => $response) { + if (null === $response->remaining && null !== $response->buffer) { + $multi->handlesActivity[$i] = []; } } - if ($multi->pendingResponses) { - // Create empty activity list to tell ResponseTrait::stream() we still have pending requests - $response = $multi->pendingResponses[0]; - $responses[$response->id] = $response; - $multi->handlesActivity[$response->id] = []; + if (\count($multi->handles) >= $multi->maxHostConnections) { + return; + } + + // Open the next pending request - this is a blocking operation so we do only one of them + foreach ($responses as $i => $response) { + if (null === $response->remaining && null !== $response->buffer) { + $response->open(); + $multi->sleep = false; + self::perform($multi); + + if (null !== $response->handle) { + $multi->handles[] = $response->handle; + } + + break; + } } }