Skip to content

net: add mechanism to wait for readability on a TCPConn #15735

Open

Description

@bradfitz

EDIT: this proposal has shifted. See #15735 (comment) below.

Old:

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.

Activity

  1. added this to the Go1.8 milestone on May 18, 2016
  2. self-assigned this
    on May 18, 2016
  3. bradfitz commented on May 18, 2016

    @bradfitz
    ContributorAuthor
  4. gopherbot commented on May 18, 2016

    @gopherbot
    Contributor

    CL https://golang.org/cl/23227 mentions this issue.

  5. RalphCorderoy commented on May 20, 2016

    @RalphCorderoy

    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.

  6. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Oct 7, 2016
  7. bradfitz commented on Oct 21, 2016

    @bradfitz
    ContributorAuthor

    I found a way to do without this in net/http, so punting to Go 1.9.

  8. modified the milestones: Go1.9, Go1.8 on Oct 21, 2016
  9. bradfitz commented on Dec 12, 2016

    @bradfitz
    ContributorAuthor

    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(f func()) {
       // ...
    }

    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

  10. ianlancetaylor commented on Dec 12, 2016

    @ianlancetaylor
    Contributor

    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.

  11. bradfitz commented on Dec 12, 2016

    @bradfitz
    ContributorAuthor

    Yeah. I'm conflicted. I see the benefits and the opportunity for overuse.

  12. dvyukov commented on Jan 6, 2017

    @dvyukov
    Member

    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?

  13. 124 remaining items

  14. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    and removed
    v2An incompatible library change
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Aug 23, 2023
  15. black23eep commented on Jun 17, 2025

    @black23eep

    Is there any progress on this issue now?

  16. scratchmex commented on Dec 17, 2025

    @scratchmex

    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

    // send and recv loop
    for {
    	select {
    	case p <- SendChan:
    		conn.Write(...)
    	case <-conn.Readable():
    		conn.Read(...)
    	}
    }
  17. ianlancetaylor commented on Dec 17, 2025

    @ianlancetaylor
    Contributor

    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.

  18. gobwas commented on Dec 17, 2025

    @gobwas

    Yes, that requires an extra goroutine.

    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 :)

  19. xnoreq commented on Feb 1, 2026

    @xnoreq

    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.

  20. DemiMarie commented on Feb 1, 2026

    @DemiMarie
    Contributor

    The options I know of are (from fastest to slowest):

    1. Use DPDK/XDP and implement a userspace TCP stack.
    2. Use a vendor-specific stack that uses kernel bypass to emulate 3 or 4.
    3. Use io_uring with preregistered buffers.
    4. 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.

  21. gopherbot commented on Jul 2, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/796620 mentions this issue: internal/poll: document why zero-byte reads do not wait for readability

  22. lesismal commented on Oct 3, 2026

    @lesismal

    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:

    • 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

    HTTP/1.1 (Actions run, Rust tokio client)

    Server Echo TPS Echo MEM Pipeline TPS Pipeline MEM
    fib 194,671 32 MB 1,140,457 36 MB
    axum (Rust) 236,244 227 MB 301,804 490 MB
    fasthttp 139,830 197 MB 692,671 197 MB
    net/http 84,739 314 MB 164,811 323 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.

    HTTP/2 h2c (Actions run)

    Server Echo TPS Echo MEM Multiplex TPS Multiplex MEM
    fib 129,676 119 MB 333,692 432 MB
    h2 (Rust) 143,107 246 MB 129,729 870 MB
    net/http 35,415 633 MB 25,051 809 MB

    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)

    Server Echo TPS Echo MEM Multiplex TPS Multiplex MEM
    quiche (Rust) 72,919 387 MB 101,233 881 MB
    fib 67,396 504 MB 94,443 1.05 GB
    quic-go 19,545 1.20 GB 17,505 1.80 GB

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions