batched
Rust macro utility for batching expensive async operations.
Installation
Or add this to your Cargo.toml:
[]
= "0.1.4"
#[batched]
- window: Minimum amount of time (in milliseconds) the background thread waits before processing a batch.
- limit: Maximum amount of items that can be grouped and processed in a single batch.
- concurrent: Maximum amount of concurrent batched tasks running (default:
Infinity) - boxed: Automatically wraps the return type in an
Arc
The target function must have a single argument, a vector of items (Vec<T>). The return value is propagated to all async calls made for the batch items.
The target function return type must implement Clone to propagate the result. If it cannot implement Clone, use the boxed option to automatically wrap your return type in an Arc.
Prerequisites
- Built for async environments (tokio), will not work without a tokio async runtime
- Target function must have async, andthe function name should end with
_batched - Not supported inside structs
;
Examples
Simple add batch
async
async
Batch insert Postgres rows
use batched;
// Creates functions [`insert_message`] and [`insert_message_multiple`]
async
async
async