Is it possible to setup Guzzle + Pool over HTTP/2?

Viewed 1358

Guzzle provides a mechanism to send concurrent requests: Pool. I used the example from the docs: http://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests. It works quite fine, sends concurrent requests and everything is awesome except one thing: it seems Guzzle ignores HTTP/2 in this case.

I've prepared a simplified script that sends two requests to https://stackoverflow.com, the first one is using Pool, the second one is just a regular Guzzle request. Only the regular request connects via HTTP/2.

<?php

include_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;

$client = new Client([
    'version' => 2.0,
    'debug' => true
]);

/************************/

$requests = function () {
    yield new Request('GET', 'https://stackoverflow.com');
};
$pool = new Pool($client, $requests());
$promise = $pool->promise();
$promise->wait();

/************************/

$client->get('https://stackoverflow.com', [
    'version' => 2.0,
    'debug' => true,
]);

Here is an output: https://pastebin.com/k0HaDWt6 (I highlighted important parts with "!!!!!")

Does anybody know why Guzzle does this and how to make Pool work with HTTP/2?

1 Answers

Found what was wrong: new Client() doesn't actually accept 'version' as an option if passed to Pool requests are created as new Request(). Either the protocol version must be provided as an option of every request or the requests must be created as $client->getAsync() (or ->postAsync or whatever).

See the corrected code:

...

$client = new Client([
    'debug' => true
]);
$requests = function () {
    yield new Request('GET', 'https://stackoverflow.com', [], null, '2.0');
};
/* OR
$client = new Client([
    'version' => 2.0,
    'debug' => true
]);
$requests = function () use ($client) {
    yield function () use ($client) {
        return $client->getAsync('https://stackoverflow.com');
    };
};
*/
$pool = new Pool($client, $requests());
$promise = $pool->promise();
$promise->wait();

...
Related