pub struct MessageBatcher { /* private fields */ }Available on non-WebAssembly only.
Expand description
Message batcher that groups notifications.
Implementations§
Source§impl MessageBatcher
impl MessageBatcher
Sourcepub fn new(config: BatchingConfig) -> Self
pub fn new(config: BatchingConfig) -> Self
Create a new message batcher.
§Examples
use pmcp::utils::{MessageBatcher, BatchingConfig};
use std::time::Duration;
// Default configuration
let batcher = MessageBatcher::new(BatchingConfig::default());
// Custom configuration for high-throughput scenarios
let config = BatchingConfig {
max_batch_size: 50,
max_wait_time: Duration::from_millis(200),
batched_methods: vec!["logs.add".to_string(), "progress.update".to_string()],
};
let high_throughput_batcher = MessageBatcher::new(config);
// Configuration for low-latency scenarios
let low_latency_config = BatchingConfig {
max_batch_size: 5,
max_wait_time: Duration::from_millis(10),
batched_methods: vec![],
};
let low_latency_batcher = MessageBatcher::new(low_latency_config);Sourcepub async fn add(&self, notification: Notification) -> Result<()>
pub async fn add(&self, notification: Notification) -> Result<()>
Add a notification to the batch.
§Examples
use pmcp::utils::{MessageBatcher, BatchingConfig};
use pmcp::types::{Notification, ClientNotification, ServerNotification};
use std::time::Duration;
let config = BatchingConfig {
max_batch_size: 3,
max_wait_time: Duration::from_millis(100),
batched_methods: vec![],
};
let batcher = MessageBatcher::new(config);
// Add various notifications
batcher.add(Notification::Client(ClientNotification::Initialized)).await?;
batcher.add(Notification::Client(ClientNotification::RootsListChanged)).await?;
// This will trigger immediate batch send (max_batch_size reached)
batcher.add(Notification::Server(ServerNotification::ToolsChanged)).await?;Sourcepub fn start_timer(&self)
pub fn start_timer(&self)
Start the batching timer.
§Examples
use pmcp::utils::{MessageBatcher, BatchingConfig};
use pmcp::types::{Notification, ClientNotification};
use std::time::Duration;
let config = BatchingConfig {
max_batch_size: 10,
max_wait_time: Duration::from_millis(50),
batched_methods: vec![],
};
let batcher = MessageBatcher::new(config);
// Start the timer to automatically flush batches
batcher.start_timer();
// Add notifications that won't reach max_batch_size
batcher.add(Notification::Client(ClientNotification::Initialized)).await?;
// Timer will ensure this gets sent after max_wait_timeSourcepub async fn receive_batch(&self) -> Option<Vec<Notification>>
pub async fn receive_batch(&self) -> Option<Vec<Notification>>
Receive the next batch of notifications.
§Examples
use pmcp::utils::{MessageBatcher, BatchingConfig};
use pmcp::types::{Notification, ClientNotification};
use std::time::Duration;
let config = BatchingConfig {
max_batch_size: 2,
max_wait_time: Duration::from_millis(100),
batched_methods: vec![],
};
let batcher = MessageBatcher::new(config);
batcher.start_timer();
// Add notifications
batcher.add(Notification::Client(ClientNotification::Initialized)).await?;
batcher.add(Notification::Client(ClientNotification::RootsListChanged)).await?;
// Receive the batch (triggered by max_batch_size)
if let Some(batch) = batcher.receive_batch().await {
println!("Received batch with {} notifications", batch.len());
for notification in batch {
// Process each notification
}
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for MessageBatcher
impl !RefUnwindSafe for MessageBatcher
impl Send for MessageBatcher
impl Sync for MessageBatcher
impl Unpin for MessageBatcher
impl !UnwindSafe for MessageBatcher
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
Mutably borrows from an owned value. Read more