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 moreAuto Trait Implementations§
impl Freeze for LockFreeEventQueue
impl RefUnwindSafe for LockFreeEventQueue
impl Send for LockFreeEventQueue
impl Sync for LockFreeEventQueue
impl Unpin for LockFreeEventQueue
impl UnwindSafe for LockFreeEventQueue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more