-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
6ca9e3b
to
418a300
Compare
$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); | ||
} | ||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
…ndler` callable exposed as an info item
Thank you @nicolas-grekas. |
…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
This code sample will delay sending the request by 2 seconds:
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.