Expand description
Lock-free connection pool — mechanical sympathy design.
Principles applied (per optimization guide):
-
No blocking primitives on hot path — crossbeam::ArrayQueue is Dmitry Vyukov’s bounded MPMC queue with pure CAS loops. No Mutex, no Semaphore. tokio::sync::Notify uses futex (Linux) / parking (macOS) — kernel boundary only when a waiter actually needs to sleep.
-
Cache-line false-sharding eliminated — crossbeam::ArrayQueue uses
CachePadded<AtomicUsize>for head and tail on separate cache lines. Producers and consumers never invalidate the same cache line. -
Zero allocation on hot path — All connections pre-allocated at construction. ArrayQueue buffer is fixed-size. No VecDeque growth, no Metrics per object, no Instant::now() on hot path.
-
Monormorphic dispatch —
acquire()andreturn_conn()are fully concrete methods on PoolInner. No trait objects, no vtable lookups on the queue path. Factory closures are set once at construction. -
Branchless inner loops — The CAS loops in ArrayQueue push/pop are tight spinning loops with backoff (pause on x86, wfe on ARM). No unpredictable branches — just cmp+cmpxchg until success.
-
Flat data structures — PoolInner is a flat struct. No nested Arc, no Weak, no Option overhead on idle queue slots.
-
Proper memory ordering — Acquire/Release semantics for size and closed state. Not Just Relaxed everywhere.
-
No virtual dispatch — Factory is boxed once at construction. The hot queue path uses monomorphic array operations.
Structs§
- Lock
Free Pool - Pool
Config - Pool
Status - Pooled
Connection - A connection checked out from the pool.