WIP: [Feature] Stream responses without putting whole to memory#180
Closed
dmitryuk wants to merge 2 commits into
Closed
WIP: [Feature] Stream responses without putting whole to memory#180dmitryuk wants to merge 2 commits into
dmitryuk wants to merge 2 commits into
Conversation
dmitryuk
commented
Nov 22, 2025
a053d82 to
bc37769
Compare
Contributor
Author
|
@Baldinof can you look and merge? |
Baldinof
reviewed
Dec 2, 2025
|
|
||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class StreamedGeneratorResponse extends Response |
Owner
There was a problem hiding this comment.
2 things:
- ideally it would extend Symfony
StreamedResponseso in case the app runs without RR, it would still work and stream the content. - maybe the constructor could accept
Generator<string>|callable() => Generator<string>, so it could be used like this
return new StreamedGeneratorResponse(function () {
yield "hello";
yield "world";
});Instead of
return new StreamedGeneratorResponse((function () {
yield "hello";
yield "world";
})());
Contributor
Author
There was a problem hiding this comment.
- I really tried to convert ob_start/on_flush to \Generator, but it looks impossible
Owner
There was a problem hiding this comment.
I mean do something like this:
class StreamedGeneratorResponse extends StreamedResponse
public function __construct(private readonly \Generator $generator, int $status = 200, array $headers = [])
{
parent::__construct(function () use ($generator) {
foreach ($generator as $chunk) {
echo $chunk;
}
}, $status, $headers);
}
public function getGenerator(): \Generator
{
return $this->generator;
}So when not using RR, the response will still be streamed the normal way
Contributor
Author
There was a problem hiding this comment.
Thanks for review, I finally reworked the PR as you suggest, please take a look
UPD: I need a time to look in failing tests
bc37769 to
5bfe1e7
Compare
Baldinof
reviewed
Dec 5, 2025
Comment on lines
+47
to
+50
| $callback = $response->getCallback(); | ||
| if (!$callback || (!($content = $callback()) instanceof \Generator)) { | ||
| throw new \RuntimeException('StreamedGeneratorResponse callback must return a Generator'); | ||
| } |
Owner
There was a problem hiding this comment.
Suggested change
| $callback = $response->getCallback(); | |
| if (!$callback || (!($content = $callback()) instanceof \Generator)) { | |
| throw new \RuntimeException('StreamedGeneratorResponse callback must return a Generator'); | |
| } | |
| $content = $response->getGenerator(); |
Contributor
Author
|
PR frozen. I looking for a way toimplement a feature out-of-box here roadrunner-php/issues#43 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #179 #130
Impossible to adapt Symfony's StreamedResponse class, so I created the custom one