Skip to content

Optimize how SSE data is written - #826

Open
pR0Ps wants to merge 1 commit into
BlueMap-Minecraft:masterfrom
pR0Ps:refactor/sseconnection-write
Open

Optimize how SSE data is written#826
pR0Ps wants to merge 1 commit into
BlueMap-Minecraft:masterfrom
pR0Ps:refactor/sseconnection-write

Conversation

@pR0Ps

@pR0Ps pR0Ps commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This is a followup of #819 (comment)

The SseConnection now writes queued events directly to the response's output stream from the connection-handling thread itself. Previously each SSE connection has its own virtual thread that would create and push the events into a pipe for the actual connection-handling thread to read from.

Per SSE connection, this new method saves the creation of a virtual thread, a PipedOutputStream/PipedInputStream pair and their internal buffer, and the overhead of having to push all the SSE data across a thread boundary.

Additionally, because the SseConnection now handles writing the data to the output stream itself and can handle flushing it as needed, the HttpResponseOutputStream no longer needs to flush the output stream after every buffer read just in case the data it was sending was part of an SSE stream.

To support this change, the HttpResponseStreamWriter interface was added to allow HttpResponse bodies to be streamed incrementally and dynamically chunked using the new ChunkedOutputStream rather than only being able to be read fixed-size chunks from an InputStream.

Note that because the pipe buffer is now gone, event buffering in the SseConnection occurs entirely in the queue. To compensate for this, the QUEUE_CAPACITY was increased from 16 to 64.

The `SseConnection` now writes queued events directly to the response's
output stream from the connection-handling thread itself. Previously
each SSE connection has its own virtual thread that would create and
push the events into a pipe for the actual connection-handling thread to
read from.

Per SSE connection, this new method saves the creation of a virtual
thread, a `PipedOutputStream`/`PipedInputStream` pair and their internal
buffer, and the overhead of having to push all the SSE data across a
thread boundary.

Additionally, because the `SseConnection` now handles writing the data
to the output stream itself and can handle flushing it as needed, the
`HttpResponseOutputStream` no longer needs to flush the output stream
after every buffer read just in case the data it was sending was part of
an SSE stream.

To support this change, the `HttpResponseStreamWriter` interface was
added to allow `HttpResponse` bodies to be streamed incrementally and
dynamically chunked using the new `ChunkedOutputStream` rather than only
being able to be read fixed-size chunks from an `InputStream`.

Note that because the pipe buffer is now gone, event buffering in the
`SseConnection` occurs entirely in the queue. To compensate for this,
the `QUEUE_CAPACITY` was increased from 16 to 64.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant