Skip to content

[HttpClient] resolve promise chains on HttplugClient::wait() #33944

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 1 commit into from
Oct 12, 2019

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Oct 10, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? no
Deprecations? no
Tickets Fix #32142
License MIT
Doc PR -

Follow up of #33743

Right now, keeping a reference to promise objects returned by HttplugClient::sendAsyncRequest(), then calling their wait() method is the only way to actually resolve the promises. That's why when these promises are destructed, we cancel the corresponding HTTP request.

But thanks to the HttplugClient::wait() method, we have a hook to tick the event loop managed by the Symfony client.

I added a test case to run into this situation.

It fails currently. I'd like asking @joelwurtz, @dbu and/or maybe @Nyholm if you could have a look and finish this PR? I'm not that familiar with promises and you might get faster and better to the goal. Anyone else is welcome also of course. Thank you for having a look :) PR welcome on my fork or as a separate one on this repos.

@nicolas-grekas
Copy link
Member Author

I figured out the issue: calling \GuzzleHttp\Promise\queue()->run() in the client's loop was missing.

PR ready \o/

@nicolas-grekas
Copy link
Member Author

For the record, the following script runs in 5s:

<?php
  
require 'vendor/autoload.php';

use Symfony\Component\HttpClient\HttplugClient;

$client = new HttplugClient();

$s = microtime(1);

$client->sendAsyncRequest($client->createRequest('GET', 'https://localhost:8000/?sleep=4'));
$client->sendAsyncRequest($client->createRequest('GET', 'https://localhost:8000/?sleep=2'))
    ->then(function ($resp) use ($client) {
        return $client->sendAsyncRequest($client->createRequest('GET', 'https://localhost:8000/?sleep=3'));
    });

$client->wait();

dump(microtime(1) - $s);

nicolas-grekas added a commit that referenced this pull request Oct 12, 2019
…() (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] resolve promise chains on HttplugClient::wait()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #32142
| License       | MIT
| Doc PR        | -

Follow up of #33743

Right now, keeping a reference to promise objects returned by `HttplugClient::sendAsyncRequest()`, then calling their `wait()` method is the only way to actually resolve the promises. That's why when these promises are destructed, we cancel the corresponding HTTP request.

But thanks to the `HttplugClient::wait()` method, we have a hook to tick the event loop managed by the Symfony client.

I added a test case to run into this situation.

~It fails currently. I'd like asking @joelwurtz, @dbu and/or maybe @Nyholm if you could have a look and finish this PR? I'm not that familiar with promises and you might get faster and better to the goal. Anyone else is welcome also of course. Thank you for having a look :) PR welcome on my fork or as a separate one on this repos.~

Commits
-------

ea0be07 [HttpClient] resolve promise chains on HttplugClient::wait()
@nicolas-grekas nicolas-grekas merged commit ea0be07 into symfony:4.4 Oct 12, 2019
@nicolas-grekas nicolas-grekas deleted the hc-promise-wait branch October 12, 2019 09:47
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HttpClient] Getting responses asynchronously as soon as they ready
2 participants