Struct PartitionedQueue
pub struct PartitionedQueue {
partitions: Vec<SpscQueue>,
partition_count: usize,
dequeue_index: AtomicU64,
}Expand description
A partitioned queue for reduced contention with multiple producers.
Instead of a single queue with a lock, this uses multiple independent partitions (SPSC queues) to reduce contention when many producers are sending messages concurrently.
Producers are routed to partitions based on their source ID, ensuring messages from the same source go to the same partition (preserving order).
§Example
use ringkernel_core::queue::{PartitionedQueue, QueueTier};
// Create 4 partitions with Medium tier capacity each
let queue = PartitionedQueue::new(4, QueueTier::Medium.capacity());
// Enqueue with source-based routing
queue.try_enqueue_from(source_id, envelope)?;
// Dequeue from any partition that has messages
if let Some(envelope) = queue.try_dequeue_any() {
// process message
}Fields§
§partitions: Vec<SpscQueue>§partition_count: usize§dequeue_index: AtomicU64Implementations§
§impl PartitionedQueue
impl PartitionedQueue
pub fn new(
partition_count: usize,
capacity_per_partition: usize,
) -> PartitionedQueue
pub fn new( partition_count: usize, capacity_per_partition: usize, ) -> PartitionedQueue
Creates a new partitioned queue.
§Arguments
partition_count- Number of partitions (should be power of 2 for efficiency)capacity_per_partition- Capacity of each partition
pub fn with_defaults() -> PartitionedQueue
pub fn with_defaults() -> PartitionedQueue
Creates a partitioned queue with default settings.
Uses 4 partitions with Medium tier capacity.
pub fn for_high_contention() -> PartitionedQueue
pub fn for_high_contention() -> PartitionedQueue
Creates a partitioned queue sized for high contention.
Uses 8 partitions with Large tier capacity.
pub fn partition_for(&self, source_id: u64) -> usize
pub fn partition_for(&self, source_id: u64) -> usize
Returns the partition index for a given source ID.
pub fn partition_count(&self) -> usize
pub fn partition_count(&self) -> usize
Returns the number of partitions.
pub fn capacity_per_partition(&self) -> usize
pub fn capacity_per_partition(&self) -> usize
Returns the capacity per partition.
pub fn total_capacity(&self) -> usize
pub fn total_capacity(&self) -> usize
Total capacity across all partitions.
pub fn total_messages(&self) -> usize
pub fn total_messages(&self) -> usize
Total messages across all partitions.
pub fn try_enqueue_from(
&self,
source_id: u64,
envelope: MessageEnvelope,
) -> Result<(), RingKernelError>
pub fn try_enqueue_from( &self, source_id: u64, envelope: MessageEnvelope, ) -> Result<(), RingKernelError>
Enqueues a message to a partition based on source ID.
Messages from the same source always go to the same partition, preserving ordering for that source.
pub fn try_enqueue(
&self,
envelope: MessageEnvelope,
) -> Result<(), RingKernelError>
pub fn try_enqueue( &self, envelope: MessageEnvelope, ) -> Result<(), RingKernelError>
Enqueues a message using the envelope’s source kernel ID.
pub fn try_dequeue_partition(
&self,
partition: usize,
) -> Result<MessageEnvelope, RingKernelError>
pub fn try_dequeue_partition( &self, partition: usize, ) -> Result<MessageEnvelope, RingKernelError>
Tries to dequeue from a specific partition.
pub fn try_dequeue_any(&self) -> Option<MessageEnvelope>
pub fn try_dequeue_any(&self) -> Option<MessageEnvelope>
Tries to dequeue from any partition that has messages.
Uses round-robin to fairly distribute dequeues across partitions.
pub fn partition_stats(&self, partition: usize) -> Option<QueueStats>
pub fn partition_stats(&self, partition: usize) -> Option<QueueStats>
Returns statistics for a specific partition.
pub fn stats(&self) -> PartitionedQueueStats
pub fn stats(&self) -> PartitionedQueueStats
Returns aggregated statistics across all partitions.
pub fn reset_stats(&self)
pub fn reset_stats(&self)
Resets statistics for all partitions.
Auto Trait Implementations§
impl !Freeze for PartitionedQueue
impl !RefUnwindSafe for PartitionedQueue
impl Send for PartitionedQueue
impl Sync for PartitionedQueue
impl Unpin for PartitionedQueue
impl UnwindSafe for PartitionedQueue
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
§fn deserialize(
&self,
deserializer: &mut D,
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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