Repository navigation
Outgoing connections, UDP and framing (roadmap 12, 13, 15) - #28
Merged
Merged
Conversation
Stats returns counters summed over the rings: open connections, accepted, accept errors, bytes read/written, idle and write timeouts, receive-buffer exhaustion (-ENOBUFS), backpressure pauses and zero-copy sends. The counters are plain fields updated on the loop thread, so the data path pays nothing; Stats reads them by posting to each loop. example_test.go adds pkg.go.dev examples for Listen with graceful shutdown, Conn.AsyncWrite from other goroutines, per-connection state and close reasons, and publishing Stats with expvar. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Framed(f, h) splits each connection's stream into frames and calls h.OnMessage once per frame. Frames that arrive whole are passed straight from the receive buffer (writing them back sends in place); partial frames are kept per connection until the rest arrives. A framer error, such as ErrFrameTooLarge, closes the connection and reaches OnClose. Built-in framers: NewLengthField (1/2/4/8-byte length, either byte order, payload limit), NewDelimiter (with a frame limit) and FixedLength; each can build frames with AppendFrame and strip headers with Payload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Listen enables SO_REUSEPORT by default, which unix sockets reject, so
Listen("unix", ...) failed with "setsockopt: operation not supported"
unless WithReusePort(false) was given. Unix sockets already share one
listening socket across rings; the option is now left off for them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Server.Dial(ctx, network, address, connCtx) connects from one of the event loops with IORING_OP_CONNECT and returns once the connection is up and OnOpen has run; ctx bounds resolution and connect, and an abandoned dial is cancelled without calling OnOpen. - DialAsync reports through a callback on the loop; Conn.DialAsync dials on the same loop as an existing connection, so the two can write to each other directly (a proxy in a few lines, see TestDialProxy). - connCtx is set before OnOpen so handlers can tell outgoing connections apart. - NewClient runs event loops without a listener for clients. - Name resolution runs off the loop; tcp, tcp4, tcp6 and unix (including abstract) addresses. Stats.Dialed counts established outgoing connections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- ListenPacket("udp"/"udp4"/"udp6", address, handler, opts...) opens
one socket per ring with SO_REUSEPORT (all bound to the same port,
also for port 0) and receives with multishot recvmsg into the
provided buffer ring (Linux 6.0+): one buffer per datagram, holding
io_uring_recvmsg_out, the source address and the payload.
- OnPacket(pc, data, from) gets the source as a netip.AddrPort, so
receiving allocates nothing. Datagrams larger than a buffer are
dropped and counted.
- PacketConn.WriteTo on the loop and AsyncWriteTo / PacketServer.
AsyncWriteTo from any goroutine send with sendmsg; header, iovec,
address and payload live in one allocation kept alive until the send
completes. IPv4 destinations work from IPv6 sockets (mapped).
- Start runs a PacketServer without blocking, so one bound to port 0
serves as a UDP client.
- Stats gain PacketsReceived, PacketsSent, PacketsTruncated and
SendErrors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README (EN/中文) sections for Dial/DialAsync/NewClient, Framed and ListenPacket; pkg.go.dev examples for a length-prefixed echo server, a TCP proxy with Conn.DialAsync, a client and a UDP echo server; changelog (Unreleased) and roadmap items 12, 13 and 15. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #28 +/- ##
==========================================
+ Coverage 60.17% 66.62% +6.45%
==========================================
Files 18 22 +4
Lines 1052 1579 +527
==========================================
+ Hits 633 1052 +419
- Misses 419 527 +108 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Builds on #27 (Server.Stats), whose commit is included; merge #27 first or merge this one alone.
Outgoing connections (item 12)
Server.Dial(ctx, network, address, connCtx)connects withIORING_OP_CONNECTfrom one of the loops and returns onceOnOpenhas run;ctxbounds resolution + connect, and an abandoned dial is cancelled without callingOnOpen.DialAsyncreports on the loop;Conn.DialAsyncdials on the same loop as an existing connection so both can write to each other directly — a full TCP proxy is ~40 lines (TestDialProxy,ExampleConn_DialAsync).connCtxis set beforeOnOpen;NewClientruns loops without a listener. tcp/tcp4/tcp6/unix (incl. abstract).Stats.Dialed.UDP (item 13)
ListenPacket("udp"/"udp4"/"udp6", ...): oneSO_REUSEPORTsocket per ring (same port, also for:0), multishotrecvmsginto the provided buffer ring; the buffer layout (io_uring_recvmsg_out+ address + payload) is parsed in place.OnPacket(pc, data, from netip.AddrPort)— no allocation per datagram. Oversized datagrams are dropped and counted.PacketConn.WriteTo/AsyncWriteTosend withsendmsg(header, iovec, address and payload in one allocation kept alive until completion).Startlets a:0PacketServer act as a UDP client. UDP counters inStats.Framing (item 15)
Framed(framer, handler)→OnMessage(c, msg)once per complete frame. Whole frames come straight from the receive buffer (writing them back sends in place); partial frames are buffered per connection. Framer errors (ErrFrameTooLarge) close the connection.NewLengthField(1/2/4/8 bytes, either byte order),NewDelimiter,FixedLength, each withAppendFrame/Payload.Fix
Listen("unix", ...)failed ("setsockopt: operation not supported"):SO_REUSEPORTwas set on the unix socket. This bug is in v0.1.0, so a v0.1.1 may be worth tagging.Docs & tests
README (EN/中文) sections, pkg.go.dev examples, changelog (Unreleased), roadmap. New tests: 5 dial (echo, refused, ctx timeout, unix, 8-connection proxy with 1MB each), 5 framing, 7 UDP (echo incl. 16KB, reuseport spread, truncation, client mode, IPv6, shared-socket close/fd leak, 16×500 load). All pass under
-racein the Lima VM (Linux 6.17), several runs each; staticcheck clean.🤖 Generated with Claude Code