$ show smallnest/rpcx
rpcx is a high-performance Go RPC framework inspired by Alibaba Dubbo and Weibo Motan. It provides a complete set of features for building microservices: multiple transport protocols (TCP, HTTP, QUIC, KCP, RDMA, io_uring), multiple serialization formats (JSON, Protobuf, MessagePack, Thrift, raw bytes), service discovery (peer-to-peer, DNS, mDNS, Redis, etcd, consul, zookeeper), load balancing (random, round-robin, weighted, consistent hashing, geographic, ICMP-based), fault tolerance (failover, failfast, failtry, failbackup), compression (gzip, snappy), and a plugin system for extensibility. The architecture separates client and server concerns with clear interfaces: RPCClient for low-level communication, XClient for high-level service governance with discovery and load balancing, and Server for hosting services with reflection-based registration. The codebase demonstrates strong engineering practices with extensive use of interfaces, dependency injection, and object pooling for performance. It includes a comprehensive test suite and benchmarks. Key components include a binary protocol with buffer pooling for zero-copy encoding, a plugin container with multiple extension points (pre/post call, connection handling, etc.), a circuit breaker implementation with sliding window, and various selectors for load balancing. The project has been actively maintained with recent refactoring to split large files into focused modules and add method-level service registration. The code is well-structured with clear separation of concerns, though some parts (like the unsafe pointer conversions in util/converter.go) show pragmatic trade-offs for performance.
The LLM identified 3 files worth highlighting for reuse:
Implements a sliding-window circuit breaker (ConsecCircuitBreaker) using atomic operations for thread safety. It tracks consecutive failures within a time window and opens the circuit when a threshold is exceeded. The Call method supports optional timeout via channel-based cancellation. This component is self-contained (imports only errors, sync/atomic, time) and can be extracted into any Go project needing circuit breaker functionality.
Provides multiple load balancing strategies: random, round-robin, weighted round-robin (using a ring buffer), consistent hashing (via doublejump), ICMP-based weighting, and geographic proximity selection. Each strategy implements a common Selector interface with Select and UpdateServer methods. The weighted round-robin implementation uses a custom ring data structure for O(1) selection. The consistent hash selector uses jump consistent hashing. The geo-selector calculates distances using the Haversine formula. This module is reusable for any client-side load balancing scenario.
Implements a limited-size buffer pool (LimitedPool) with logarithmic size classes for efficient memory reuse. The pool maintains multiple level pools for sizes between minSize and maxSize, using powers of two. The Get method returns a buffer of at least the requested size, and Put returns it to the appropriate pool. This pattern reduces allocation overhead for byte slices in high-throughput scenarios. The component is self-contained and can be reused in any Go application needing pooled buffer allocation.