pub struct SyncBatcher<Req, Res, E> { /* private fields */ }Expand description
A batcher for synchronous workloads with thread-local resources.
Unlike crate::Batcher, which runs an async process closure on the tokio runtime,
SyncBatcher spawns a dedicated OS thread that:
- Initializes resources once via the
initclosure (e.g.,TextEmbedding) - Processes batches synchronously using those resources
This avoids spawn_blocking overhead for each request and keeps thread-local
state alive on the worker thread.
§Example
let config = BatcherConfig::default();
let batcher: SyncBatcher<String, Embedding, String> = SyncBatcher::new(config, || {
let embedding = TextEmbedding::try_new(()).unwrap();
move |items: Vec<String>| {
embedding.embed(items)
}
});
let result = batcher.run("hello".to_string()).await?;Implementations§
Source§impl<Req, Res, E> SyncBatcher<Req, Res, E>
impl<Req, Res, E> SyncBatcher<Req, Res, E>
Sourcepub fn new<F, G>(config: BatcherConfig, init: F) -> Self
pub fn new<F, G>(config: BatcherConfig, init: F) -> Self
Spawn the sync batch worker on a dedicated thread.
init runs once on the worker thread and returns the processing closure.
This is where you initialize thread-local resources (e.g., TextEmbedding).
The processing closure receives a Vec<Req> and must return
Result<Vec<Res>, E> synchronously.
Trait Implementations§
Auto Trait Implementations§
impl<Req, Res, E> Freeze for SyncBatcher<Req, Res, E>
impl<Req, Res, E> RefUnwindSafe for SyncBatcher<Req, Res, E>
impl<Req, Res, E> Send for SyncBatcher<Req, Res, E>
impl<Req, Res, E> Sync for SyncBatcher<Req, Res, E>
impl<Req, Res, E> Unpin for SyncBatcher<Req, Res, E>
impl<Req, Res, E> UnsafeUnpin for SyncBatcher<Req, Res, E>
impl<Req, Res, E> UnwindSafe for SyncBatcher<Req, Res, E>
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