[−][src]Macro batched_fn::batched_fn
Macro for creating a batched function.
This macro has 3 parameters: handler
, config
, and
context
.
Parameters
handler
The handler must be in the form of a closure declaration that takes a batch and any number of references to objects in the context as input and returns a different type of batch.
config
Within the config you must specify the max_batch_size
and delay
.
The batched function will wait at most delay
milliseconds after receiving a single
input to fill a batch of size max_batch_size
. If enough inputs to fill a full batch
are not received within delay
milliseconds then the partial batch will be ran as-is.
context
Any additional reference that the handler takes as input must be defined within the context.
Examples
async fn double(x: i32) -> i32 { let batched_double = batched_fn! { handler = |batch: Vec<i32>| -> Vec<i32> { batch.into_iter().map(|x| x*2).collect() }; config = { max_batch_size: 4, delay: 50, }; context = {}; }; batched_double(x).await }
You can also provide an arbitrary number of additional arguments to the handler by reference.
All of the objects have to be initialized in the context
:
async fn multiply(x: i32) -> i32 { let batched_multiply = batched_fn! { handler = |batch: Vec<i32>, factor: &i32| -> Vec<i32> { batch.into_iter().map(|x| *factor * x ).collect() }; config = { max_batch_size: 4, delay: 50, }; context = { factor: 3, }; }; batched_multiply(x).await }