Skip to content

Commit 6eb1142

Browse files
committed
Use StreamWorkerInterface::withStreamMode() for stream output if worker supports it
1 parent 30d89df commit 6eb1142

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@
4242
"psr/http-factory": "^1.0.1",
4343
"psr/http-message": "^1.0.1 || ^2.0",
4444
"spiral/roadrunner": "^2023.1",
45-
"spiral/roadrunner-worker": "^3.0"
45+
"spiral/roadrunner-worker": "dev-ping-pong-stream as 3.1.0"
4646
},
4747
"require-dev": {
48+
"buggregator/trap": "^1.0",
4849
"jetbrains/phpstorm-attributes": "^1.0",
4950
"nyholm/psr7": "^1.3",
5051
"phpunit/phpunit": "^10.0",
5152
"symfony/process": "^6.2",
53+
"symfony/var-dumper": "^6.3",
5254
"vimeo/psalm": "^5.9"
5355
},
5456
"autoload": {

src/HttpWorker.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spiral\RoadRunner\Http\Exception\StreamStoppedException;
99
use Spiral\RoadRunner\Message\Command\StreamStop;
1010
use Spiral\RoadRunner\Payload;
11+
use Spiral\RoadRunner\StreamWorkerInterface;
1112
use Spiral\RoadRunner\WorkerInterface;
1213

1314
/**
@@ -86,18 +87,22 @@ private function respondStream(int $status, Generator $body, array $headers = []
8687
'headers' => $headers ?: (object)[],
8788
], \JSON_THROW_ON_ERROR);
8889

90+
$worker = $this->worker instanceof StreamWorkerInterface
91+
? $this->worker->withStreamMode()
92+
: $this->worker;
93+
8994
do {
9095
if (!$body->valid()) {
9196
$content = (string)$body->getReturn();
92-
$this->worker->respond(new Payload($content, $head, true));
97+
$worker->respond(new Payload($content, $head, true));
9398
break;
9499
}
95100
$content = (string)$body->current();
96-
if ($this->worker->getPayload(StreamStop::class) !== null) {
101+
if ($worker->getPayload(StreamStop::class) !== null) {
97102
$body->throw(new StreamStoppedException());
98103
return;
99104
}
100-
$this->worker->respond(new Payload($content, $head, false));
105+
$worker->respond(new Payload($content, $head, false));
101106
$body->next();
102107
$head = null;
103108
} while (true);

0 commit comments

Comments
 (0)