Skip to content

Add bounded chunked responses to httpcore - #220

Open
Bronek wants to merge 2 commits into
mainfrom
bronek/sse_httpcore
Open

Add bounded chunked responses to httpcore#220
Bronek wants to merge 2 commits into
mainfrom
bronek/sse_httpcore

Conversation

@Bronek

@Bronek Bronek commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

ServerConnection frames response bodies with Content-Length. Server-sent events need a response that stays open as more data arrives. This PR adds ChunkedResponse to support that lifetime, with HTTP chunked framing and bounded buffers.

ServerConnection::into_stream transfers the queued response head and buffers into ChunkedResponse. The head is sent before any appended chunks. Buffered pipelined requests are abandoned, and subsequent input is discarded. The connection cannot be reused, and closes without a terminal chunk.

The transition reserves a 64 KiB send buffer and trims the inherited read buffer to 4 KiB of discard scratch. Accepted pushes do not reallocate. The unsent-byte limit includes the response head and chunk framing; a push exceeding it is refused, requiring the caller to close the connection.

The machine also tracks time without write progress while output is pending. The caller supplies both the clock and deadline, and decides when to close a stalled connection. This measures writes accepted by the socket, not reads by the peer.

Direct machine tests cover framing, partial writes, buffer limits and allocation stability, stall timing, and the transition from request parsing to discarding input.

The stacked PR adds the first consumer: block subscriptions at /eth/v1/events. Topic selection, event publication, and connection expiry policy belong there.

@Bronek
Bronek force-pushed the bronek/sse_httpcore branch from 33f4067 to 9c4b6b2 Compare September 7, 2026 14:53
@Bronek
Bronek marked this pull request as ready for review September 7, 2026 15:49
Move the unsent response head and read buffer from ServerConnection into
ChunkedResponse. Append one HTTP chunk per nonempty push and reuse the
read buffer as discard scratch.

Refuse pushes whose framed output would take pending bytes past 64 KiB.
Track time without committed writes using caller-supplied instants;
clear the wait when output drains. The caller owns connection closure.
No terminal chunk is emitted.

Add frame_chunked_head for a 200 response with caller-selected content
headers, chunked transfer encoding, and Connection: close.

Tests cover head ordering, chunk framing, push rejection, partial drains,
capacity reuse for repeated bursts, waiting-clock transitions, discarded
input, and moving both buffers without copying.

Production wiring, SSE channels, /eth/v1/events, and the ADR-0004
amendment remain for the next change.

Assisted-by: Claude Code:claude-fable-5-1
Assisted-by: Codex:gpt-6-astra
Reserve the 64 KiB output allowance when constructing ChunkedResponse and
trim excess capacity inherited from the response buffer. Accepted pushes
then reuse that allocation through partial and full drains.

Reduce the inherited read buffer to 4 KiB of discard scratch, releasing
capacity that request handling may have grown toward 16 MiB. Assert that
the initial response head is nonempty and fits the output allowance.

Resizing at the transition may reallocate either buffer; the swap no
longer promises no copying. The output limit includes the response head
and chunk framing, while discard scratch is separate.

Tests cover output capacity across partial drains, allocation reuse after
repeated bursts and a maximum-sized push, scratch reduction after a large
request body, and rejection of an oversized head.

Assisted-by: Claude Code:claude-fable-5-1
Assisted-by: Codex:gpt-6-astra
@Bronek
Bronek force-pushed the bronek/sse_httpcore branch from 9c4b6b2 to 05035f7 Compare September 8, 2026 08:49
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