Skip to content

Outgoing connections, UDP and framing (roadmap 12, 13, 15) - #28

Merged
y001j merged 6 commits into
masterfrom
feat/dial-udp-codec
Nov 22, 2026
Merged

y001j merged 6 commits into
masterfrom
feat/dial-udp-codec

Conversation

@y001j

@y001j y001j commented Nov 22, 2026

Copy link
Copy Markdown
Owner

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 with IORING_OP_CONNECT from one of the loops and returns once OnOpen has run; ctx bounds resolution + connect, and an abandoned dial is cancelled without calling OnOpen.
  • DialAsync reports on the loop; Conn.DialAsync dials 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).
  • connCtx is set before OnOpen; NewClient runs loops without a listener. tcp/tcp4/tcp6/unix (incl. abstract). Stats.Dialed.

UDP (item 13)

  • ListenPacket("udp"/"udp4"/"udp6", ...): one SO_REUSEPORT socket per ring (same port, also for :0), multishot recvmsg into 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 / AsyncWriteTo send with sendmsg (header, iovec, address and payload in one allocation kept alive until completion). Start lets a :0 PacketServer act as a UDP client. UDP counters in Stats.

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 with AppendFrame/Payload.

Fix

  • Listen("unix", ...) failed ("setsockopt: operation not supported"): SO_REUSEPORT was 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 -race in the Lima VM (Linux 6.17), several runs each; staticcheck clean.

🤖 Generated with Claude Code

y001j and others added 6 commits November 22, 2026 20:57
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

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.14815% with 118 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.62%. Comparing base (70997cf) to head (6e56453).

Files with missing lines Patch % Lines
dial.go 64.77% 56 Missing ⚠️
udp.go 77.52% 49 Missing ⚠️
codec.go 89.33% 8 Missing ⚠️
server.go 80.00% 4 Missing ⚠️
stats.go 97.72% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@y001j
y001j merged commit 1547fd8 into master Nov 22, 2026
6 checks passed
@y001j
y001j deleted the feat/dial-udp-codec branch November 22, 2026 12:16
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