Documentation
¶
Overview ¶
Package udpbatch sends and receives multiple UDP datagrams per system call on Darwin, using the kernel's sendmsg_x and recvmsg_x entry points.
Linux programs batch UDP with sendmmsg and recvmmsg. Darwin exposes the same capability only through private symbols declared in xnu's bsd/sys/socket_private.h, so Go UDP stacks on macOS otherwise issue one system call per datagram.
[Available] reports whether the symbols resolved and passed a loopback self-test on the running kernel; every call returns [ErrUnavailable] if not. Callers must keep a per-datagram fallback path; this package is an optimization, never a requirement.
Connections, not descriptors ¶
[Send] and [Recv] take a syscall.RawConn rather than a bare file descriptor and run inside its Write or Read callback. This avoids fd reuse races with Go's network poller and parks the goroutine on EAGAIN instead of busy-spinning.
Sending requires connected sockets ¶
socket_private.h documents that msg_name, msg_namelen, msg_control, and msg_controllen must be zero on input to sendmsg_x. [Send] therefore accepts payloads without per-datagram destinations and requires a connected socket. [Recv] populates each [Message.Addr] and works on unconnected sockets.
Partial transfers ¶
Like sendmmsg, the kernel may accept fewer datagrams than offered. [Send] returns the accepted count; callers retry the remaining tail.
Version behavior ¶
Private syscalls can change behavior across macOS releases:
- macOS 10.15: recvmsg_x does not write back msg_controllen (control messages are not exposed here).
- macOS 26.x (measured by this package's tests): truncation is silent. Although xnu's header documents syscall.MSG_TRUNC in msg_flags when a datagram exceeds the buffer, the kernel clamps msg_datalen to the buffer size without setting MSG_TRUNC. Size [Message.Payload] strictly above the largest expected datagram if truncation must be distinguished from an exact fit.
Performance ¶
Existing throughput numbers for this package (for example from examples/network/udpbatchbench -sweep) are loopback-only: they measure syscall-boundary overhead, not real NIC queuing, pacing, or wire loss. [BatchSize] (32) is a starting point rather than a wire-tuned value until non-loopback wire benchmarks are captured; see BenchmarkWireSend (UDPBATCH_WIRE_PEER=host:port) to measure batch sizes over a real interface.
Distribution ¶
Resolving private symbols with dlsym is flagged by Mac App Store static analysis. Do not link this package into an App Store build.