pub struct BatchedQueueSender<T> { /* private fields */ }Expand description
A sender handle for adding items to a batched queue.
BatchedQueueSender provides methods to add items to a batched queue from multiple
threads. It handles the details of batch management and automatic flushing of batches
when they reach the configured size.
§Examples
use batched_queue::BatchedQueue;
use std::thread;
let queue = BatchedQueue::<String>::new(5).expect("Failed to create queue");
let sender = queue.create_sender();
// Share the sender with another thread
thread::spawn(move || {
for i in 0..10 {
sender.push(format!("Item {}", i)).expect("Failed to push item");
}
// Ensure any remaining items are sent
sender.flush().expect("Failed to flush");
});Implementations§
Source§impl<T: Send + Clone + 'static> BatchedQueueSender<T>
impl<T: Send + Clone + 'static> BatchedQueueSender<T>
Sourcepub fn push(&self, item: T) -> Result<(), BatchedQueueError>
pub fn push(&self, item: T) -> Result<(), BatchedQueueError>
Adds an item to the queue.
If adding this item causes the current batch to reach the configured batch size, the batch will be automatically sent for processing. This method will block if the channel is bounded and full.
§Arguments
item- The item to add to the queue
§Examples
use batched_queue::BatchedQueue;
let queue = BatchedQueue::<i32>::new(5).expect("Failed to create queue");
let sender = queue.create_sender();
for i in 0..10 {
sender.push(i).expect("Failed to push item");
}Sourcepub fn try_push(&self, item: T) -> Result<(), BatchedQueueError>
pub fn try_push(&self, item: T) -> Result<(), BatchedQueueError>
Attempts to add an item to the queue without blocking.
This method is similar to push, but it will not block if the channel
is bounded and full. Instead, if a full batch cannot be sent because
the channel is full, the batch is kept in the current batch and will
be sent on a future push or flush operation.
§Arguments
item- The item to add to the queue
§Examples
use batched_queue::BatchedQueue;
// Create a queue with limited capacity
let queue = BatchedQueue::<i32>::new_bounded(5, 1).expect("Failed to create queue");
let sender = queue.create_sender();
for i in 0..20 {
sender.try_push(i);
}Sourcepub fn flush(&self) -> Result<(), BatchedQueueError>
pub fn flush(&self) -> Result<(), BatchedQueueError>
Flushes any pending items into a batch, even if the batch is not full.
This method will block if the channel is bounded and full.
§Error
Returns BatchedQueueError::Disconnected if the receiving end has been dropped,
§Examples
use batched_queue::BatchedQueue;
let queue = BatchedQueue::<i32>::new(10).expect("Failed to create queue");
let sender = queue.create_sender();
// Add some items, but not enough to form a complete batch
for i in 0..3 {
sender.push(i).expect("Failed to push item");
}
// Flush to ensure items are sent for processing
sender.flush().expect("Failed to flush");Sourcepub fn try_flush(&self) -> Result<(), BatchedQueueError>
pub fn try_flush(&self) -> Result<(), BatchedQueueError>
Attempts to flush any pending items without blocking.
§Errors
Returns BatchedQueueError::Disconnected if the receiving end has been dropped,
or BatchedQueueError::ChannelFull if the channel is full.
§Examples
use batched_queue::BatchedQueue;
// Create a queue with limited capacity
let queue = BatchedQueue::<i32>::new_bounded(5, 1).expect("Failed to create queue");
let sender = queue.create_sender();
for i in 0..3 {
sender.push(i).expect("Failed to push item");
}
// Try to flush without blocking
if !sender.try_flush().is_ok() {
println!("Channel is full, will try again later");
}