Skip to content

Coalesce response head with body to avoid extra flush#311

Closed
rnro wants to merge 1 commit into
apple:mainfrom
rnro:HTTPDrippingDownloadHandler_dont_flush_head
Closed

Coalesce response head with body to avoid extra flush#311
rnro wants to merge 1 commit into
apple:mainfrom
rnro:HTTPDrippingDownloadHandler_dont_flush_head

Conversation

@rnro

@rnro rnro commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Motivation

  • HTTPDrippingDownloadHandler used writeAndFlush for the response head, causing it to be flushed separately from the body. This results in two flushes per response instead of one, which can produce extra packets on the wire for small downloads.

Modifications

  • Change onResponseDelayCompleted to use context.write instead of context.writeAndFlush for the response head, so it is coalesced with the body in the same flush.
  • Add a needsFlush flag to ensure the head is still flushed promptly if writeChunk doesn't flush (e.g. zero-size chunks or channel not writable).
  • Add testSmallDownloadDoesNotFlushHeadSeparately verifying that head, body, and end are all written before the first flush.

Result

  • For small single-chunk downloads, the response head and body are sent in a single flush instead of two.

Motivation

* `HTTPDrippingDownloadHandler` used `writeAndFlush` for the response
  head, causing it to be flushed separately from the body. This results
  in two flushes per response instead of one, which can produce extra
  packets on the wire for small downloads.

Modifications

* Change `onResponseDelayCompleted` to use `context.write` instead of
  `context.writeAndFlush` for the response head, so it is coalesced
  with the body in the same flush.
* Add a `needsFlush` flag to ensure the head is still flushed promptly
  if `writeChunk` doesn't flush (e.g. zero-size chunks or channel not
  writable).
* Add `testSmallDownloadDoesNotFlushHeadSeparately` verifying that head,
  body, and end are all written before the first flush.

Result

* For small single-chunk downloads, the response head and body are sent
  in a single flush instead of two.
@rnro rnro force-pushed the HTTPDrippingDownloadHandler_dont_flush_head branch from 5737c6c to 175f10d Compare April 24, 2026 13:54
@rnro rnro closed this Apr 30, 2026
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