Skip to content

Conversation

SergeC
Copy link

@SergeC SergeC commented Feb 20, 2025

When using a Guzzle Pool, there is no way to understand which response arrived for on_headers callback. This pull request
addresses this issue by passing a request as a second argument to the on_headers callback function, and thus we can
understand to which request this response belongs.

There was a previous attempt to do the same, but PR was closed because it was stale #2828 . Here is an example of code:

require __DIR__ . '/../vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;

$client = new Client();

$requests = static function ($total) {
    for ($i = 1; $i < $total; $i++) {
        yield $i => new Request('GET', 'http://httpbin.org/redirect/' . $i);
    }
};

$pool = new Pool($client, $requests(5), [
    'concurrency' => 5,
    'options' => [
        'fulfilled' => function (Response $response, $index) {
            // this is delivered each successful response
            var_dump($index);
        },
        'rejected' => function (RequestException $reason, $index) {
            // this is delivered each failed request
            var_dump($index);
        },
        'on_headers' => static function (ResponseInterface $response, Request $request) {
            var_dump((string)$request->getUri());
        },
    ],
]);

// Initiate the transfers and create a promise
$promise = $pool->promise();

// Force the pool of requests to complete.
$promise->wait();

@Tomorrowxxy
Copy link

I think this PR should be merged as soon as possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants