Skip to content

Commit 811a53c

Browse files
committed
perf(response): Flush requests early to clients
If not, the response will wait for async actions, e.g. HTTP requests from IClientService, to be finished before returning. This is not desired in many cases with e.g. with notifications. Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 47ed8d7 commit 811a53c

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

lib/private/AppFramework/App.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,13 @@ public static function main(
198198
$io->setOutput($output);
199199
}
200200
}
201+
202+
if ($response->getFlushEarly()) {
203+
fastcgi_finish_request();
204+
while (ob_get_level() > 0) {
205+
ob_end_flush();
206+
}
207+
flush();
208+
}
201209
}
202210
}

lib/public/AppFramework/Http/Response.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Response {
6464

6565
/** @var bool */
6666
private $throttled = false;
67+
private bool $flushEarly = true;
6768
/** @var array */
6869
private $throttleMetadata = [];
6970

@@ -397,4 +398,25 @@ public function getThrottleMetadata() {
397398
public function isThrottled() {
398399
return $this->throttled;
399400
}
401+
402+
/**
403+
* Request the response should be flushed to the connected client immediately
404+
*
405+
* @since 34.0.0
406+
*/
407+
public function setFlushEarly(bool $flushEarly): void {
408+
$this->flushEarly = $flushEarly;
409+
}
410+
411+
/**
412+
* Whether the response should be flushed to the connected client immediately
413+
*
414+
* If not, the response will wait for async actions, e.g. HTTP requests from
415+
* IClientService, to be finished before returning.
416+
*
417+
* @since 34.0.0
418+
*/
419+
public function getFlushEarly(): bool {
420+
return $this->flushEarly;
421+
}
400422
}

0 commit comments

Comments
 (0)