You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
net: add mechanism to wait for readability on a TCPConn #15735
The net/http package needs a way to wait for readability on a TCPConn without actually reading from it. (See #15224)
http://golang.org/cl/22031 added such a mechanism, making Read(0 bytes) do a wait for readability, followed by returning (0, nil). But maybe that is strange. Windows already works like that, though. (See new tests in that CL)
Reconsider this for Go 1.8.
Maybe we could add a new method to TCPConn instead, like WaitRead.
Reacted by Lorenzo Masini, Edmond Lin, Artem Tikhomirov, Stanislav Ovsiannikov, Crossle Song, Alexander Emelin, Jianhui Zhao, Roman Grytskiv, Alexander Tipugin, Mahendra Kanani and 38 more
read(2) with a count of zero may be used to detect errors. Linux man page confirms, as does POSIX's read(3p) here. Mentioning it in case it influences this subverting of a Read(0 bytes) not calling syscall.Read.
Actually, the more I think about this, I don't even want my idle HTTP/RPC goroutines to stick around blocked in a read call. In addition to the array memory backed by the slice given to Read, the goroutine itself is ~4KB of wasted memory.
What I'd really like is a way to register a func() to run when my *net.TCPConn is readable (when a Read call wouldn't block). By analogy, I want the time.AfterFunc efficiency of running a func in a goroutine later, rather than running a goroutine just to block in a time.Sleep.
My new proposal is more like:
package net
// OnReadable runs f in a new goroutine when c is readable;// that is, when a call to c.Read will not block.func (c*TCPConn) OnReadable(ffunc()) {
// ...
}
Yes, maybe this is getting dangerously into event-based programming land.
Or maybe just the name ("OnWhatever") is offensive. Maybe there's something better.
Reacted by Alexandre Fiori, Sergey Kamardin, Alexander Emelin, Ismael Dönmez, Trevor N. Suarez, valery-barysok, Eduard Bondarenko, Serge, Alex Bubnov, Diego Medina and 121 more
Reacted by Axel Wagner, Tom Thorogood, Richard Dzenis and Ivan Gonzalez
Reacted by Oleg Kovalov, Sandor Szücs, Gabriel, Eloff, Shinya Sugamoto, Ewetumo Alexander and Kamil Klimkiewicz
I'm worried that the existence of such a method will encourage people to start writing their code as callbacks rather than as straightforward goroutines.
Reacted by Yonka Fang, QuantumGhost, Zhennan LI, Eric Lagergren, Lin Lin and Aliaksandr Valialkin
If we do OnReadable(f func()), won't we need to fork half of standard library for async style? Compress, io, tls, etc readers all assume blocking style and require a blocked goroutine.
I don't see any way to push something asynchronously into e.g. gzip.Reader. Does this mean that I have to choose between no blocked goroutine + my own gzip impl and blocked goroutine + std lib?
why can't we have a chan-like interface to use in select? like proposed in #15735 (comment). This to have a single loop for send and receive on a connection
It's difficult to implement efficiently in the runtime. At least, I don't see how to do it.
Note that you can already do something similar by having a goroutine read data from the network connection and send it on a channel. Yes, that requires an extra goroutine.
Go's entire standard library was designed around blocking I/O and not for high(est) performance, so why should it be any different in this issue's case?
If you want to select on read events of thousands of fds at highest possible performance/lowest possible overhead then you can't use idiomatic Go (goroutines, channels, select) anyway because it's not designed for that. You can still write your own unidiomatic, non-portable, low-level code though.
So I think the requirements and expectations should be clear: provide a method that blocks until data is available.
That's the only approach that is consistent with Go's design.
Users can wrap a goroutine around that, send "ready" events to their own (buffered) channels, etc.
The options I know of are (from fastest to slowest):
Use DPDK/XDP and implement a userspace TCP stack.
Use a vendor-specific stack that uses kernel bypass to emulate 3 or 4.
Use io_uring with preregistered buffers.
Use the sockets API.
For maximum performance while still not relying on elevated privileges, I think it would be best to use a proactor model with io_uring. If CGo were faster, binding to an optimized C or Rust library for this would be the obvious choice, at least if there were not lifetime constraints that forced the Go bindings to be highly unsafe.
I've created a new event-driven, high-performance, low-memory Go network library: fib. It's faster than almost every Go networking library before it, built with Claude, with almost all of its code generated by Claude.
It supports TLS, HTTP 1/2/3, and WebSockets, and its performance surpasses almost all previous Go libraries. In some scenarios, its performance is close to or even better than some C/C++/Rust libraries.
In the benchmarks automatically run on GitHub Actions, fib achieves several times higher throughput and significantly less memory usage compared to standard libraries in some scenarios. It also shows performance improvements compared to libraries known for their performance, such as fasthttp, herz. Compared to C/C++/Rust frameworks, it's very close, and in some scenarios, it even surpasses them.
Of course, different hardware specifications, test parameters, and framework configurations may result in different results. You are welcome to run these benchmarks on your own machines.
Some of the headline gains from GitHub Actions CI:
HTTP/1.1: echo is 2.3× net/http; pipelining is 6.9× net/http and 1.65× fasthttp, on a tenth of
net/http's memory
HTTP/2: echo is 3.7× net/http; multiplexed it's 13× net/http and 2.6× Rust's h2
HTTP/3: 3.4–5.4× quic-go's throughput, and past 90% of Cloudflare's quiche
TLS: pipelining is 2.9× crypto/tls over net, on about a third of the memory
WebSocket: the highest echo throughput of the 17 servers tested, on 32 MB
Echo is 39% ahead of fasthttp on a sixth of its memory. Pipelining shows the biggest gap: fib
reaches 1.14 million TPS, 3.8× axum, and stays around 36 MB the whole time. In my own runs at
other connection counts, fib also matches or beats fasthttp.
This is the biggest jump of the lot: several times quic-go's throughput, on 40–60% less memory.
fib still uses more memory than quiche, which is next on the list.
Activity
bradfitz commentedon May 18, 2016
/cc @ianlancetaylor @rsc
gopherbot commentedon May 18, 2016
CL https://golang.org/cl/23227 mentions this issue.
net: don't return io.EOF from zero byte reads
net: don't return io.EOF from zero byte reads on Plan 9
RalphCorderoy commentedon May 20, 2016
read(2) with a count of zero may be used to detect errors. Linux man page confirms, as does POSIX's read(3p) here. Mentioning it in case it influences this subverting of a Read(0 bytes) not calling syscall.Read.
bradfitz commentedon Oct 21, 2016
I found a way to do without this in net/http, so punting to Go 1.9.
bradfitz commentedon Dec 12, 2016
Actually, the more I think about this, I don't even want my idle HTTP/RPC goroutines to stick around blocked in a read call. In addition to the array memory backed by the slice given to
Read, the goroutine itself is ~4KB of wasted memory.What I'd really like is a way to register a
func()to run when my*net.TCPConnis readable (when aReadcall wouldn't block). By analogy, I want thetime.AfterFuncefficiency of running a func in a goroutine later, rather than running a goroutine just to block in atime.Sleep.My new proposal is more like:
Yes, maybe this is getting dangerously into event-based programming land.
Or maybe just the name ("
OnWhatever") is offensive. Maybe there's something better.I would use this in http, http2, and grpc.
/cc @ianlancetaylor @rsc
ianlancetaylor commentedon Dec 12, 2016
Sounds like you are getting close to #15021.
I'm worried that the existence of such a method will encourage people to start writing their code as callbacks rather than as straightforward goroutines.
bradfitz commentedon Dec 12, 2016
Yeah. I'm conflicted. I see the benefits and the opportunity for overuse.
dvyukov commentedon Jan 6, 2017
If we do OnReadable(f func()), won't we need to fork half of standard library for async style? Compress, io, tls, etc readers all assume blocking style and require a blocked goroutine.
I don't see any way to push something asynchronously into e.g. gzip.Reader. Does this mean that I have to choose between no blocked goroutine + my own gzip impl and blocked goroutine + std lib?
124 remaining items
black23eep commentedon Jun 17, 2025
Is there any progress on this issue now?
scratchmex commentedon Dec 17, 2025
why can't we have a chan-like interface to use in select? like proposed in #15735 (comment). This to have a single loop for send and receive on a connection
ianlancetaylor commentedon Dec 17, 2025
It's difficult to implement efficiently in the runtime. At least, I don't see how to do it.
Note that you can already do something similar by having a goroutine read data from the network connection and send it on a channel. Yes, that requires an extra goroutine.
gobwas commentedon Dec 17, 2025
And an extra memory buffer – which I think was the reasoning for this feature request.
upd. My comment above is just to highlight that mentioned channel and a goroutine did basically a full circle in this discussion :)
xnoreq commentedon Feb 1, 2026
Go's entire standard library was designed around blocking I/O and not for high(est) performance, so why should it be any different in this issue's case?
If you want to select on read events of thousands of fds at highest possible performance/lowest possible overhead then you can't use idiomatic Go (goroutines, channels, select) anyway because it's not designed for that. You can still write your own unidiomatic, non-portable, low-level code though.
So I think the requirements and expectations should be clear: provide a method that blocks until data is available.
That's the only approach that is consistent with Go's design.
Users can wrap a goroutine around that, send "ready" events to their own (buffered) channels, etc.
DemiMarie commentedon Feb 1, 2026
The options I know of are (from fastest to slowest):
For maximum performance while still not relying on elevated privileges, I think it would be best to use a proactor model with
io_uring. If CGo were faster, binding to an optimized C or Rust library for this would be the obvious choice, at least if there were not lifetime constraints that forced the Go bindings to be highly unsafe.internal/poll: document why zero-byte reads do not wait for readability
internal/poll: document why zero-byte reads do not wait for readability
gopherbot commentedon Jul 2, 2026
Change https://go.dev/cl/796620 mentions this issue:
internal/poll: document why zero-byte reads do not wait for readabilityinternal/poll: document why zero-byte reads do not wait for readability
internal/poll: document why zero-byte reads do not wait for readability
lesismal commentedon Oct 3, 2026
I've created a new event-driven, high-performance, low-memory Go network library: fib. It's faster than almost every Go networking library before it, built with Claude, with almost all of its code generated by Claude.
Repository: fib
It supports TLS, HTTP 1/2/3, and WebSockets, and its performance surpasses almost all previous Go libraries. In some scenarios, its performance is close to or even better than some C/C++/Rust libraries.
In the benchmarks automatically run on GitHub Actions, fib achieves several times higher throughput and significantly less memory usage compared to standard libraries in some scenarios. It also shows performance improvements compared to libraries known for their performance, such as fasthttp, herz. Compared to C/C++/Rust frameworks, it's very close, and in some scenarios, it even surpasses them.
Of course, different hardware specifications, test parameters, and framework configurations may result in different results. You are welcome to run these benchmarks on your own machines.
Some of the headline gains from GitHub Actions CI:
net/http's memory
net, on about a third of the memoryHTTP/1.1 (Actions run, Rust tokio client)
Echo is 39% ahead of fasthttp on a sixth of its memory. Pipelining shows the biggest gap: fib
reaches 1.14 million TPS, 3.8× axum, and stays around 36 MB the whole time. In my own runs at
other connection counts, fib also matches or beats fasthttp.
HTTP/2 h2c (Actions run)
Echo reaches 90% of Rust's h2 on half its memory. Multiplexed, it turns around and fib is 2.6×
ahead of h2.
HTTP/3 (Actions run, quiche client)
This is the biggest jump of the lot: several times quic-go's throughput, on 40–60% less memory.
fib still uses more memory than quiche, which is next on the list.
For more details, please see here:
https://github.com/lesismal/fib/blob/main/docs/blog/introducing-fib.md
Complete benchmark code for each protocol:
https://github.com/lesismal/go-tls-benchmark
https://github.com/lesismal/go-http1-benchmark
https://github.com/lesismal/go-http2-benchmark
https://github.com/lesismal/go-http3-benchmark
https://github.com/lesismal/go-websocket-benchmark