Skip to content

perf(response): Flush requests early to clients - #57991

Open
nickvergessen wants to merge 3 commits into
masterfrom
perf/noid/flush-response-early
Open

nickvergessen wants to merge 3 commits into
masterfrom
perf/noid/flush-response-early

Conversation

@nickvergessen

Copy link
Copy Markdown
Member

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.

Checklist

@nickvergessen nickvergessen added this to the Nextcloud 34 milestone Feb 2, 2026
@nickvergessen nickvergessen self-assigned this Feb 2, 2026
@nickvergessen
nickvergessen requested a review from a team as a code owner February 2, 2026 10:45
@nickvergessen
nickvergessen requested review from ArtificialOwl, icewind1991, leftybournes and salmart-dev and removed request for a team February 2, 2026 10:45
@nickvergessen
nickvergessen marked this pull request as draft February 2, 2026 10:45

@Altahrim Altahrim left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comments, but I like the idea :)

There is also fastcgi_finish_request for FPM, it may worth a try as it also kills the connection

Comment thread lib/private/AppFramework/App.php Resolved
@nickvergessen

Copy link
Copy Markdown
Member Author

From PHP comments a small reminder:

There are some pitfalls you should be aware of when using this function.
...
Another important thing is session handling. Sessions are locked as long as they're active (see the documentation for session_write_close()). This means subsequent requests will block until the session is closed.

You should therefore call session_write_close() as soon as possible (even before fastcgi_finish_request()) to allow subsequent requests and a good user experience.

We should ensure session was closed before.

@Altahrim

Copy link
Copy Markdown
Collaborator

Good remark about session.

I think one of the best scenario would be:

  • open session
  • generate HTTP headers
  • flush
  • generate <head>
  • close_session
  • flush // At this point, browser can already load all related resources (CSS, JS…)
  • generate content (all info about users have already been loaded)
  • flush / end-request
  • do post query stuff not needed by client

I didn't check what is possible. It may be possible to reduce session access even more

@juliusknorr

Copy link
Copy Markdown
Member

Regarding reducing session access, #32162 is probably interesting. Back then we introduced a config flag to close the session early and reopen if needed, but it has impact on the session expiry which can then not be as reliably as before anymore.

@susnux susnux modified the milestones: Nextcloud 34.0.1, Nextcloud 35 Jun 9, 2026
@nickvergessen
nickvergessen force-pushed the perf/noid/flush-response-early branch from d4ce68e to 677634f Compare July 31, 2026 13:56
@nickvergessen
nickvergessen requested a review from Altahrim July 31, 2026 13:56
@nickvergessen
nickvergessen marked this pull request as ready for review July 31, 2026 13:58
@nickvergessen

Copy link
Copy Markdown
Member Author

Rebased and further discussed that with my coding peer (Claude).
It's just the base work for now each part that could benefit from it needs to do that manually:

  • Use async when sending requests (webhooks, guzzle, federation needs some rework here, …)
  • Don't flush notifications early (so notifications app or something else can do it in the code after it)
  • If that is all working, the app can opt-in into the "flush early"

@AndyScherzinger AndyScherzinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

This was referenced Aug 12, 2026
@nextcloud-bot nextcloud-bot mentioned this pull request Aug 20, 2026
@skjnldsv skjnldsv mentioned this pull request Aug 25, 2026
@skjnldsv
skjnldsv force-pushed the perf/noid/flush-response-early branch from 677634f to 770be47 Compare December 1, 2026 08:45
@skjnldsv
skjnldsv enabled auto-merge December 1, 2026 08:45
*
* @return bool true if the connection was closed, false if the response was
* only flushed because the SAPI does not support closing it
* @since 35.0.0

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 35.0.0
* @since 36.0.0

by now 🙈

nickvergessen and others added 3 commits December 6, 2026 01:52
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>
Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com>
Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com>
…Access

The DIContainer no longer implements ArrayAccess, so the tests failed to
register their mocks. The new public API also lands in 36, not 35.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
@susnux
susnux force-pushed the perf/noid/flush-response-early branch from 770be47 to 6b82d83 Compare December 6, 2026 00:53

@susnux susnux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Rebased and adjusted the version comment :)
If you agree then lets :shipit:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants