pub struct LockFreeEventQueue { /* private fields */ }Expand description
Lock-free bounded event queue for high-throughput ingestion
§Design Pattern
Uses crossbeam’s lock-free MPMC (Multi-Producer, Multi-Consumer) queue to eliminate contention in the hot path of event ingestion.
§Benefits
- Zero Lock Contention: Multiple threads can push/pop concurrently
- Predictable Latency: No waiting for locks
- High Throughput: Optimized for concurrent access
- Backpressure Handling: Returns error when full
§Performance
- Push: ~10-20ns (lock-free)
- Pop: ~10-20ns (lock-free)
- vs RwLock: 100-500ns (with contention)
§Example
let queue = LockFreeEventQueue::new(10000);
// Multiple producers can push concurrently
let event = Event::from_strings(...)?;
queue.try_push(event)?;
// Multiple consumers can pop concurrently
if let Some(event) = queue.try_pop() {
// Process event
}Implementations§
Source§impl LockFreeEventQueue
impl LockFreeEventQueue
Sourcepub fn try_push(&self, event: Event) -> Result<()>
pub fn try_push(&self, event: Event) -> Result<()>
Try to push an event to the queue (non-blocking)
Returns an error if the queue is full. Callers should implement backpressure handling (e.g., retry with exponential backoff, or return HTTP 503 Service Unavailable).
§Performance
- Lock-free operation (~10-20ns)
- No waiting on contention
- Constant time O(1)
Sourcepub fn try_pop(&self) -> Option<Event>
pub fn try_pop(&self) -> Option<Event>
Try to pop an event from the queue (non-blocking)
Returns None if the queue is empty.
§Performance
- Lock-free operation (~10-20ns)
- No waiting on contention
- Constant time O(1)
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get current queue length
Note: This is approximate in concurrent scenarios due to race conditions between length check and actual push/pop operations.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if queue is empty
Note: Result may be stale in concurrent scenarios.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Check if queue is full
Note: Result may be stale in concurrent scenarios.
Sourcepub fn fill_ratio(&self) -> f64
pub fn fill_ratio(&self) -> f64
Get approximate fill percentage (0.0 to 1.0)
Trait Implementations§
Source§impl Clone for LockFreeEventQueue
impl Clone for LockFreeEventQueue
Source§fn clone(&self) -> LockFreeEventQueue
fn clone(&self) -> LockFreeEventQueue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more