Skip to content
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

Support for streamed output #10

Merged
merged 8 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": ">=7.4",
"ext-json": "*",
"spiral/roadrunner-worker": "^2.0",
"spiral/roadrunner-worker": "^2.2.0",
"psr/http-factory": "^1.0.1",
"psr/http-message": "^1.0.1"
},
Expand All @@ -30,15 +30,15 @@
"phpstan/phpstan": "~0.12",
"phpunit/phpunit": "~8.0",
"jetbrains/phpstorm-attributes": "^1.0",
"vimeo/psalm": "^4.4",
"vimeo/psalm": "^4.22",
"symfony/var-dumper": "^5.1"
},
"scripts": {
"analyze": "psalm"
},
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
"dev-master": "2.2.x-dev"
}
},
"suggest": {
Expand Down
37 changes: 33 additions & 4 deletions src/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Spiral\RoadRunner\Http;

use Generator;
use Spiral\RoadRunner\Payload;
use Spiral\RoadRunner\WorkerInterface;
use Stringable;

/**
* @psalm-import-type HeadersList from Request
Expand Down Expand Up @@ -83,12 +85,39 @@ public function waitRequest(): ?Request
*/
public function respond(int $status, string $body, array $headers = []): void
{
$headers = (string)\json_encode([
$head = (string)\json_encode([
'status' => $status,
'headers' => $headers ?: (object)[],
], \JSON_THROW_ON_ERROR);

$this->worker->respond(new Payload($body, $headers));
$this->worker->respond(new Payload($body, $head));
}

/**
* Respond data using Streamed Output
*
* @param Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null> $body Body generator.
* Each yielded value will be sent as a separated stream chunk.
* Returned value will be sent as a last stream package.
*/
public function respondStream(int $status, Generator $body, array $headers = []): void
{
$head = (string)\json_encode([
'status' => $status,
'headers' => $headers ?: (object)[],
], \JSON_THROW_ON_ERROR);

do {
if (!$body->valid()) {
$content = (string)$body->getReturn();
$this->worker->respond(new Payload($content, $head, true));
break;
}
$content = (string)$body->current();
$this->worker->respond(new Payload($content, $head, false));
$body->next();
$head = null;
} while (true);
}

/**
Expand Down Expand Up @@ -131,7 +160,7 @@ private function hydrateRequest(Request $request, array $context): void
}

/**
* @param array<mixed, mixed> $headers
* Remove all non-string and empty-string keys
*
* @return array<string, mixed>
*/
Expand All @@ -144,7 +173,7 @@ private function filterHeaders(array $headers): array
unset($headers[$key]);
}
}

/** @var array<string, mixed> $headers */
return $headers;
}
}
36 changes: 34 additions & 2 deletions src/PSR7Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Spiral\RoadRunner\Http;

use Generator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Spiral\RoadRunner\WorkerInterface;
use Stringable;

/**
* Manages PSR-7 request and response.
Expand All @@ -27,6 +30,8 @@
*/
class PSR7Worker implements PSR7WorkerInterface
{
public int $chunk_size = 8 * 1024;

/**
* @var HttpWorker
*/
Expand Down Expand Up @@ -108,13 +113,40 @@ public function waitRequest(): ?ServerRequestInterface
*/
public function respond(ResponseInterface $response): void
{
$this->httpWorker->respond(
$this->httpWorker->respondStream(
$response->getStatusCode(),
(string)$response->getBody(),
$this->streamToGenerator($response->getBody()),
$response->getHeaders()
);
}

/**
* @return Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null> Compatible
* with {@see \Spiral\RoadRunner\Http\HttpWorker::respondStream()}.
*/
private function streamToGenerator(StreamInterface $stream): Generator
{
$stream->rewind();
$size = $stream->getSize();
if ($size !== null && $size < $this->chunk_size) {
return (string)$stream;
}
$sum = 0;
while (!$stream->eof()) {
if ($size === null) {
$chunk = $stream->read($this->chunk_size);
} else {
$left = $size - $sum;
$chunk = $stream->read(\min($this->chunk_size, $left));
if ($left <= $this->chunk_size && \strlen($chunk) === $left) {
return $chunk;
}
}
$sum += \strlen($chunk);
yield $chunk;
}
}

/**
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
Expand Down
1 change: 0 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ final class Request
/**
* @var string
*/

public string $remoteAddr = '127.0.0.1';

/**
Expand Down