Skip to content

feat: add DialOptions.WriteBufferSize - #608

Open
tmojzes wants to merge 2 commits into
coder:masterfrom
tmojzes:dial-write-buffer-size
Open

tmojzes wants to merge 2 commits into
coder:masterfrom
tmojzes:dial-write-buffer-size

Conversation

@tmojzes

@tmojzes tmojzes commented Nov 27, 2026

Copy link
Copy Markdown

Hi! This PR adds a WriteBufferSize option to DialOptions so 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. Dial currently 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.
  • Any other size gets its own writer. It isn't returned to the pool, so the pool only ever holds default-size writers.
  • msgWriter.close now clears c.bw and c.writeBuf, so a closed connection doesn't keep a large custom buffer alive.
  • On WASM, DialOptions gets a WriteBufferSize field that does nothing, so code that sets it still compiles for GOOS=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):

default   17 writes/op  213 MB/s
128KiB     1 write/op   527 MB/s

Tradeoffs and what this PR doesn't do

  • Custom-size writers aren't pooled, so each Dial with 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.
  • There's no upper limit on the size. It follows the other size options in leaving a sensible value to the caller.
  • Only client writes are affected. Accept and the server side are unchanged.

Tests

  • TestDialWriteBufferSize counts 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.
  • TestBufioWriterPool checks that custom-size writers never end up in the shared pool.

Locally, go test -race . reports a goroutine leak in TestMain (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.

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
tmojzes force-pushed the dial-write-buffer-size branch from 151c750 to 79a1992 Compare November 27, 2026 14:48

This branch has not been deployed

No deployments
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