Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Getting responses asynchronously as soon as they ready #32142

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

Closed
TiGR opened this issue Jun 23, 2019 · 4 comments · Fixed by #33743 or #33944
Closed

[HttpClient] Getting responses asynchronously as soon as they ready #32142

TiGR opened this issue Jun 23, 2019 · 4 comments · Fixed by #33743 or #33944

Comments

@TiGR
Copy link
Contributor

TiGR commented Jun 23, 2019

Here is the deal: I use Symfony and HttpClient in telegram bot. Bot sends various requests to Telegram's API, and most of these I don't really care to get the response in main bot logic (I want to run the code as fast as possible). However, I'd like to add requests and responses to Sentry breadcrumbs for reporting. Adding requests is easy. Adding responses is tricky.

I don't want actually to call Response->getContent(), so that it would stop main logic execution until I get the response. However, I'd like to somehow get notified as soon as response is ready. Then, I'd like to add the response to breadcrumbs, and then if there were errors in responses - send a notification.

I've tried subscribing to on_progress, but that didn't work, since responses were not ready when I tried using them from that callback.

So, I couldn't find anything about some sort of event system, so that I could subscribe and get responses as soon as they get ready. Is it really possible? And if not, then it's a feature request, I guess...

@nicolas-grekas
Copy link
Member

Thanks for sharing your use case. In the doc, I tried to explain what the contracts provide to fit this use case:
https://symfony.com/doc/current/components/http_client.html#multiplexing-responses

This allows building what you're describing without much complexity. Can you figure out how?
I'm interested to know before giving you more hints, to challenge the doc :)

@TiGR
Copy link
Contributor Author

TiGR commented Jun 25, 2019

So, it seems that there is no way to achieve this without interrupting main program flow for this (you have to switch to that foreach cycle, and be there until the very last request is finished). I just thought it might be possible to get it somehow out of on_progress callback, or to have another callback for that (say, on_complete).

But it seems that at some point I have to switch to analyzing all responses, maybe on kernel exception event, currently I do it on kernel terminate event.

@nicolas-grekas
Copy link
Member

Somehow :)
See #32174

@nicolas-grekas
Copy link
Member

Will be fixed by #33743

nicolas-grekas added a commit that referenced this issue 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment