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

Skip to content

[HttpClient] Fix promise behavior in HttplugClient #37491

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
Jul 4, 2020
Merged

[HttpClient] Fix promise behavior in HttplugClient #37491

merged 1 commit into from
Jul 4, 2020

Conversation

bohanyang
Copy link
Contributor

Q A
Branch? 4.4 & up
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #37488
License MIT
Doc PR symfony/symfony-docs#...

The Problem

Promises have 2 important methods: then and wait.
To implement Httplug's promise interface, we built HttplugPromise on top of Guzzle promise.
However, when an error occurred (Httplug NetworkException thrown) while init the request/before actually sending the request,
HttplugClient::sendAsyncRequest will return a Http\Promise\RejectedPromise, which is a dummy implementation.

If the then callable returns a promise-like object, Http\Promise\RejectedPromise will treat it as plain value.
Guzzle promise will try to resolve the promise-like value, which is an object that has then method on it.

https://github.com/guzzle/promises/blob/bbf3b200bc83c1e9298580a9f99b9be248543467/src/Promise.php#L116

To fix this, I edited src/Symfony/Component/HttpClient/HttplugClient.php.

Next, let me explain why to edit src/Symfony/Component/HttpClient/Response/HttplugPromise.php.
After the previous fix, when a Guzzle promise returned by the then callable, things will work.
However, If I return a HttplugPromiseInterface, it doesn't work, because Guzzle promise wait the return value (result)
only if it's a Guzzle promise.

https://github.com/guzzle/promises/blob/bbf3b200bc83c1e9298580a9f99b9be248543467/src/Promise.php#L63

To fix this, I referenced the wait code of Guzzle promise and edited our HttplugPromise.

How this fix make sense

So, why to return a promise from the then callable?
This let us change the promise chain according to current promise's result (fulfilled/rejected).

For example, we can retry an HTTP request if it failed.
Please take a look at my test code.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thanks. I suppose this should sorry arbitrary levels of needed promises?

@nicolas-grekas nicolas-grekas added this to the 4.4 milestone Jul 4, 2020
@nicolas-grekas nicolas-grekas changed the title [HttpClient] Fix promise behavior in HttplugClient (issue #37488) [HttpClient] Fix promise behavior in HttplugClient Jul 4, 2020
@nicolas-grekas
Copy link
Member

Thank you @brentybh.

@nicolas-grekas nicolas-grekas merged commit 3270726 into symfony:4.4 Jul 4, 2020
This was referenced Jul 24, 2020
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.

3 participants