pub struct BatchMiddleware { /* private fields */ }Expand description
Middleware that automatically batches messages before processing.
This middleware intercepts individual messages and assembles them into batches. When a batch is complete (by size or timeout), it processes all messages together using a BatchHandler, then acknowledges each message individually.
§Example
use foxtive_worker::middleware::batch::BatchMiddleware;
use foxtive_worker::{BatchConfig, BatchHandler, ReceivedBatchMessage, MessageBatch};
use foxtive_worker::error::WorkerResult;
use std::sync::Arc;
// Create a batch handler
struct MyBatchHandler;
#[async_trait::async_trait]
impl BatchHandler for MyBatchHandler {
async fn process_batch(&self, _batch: MessageBatch<serde_json::Value>) -> WorkerResult<()> {
Ok(())
}
}
// Configure batching
let config = BatchConfig::default()
.with_batch_size(10)
.with_flush_interval(std::time::Duration::from_secs(5));
// Create middleware
let batch_middleware = BatchMiddleware::new(Arc::new(MyBatchHandler), config);Implementations§
Source§impl BatchMiddleware
impl BatchMiddleware
Sourcepub fn new(handler: Arc<dyn BatchHandler>, config: BatchConfig) -> Self
pub fn new(handler: Arc<dyn BatchHandler>, config: BatchConfig) -> Self
Create a new batch middleware
Sourcepub async fn start(&mut self) -> WorkerResult<()>
pub async fn start(&mut self) -> WorkerResult<()>
Start the background batch processing task
Trait Implementations§
Source§impl Middleware for BatchMiddleware
impl Middleware for BatchMiddleware
Source§fn handle<'life0, 'async_trait>(
&'life0 self,
message: ReceivedMessage<Value>,
_next: Box<dyn MessageHandler>,
) -> Pin<Box<dyn Future<Output = Result<MiddlewareResult, WorkerError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle<'life0, 'async_trait>(
&'life0 self,
message: ReceivedMessage<Value>,
_next: Box<dyn MessageHandler>,
) -> Pin<Box<dyn Future<Output = Result<MiddlewareResult, WorkerError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Process a message with access to the next handler in the chain. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for BatchMiddleware
impl !UnwindSafe for BatchMiddleware
impl Freeze for BatchMiddleware
impl Send for BatchMiddleware
impl Sync for BatchMiddleware
impl Unpin for BatchMiddleware
impl UnsafeUnpin for BatchMiddleware
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