Skip to main content

Module lockfree_pool

Module lockfree_pool 

Source
Expand description

Lock-free connection pool — mechanical sympathy design.

Principles applied (per optimization guide):

  1. 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.

  2. 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.

  3. 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.

  4. Monormorphic dispatchacquire() and return_conn() are fully concrete methods on PoolInner. No trait objects, no vtable lookups on the queue path. Factory closures are set once at construction.

  5. 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.

  6. Flat data structures — PoolInner is a flat struct. No nested Arc, no Weak, no Option overhead on idle queue slots.

  7. Proper memory ordering — Acquire/Release semantics for size and closed state. Not Just Relaxed everywhere.

  8. No virtual dispatch — Factory is boxed once at construction. The hot queue path uses monomorphic array operations.

Structs§

LockFreePool
PoolConfig
PoolStatus
PooledConnection
A connection checked out from the pool.

Enums§

PoolError

Type Aliases§

BoxFuture
CreateFn
ValidateFn