Skip to content

Commit

Permalink
Update: Ensure that progress reports are sent ASAP
Browse files Browse the repository at this point in the history
Follow-up to 9741e39.

Various layers in the network stack including php-fpm, Apache’s `mod_proxy_fcgi` and Nginx’s `ngx_http_fastcgi_module` will buffer responses.
Since the individual events are short compared to buffer sizes, it may happen that PHP keeps holding onto the output, waiting for the response buffer to fill up, and, in the meanwhile, nginx will time out waiting for the contents.
Let’s prevent that by ending all output buffering we can on PHP side.
(There would be ¿up to two? levels: one for `output_buffering`, one for `zlib.output_compression`.)

Web server buffering is not that critical (unless there is another layer like a proxy) but it is useful to disable it as well so that progress updates are reflected live. We will do that for nginx, where it can be done simply with a response header (unless disabled in nginx configuration).

Since the response will now start arriving immediately, we need to modify the client so that the promise returned by `refreshAll` is only fulfilled once the whole body arrives. Otherwise, the refresh icon would stop spinning just after receiving the headers.
  • Loading branch information
jtojnar committed Apr 13, 2023
1 parent 11a6ae1 commit 9a3d7cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- Re-added “Next” button on smartphones. ([#1406](https://github.com/fossar/selfoss/issues/1406)
- Fix compressed SVG (svgz) support. ([#1418](https://github.com/fossar/selfoss/pulls/1418)
- Fix article links containing HTML-special characters. ([#1407](https://github.com/fossar/selfoss/issues/1407))
- Reduce the chance of “Update all sources” button timing out. ([#1428](https://github.com/fossar/selfoss/pulls/1428))
- Reduce the chance of “Update all sources” button timing out. ([#1428](https://github.com/fossar/selfoss/pulls/1428), [#1430](https://github.com/fossar/selfoss/pulls/1430))
- Fix a log-in loop in client. ([#1429](https://github.com/fossar/selfoss/pulls/1429))
- Fix errors in Firefox’s private browsing mode.

Expand Down
2 changes: 1 addition & 1 deletion assets/js/requests/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function refreshAll() {
'Accept': 'text/event-stream',
},
timeout: 0,
}).promise;
}).promise.then(response => response.text());
}

/**
Expand Down
27 changes: 24 additions & 3 deletions src/controllers/Sources/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,44 @@ public function updateAll(): void {
$reportProgress = $accept === 'text/event-stream';

if ($reportProgress) {
// Individual events are short so we need to prevent various layers in the stack from buffering the response body.
// Otherwise a consuming layer may time out before any output gets to it.

// Ensure PHP compression is disabled since it would enable output buffering.
// https://www.php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression
ini_set('zlib.output_compression', '0');

// End implicit buffering caused by `output_buffering` option.
while (ob_get_level() > 0) {
ob_end_clean();
}

// Ask nginx to not buffer FastCGI response.
// http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering
header('X-Accel-Buffering: no');

// Server-sent events inspired response format.
header('Content-Type: text/event-stream');

$updateVisitor = new class() implements UpdateVisitor {
private int $finishedCount = 0;

private function sendEvent(string $type, string $data = '{}'): void {
echo "event: {$type}\ndata: {$data}\n\n";
flush();
}

public function started(int $count): void {
echo "event: started\ndata: {\"count\": {$count}}\n\n";
$this->sendEvent('started', "{\"count\": {$count}}");
}

public function sourceUpdated(): void {
++$this->finishedCount;
echo "event: sourceUpdated\ndata: {\"finishedCount\": {$this->finishedCount}}\n\n";
$this->sendEvent('sourceUpdated', "{\"finishedCount\": {$this->finishedCount}}");
}

public function finished(): void {
echo "event: finished\ndata: {}\n\n";
$this->sendEvent('finished');
}
};
} else {
Expand Down

0 comments on commit 9a3d7cf

Please sign in to comment.