Skip to content

[HttpClient] added support for pausing responses with a new pause_handler callable exposed as an info item #37136

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
Jun 9, 2020

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

This code sample will delay sending the request by 2 seconds:

$response = $client->request('GET', 'https://symfony.com/');
$response->getInfo('pause_handler')(2);

Unlike "competing" HTTP clients written in PHP, this one works while streaming a request/response. This means this PR allows implementing delays before retries but it also enables throttling the streams while still maintaining async/multiplexing.

Returning the handler as an info item saves adding a new method and thus plays well with decorators, without requiring a new dedicated interface.

While this can be used directly, the target use case is within an async-decorator, by using the AsyncContext::pause() method.

As a bonus, this PR improves NativeHttpClient by making it able to count the maximum number of open connections per-host.

Comment on lines +103 to +111
$delay = Loop::defer(static function () use ($duration, $id, &$delay) {
if (0 < $duration -= microtime(true)) {
$delay = Loop::delay(ceil(1000 * $duration), static function () use ($id) { Loop::enable($id); });
} else {
$delay = null;
Loop::enable($id);
}
});
Copy link
Member Author

Choose a reason for hiding this comment

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

PR ready.
Here is the workaround for amphp/amp#319
/cc @kelunik FYI

Copy link
Contributor

Choose a reason for hiding this comment

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

@nicolas-grekas The Loop::defer should already be enough, no need to check the actually passed time.

Copy link
Member Author

Choose a reason for hiding this comment

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

The time computation accounts for the duration of the tick itself. Since this delay is added at the end of the tick, isn't it?

@fabpot
Copy link
Member

fabpot commented Jun 9, 2020

Thank you @nicolas-grekas.

javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this pull request Apr 20, 2021
…oratorTrait (nicolas-grekas)

This PR was merged into the 5.2 branch.

Discussion
----------

[HttpClient] add doc about extending and AsyncDecoratorTrait

About symfony/symfony#36779 and symfony/symfony#37136

Commits
-------

f41ef12 [HttpClient] add doc about extending and AsyncDecoratorTrait
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.

4 participants