batch_aint_one/
processor.rs

1use std::{
2    fmt::{Debug, Display},
3    future::Future,
4    hash::Hash,
5};
6
7/// Process a batch of inputs for a given key.
8///
9/// Should be cheap to clone.
10pub trait Processor: 'static + Send + Clone {
11    /// The key used to group inputs.
12    type Key: 'static + Debug + Eq + Hash + Clone + Send + Sync;
13    /// The input type for each item.
14    type Input: Send;
15    /// The output type for each item.
16    type Output: Send;
17    /// The error type that can be returned when processing a batch.
18    type Error: Send + Clone + Display + Debug;
19    /// The resources that will be acquired before processing each batch, and can be used during
20    /// processing.
21    type Resources: Send;
22
23    /// Acquire resources to be used for processing the next batch with the given key.
24    ///
25    /// This method is called before processing each batch.
26    ///
27    /// For `BatchingPolicy::Immediate`, the batch will keep accumulating items until the resources
28    /// are acquired.
29    ///
30    /// Can be used to e.g. acquire a database connection from a pool.
31    fn acquire_resources(
32        &self,
33        key: Self::Key,
34    ) -> impl Future<Output = Result<Self::Resources, Self::Error>> + Send;
35
36    /// Process the batch.
37    ///
38    /// The order of the outputs in the returned `Vec` must be the same as the order of the inputs
39    /// in the given iterator.
40    fn process(
41        &self,
42        key: Self::Key,
43        inputs: impl Iterator<Item = Self::Input> + Send,
44        resources: Self::Resources,
45    ) -> impl Future<Output = Result<Vec<Self::Output>, Self::Error>> + Send;
46}