Skip to main content

SyncBatcher

Struct SyncBatcher 

Source
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:

  1. Initializes resources once via the init closure (e.g., TextEmbedding)
  2. 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>
where Req: Send + 'static, Res: Send + 'static, E: Clone + Send + 'static,

Source

pub fn new<F, G>(config: BatcherConfig, init: F) -> Self
where F: FnOnce() -> G + Send + 'static, G: FnMut(Vec<Req>) -> Result<Vec<Res>, E> + Send + 'static,

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.

Source

pub async fn run(&self, item: Req) -> Result<Res, E>

Submit one item for processing.

Waits until the worker has processed the batch containing this item, then returns its result.

Returns Err(E) if the worker died before processing the request, or if the processing closure returned an error.

Trait Implementations§

Source§

impl<Req: Clone, Res: Clone, E: Clone> Clone for SyncBatcher<Req, Res, E>

Source§

fn clone(&self) -> SyncBatcher<Req, Res, E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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>
where Req: Send, Res: Send, E: Send,

§

impl<Req, Res, E> Sync for SyncBatcher<Req, Res, E>
where Req: Send, Res: Send, E: Send,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.