Skip to content

[HttpClient] fix scheduling pending NativeResponse #35120

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\HttpClient\Internal;

use Symfony\Component\HttpClient\Response\NativeResponse;

/**
* Internal representation of the native client's state.
*
Expand All @@ -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 */
Expand Down
43 changes: 22 additions & 21 deletions src/Symfony/Component/HttpClient/Response/NativeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}

Expand Down