$ show xtaci/kcp-go
kcp-go is a production-grade, high-performance reliable UDP library for Go, implementing the KCP (KCP Protocol) ARQ protocol. It provides a stream-based interface (net.Conn) over UDP with low latency, high throughput, and packet-level control. The library is widely used in gaming, streaming, and network acceleration (e.g., kcptun). Architecturally, it is layered: an application layer using net.Conn, a session layer (sess.go) managing encryption, FEC, and connection lifecycle, a pure protocol state machine (kcp.go) handling ARQ, retransmission, and congestion control, and a transport layer using raw UDP sockets. The data pipeline for outgoing packets is: App Write -> KCP Send -> KCP flush -> FEC Encode -> Encrypt -> UDP Socket. Incoming: UDP Socket -> Decrypt -> FEC Decode -> KCP Input -> App Read. Key features include: reliable ARQ with fast retransmit, Forward Error Correction (FEC) using Reed-Solomon codes, packet-level encryption (AES, TEA, Salsa20, etc., plus AEAD support), platform-specific optimizations (sendmmsg/recvmmsg on Linux), a global timer scheduler to avoid per-session goroutines, and a buffer pool to minimize GC pressure. The codebase demonstrates exceptional craftsmanship with hand-optimized CFB encryption (8x loop unrolling), a generic ring buffer with dynamic resizing, and an auto-tuning FEC decoder that adapts to peer parameters.
The LLM identified 3 files worth highlighting for reuse:
Implements an auto-tuning algorithm for FEC parameter detection. It samples incoming FEC packet types (data/parity) into a fixed-size ring buffer, sorts by sequence number, and scans for rising/falling edges to determine the data and parity shard counts. This allows the decoder to dynamically adapt to the peer's FEC configuration without prior knowledge. The component is self-contained, uses no external dependencies beyond the standard library, and could be extracted for any application needing to detect repeating binary patterns in a stream.
A two-stage parallel timed task scheduler. Stage 1 collects tasks from callers under a mutex and feeds them into a channel. Stage 2 runs multiple worker goroutines (one per CPU), each maintaining a local min-heap of timed tasks. Workers execute overdue tasks immediately and use a timer for the earliest future task. This design decouples task submission from scheduling, avoids per-task goroutines, and distributes timer-driven work across CPUs. The component is isolated, uses only standard library (container/heap, sync, time), and can be reused in any Go application requiring efficient deferred execution.
A generic ring buffer (circular buffer) with dynamic resizing. It provides efficient FIFO queue operations with amortized O(1) push/pop. The growth policy doubles capacity below a threshold (1024) and increases by 10% above it. It supports iteration (ForEach, ForEachReverse), discard, peek, and clear operations. The implementation is self-contained, uses no external dependencies, and can be extracted as a standalone generic data structure for any Go project needing a resizable ring buffer.