Repository navigation
Conversation
Client frames are masked by copying the payload through the connection's bufio.Writer, which Dial always creates with the bufio default of 4096 bytes and flushes every time it fills. A 64 KiB message therefore costs 17 writes (16 over TLS records), while server frames, which are not masked, go out in one. WriteBufferSize lets the dialer size that buffer. Zero or negative keeps the pooled 4096-byte writer, so existing users are unaffected; other sizes get a dedicated writer that is not returned to the pool and is released when the connection closes. On WASM the field has no effect, since the browser frames writes, and exists so code compiles on both platforms. BenchmarkDialWriteBufferSize (64 KiB echo, loopback): default 17 writes/op 213 MB/s 128KiB 1 write/op 527 MB/s
httptest.Server.Close does not wait for hijacked connections, so the goroutine leak check in the bench job could catch the handler exiting.
tmojzes
force-pushed
the
dial-write-buffer-size
branch
from
November 27, 2026 14:48
151c750 to
79a1992
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! This PR adds a
WriteBufferSizeoption toDialOptionsso clients can choose the size of the buffer used to mask and write frames.Why
Client frames have to be masked, so their payload is copied through the connection's
bufio.Writer.Dialcurrently always creates that writer with the bufio default of 4096 bytes and flushes it every time it fills. A 64 KiB message therefore goes out in 17 writes, while server frames, which aren't masked, go out in one. For bulk transfers from the client, this noticeably limits throughput.What changes
DialOptions.WriteBufferSize: zero or negative keeps the current behavior, a pooled 4096-byte writer, so existing users shouldn't notice any difference.msgWriter.closenow clearsc.bwandc.writeBuf, so a closed connection doesn't keep a large custom buffer alive.DialOptionsgets aWriteBufferSizefield that does nothing, so code that sets it still compiles forGOOS=js. I wasn't sure about this one, since other native-only options aren't mirrored there. I'm happy to remove it if you'd rather keep the WASM options minimal.Benchmark
BenchmarkDialWriteBufferSize(64 KiB echo over loopback, darwin/arm64):Tradeoffs and what this PR doesn't do
Dialwith a non-default size allocates one buffer. A pool per size, or per size class, would avoid that, but it seemed like more code than this needed. Connections that raise the buffer size are usually long-lived ones. I'm happy to change this if you see it differently.Acceptand the server side are unchanged.Tests
TestDialWriteBufferSizecounts writes on the client connection for default, negative, explicit 4096, small, large and tiny (5-byte) buffers. It also checks that messages echo back intact.TestBufioWriterPoolchecks that custom-size writers never end up in the shared pool.Locally,
go test -race .reports a goroutine leak inTestMain(an idle HTTP keep-alive connection), but master does the same on my machine, so I don't think it's related.A note on how this was written
I wrote this change with the help of an AI assistant (Claude Code). I've reviewed and adjusted the result, but I'm still learning this codebase, so please let me know if anything doesn't fit the project's style or approach. I'm glad to rework it.