Skip to content

fix(triple): send buffered streaming writes without waiting for a flush - #3896

Open
AsperforMias wants to merge 1 commit into
apache:developfrom
AsperforMias:fix/triple-bidi-write-flush
Open

AsperforMias wants to merge 1 commit into
apache:developfrom
AsperforMias:fix/triple-bidi-write-flush

Conversation

@AsperforMias

@AsperforMias AsperforMias commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3893
Part of #3898

The streaming write buffer (32 KiB, on by default since #3749) held small messages until 32 KiB accumulated, the request side closed, or a receive started waiting. As a result:

Batching only while an earlier write is still in flight does not help with io.Pipe: each pipe write returns only after the transport has read it, so a single sender never finds a write in flight, and the buffer would batch nothing. This PR therefore makes the request body itself a bounded buffered pipe when write buffering is on.

Changes

  • bufferedPipe replaces io.Pipe plus streamBufferWriter. Send copies the message into a 32 KiB buffer and returns. The HTTP transport reads the request body in its own goroutine and takes everything buffered on each read: a message sent while the transport is idle goes out at once, and messages sent while it is still writing leave together in the next read. A full buffer blocks Send, so flow control still holds senders back. A message at least as large as the buffer is read straight from the caller, as with io.Pipe. Writes are serialized, and CloseRequest waits for a write in progress, so a message is never cut short.
  • The first Send starts the HTTP request, as it does without buffering.
  • The waiter tracking and pre-wait flushes in both protocol clients are gone; CloseRequest is CloseWrite again.
  • CloseRequest after the context has ended fails the request body instead of closing it cleanly and returns the context error, so the provider sees the stream reset rather than a request that ended normally just before the cancellation. With the request now live from the first Send, the E2E run showed exactly that clean end; the unbuffered io.Pipe path had the same race.
  • With buffering off, nothing changes.

The buffer was enabled by default after v3.3.2-20260910, so the delays affect develop only.

Throughput (tools/benchmark/stream_ab, h2c, local, messages/s, mean of 3 runs of 8 s)

payload / msgs per stream / concurrency develop, buffering off develop, buffering on this PR
128 B / 128 / 50 84,115 189,368 180,568
128 B / 128 / 1 70,187 130,067 130,617
128 B / 1024 / 8 87,904 180,991 174,098
1 KiB / 128 / 50 36,092 56,656 55,361

With 128 messages of 128 B per stream, develop holds all of a stream's messages until CloseRequest; this PR sends them as they are written and keeps the batching gain.

Verification

  • TestClientStreamSendReachesPeerBeforeClose, TestBidiSendReachesPeerWithoutReceive, TestWriteBufferingSendsWithoutWaitingForClose: a small Send reaches the server with nothing closing or waiting. TestBidiSendReachesPeerWhileReceiving keeps the [Bug]: A Triple streaming Send waits in the write buffer until the stream closes or receives #3893 case. All fail when the pipe hands data to the reader only after close.
  • TestBidiReceiveNotHeldUpBySend: a receive returns while a 16 MiB Send is blocked by flow control.
  • TestDuplexHTTPCallCloseWriteAfterContextEnds: with and without buffering, after cancellation or deadline expiry, closing the request side returns the context error and the transport sees the request body fail instead of end.
  • bufferedPipe unit tests: batching, blocking while full, large writes read directly and in order, no interleaving between concurrent writes, close semantics. Uploading 16 messages of 1 MiB over HTTP/2 runs at the same rate as with io.Pipe (about 3 GB/s locally). TestWriteBufferingDeliversEveryByte: 1000 messages of 100 B arrive in about a dozen server reads.
  • End to end with a Triple provider and consumer, messages sent 1 s apart: on develop the provider receives the three client stream messages together at CloseAndRecv and the bidi messages at the first Recv; with this PR each arrives within 1 ms of its Send.

Independent of #3897; both touch duplex_http_call.go in different places.

Checklist

  • I confirm the target branch is develop
  • I have run make fmt to format my code
  • I have run make test to run local tests
  • I have added tests that prove my fix is effective or that my feature works

@codecov-commenter

codecov-commenter commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.16129% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.08%. Comparing base (60d1c2a) to head (3874e2a).
⚠️ Report is 1073 commits behind head on develop.

Files with missing lines Patch % Lines
protocol/triple/triple_protocol/buffered_pipe.go 94.28% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           develop    #3896       +/-   ##
============================================
+ Coverage    46.76%   61.08%   +14.31%     
============================================
  Files          295      515      +220     
  Lines        17172    42418    +25246     
============================================
+ Hits          8031    25911    +17880     
- Misses        8287    14699     +6412     
- Partials       854     1808      +954     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AsperforMias
AsperforMias force-pushed the fix/triple-bidi-write-flush branch from 8e33bd9 to 32d4f34 Compare November 29, 2026 09:14
@AsperforMias AsperforMias changed the title fix(triple): flush streaming writes while a receive is waiting fix(triple): send buffered streaming writes without waiting for a flush Nov 29, 2026
@AsperforMias
AsperforMias force-pushed the fix/triple-bidi-write-flush branch from 32d4f34 to 8b426f4 Compare November 29, 2026 09:47
The streaming write buffer held small messages until 32 KiB accumulated,
the request side closed, or a receive started waiting. A client stream
delivered nothing before CloseAndRecv, a bidi stream that only sent never
opened, and a Send made while another goroutine was blocked in Recv never
reached the server.

With write buffering on, the request body is now a bounded buffered pipe
instead of io.Pipe plus a separate buffer. Send copies the message into
the 32 KiB buffer and returns; the HTTP transport takes everything
buffered on each read. A message sent while the transport is idle goes
out at once, and messages sent while it is still writing leave together,
so bursts are still batched. A full buffer blocks Send, which keeps flow
control in effect. A message at least as large as the buffer is read
straight from the caller, as with io.Pipe.

CloseWrite after the context has ended now fails the request body and
returns the context error, so the server sees the stream reset instead
of a request that ended normally.
@AsperforMias
AsperforMias force-pushed the fix/triple-bidi-write-flush branch from 8b426f4 to 3874e2a Compare November 29, 2026 10:00
@sonarqubecloud

Copy link
Copy Markdown

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.

2 participants