tower_batch/lib.rs
1#![allow(clippy::type_complexity)]
2
3//! A Tower middleware that provides a buffered mpsc for processing requests in batches.
4//!
5//! Writing data in bulk is a common technique for improving the efficiency of certain tasks.
6//! `batch-tower` is a middleware that allows you to buffer requests for batch processing until
7//! the buffer reaches a maximum size OR a maximum duration elapses.
8//!
9//! Clients enqueue requests by sending on the channel from any of the handles ([`Batch`]), and the
10//! single service running elsewhere (usually spawned) receives and collects the requests wrapped
11//! in [`Item(R)`](BatchControl::Item). Once the [`Batch`]) buffer is full or the maximum duration
12//! elapses, the service is instructed to write the data with a [`Flush`](BatchControl::Flush)
13//! request. Upon completion of the flush operation, the client's will receive a response with the
14//! outcome.
15
16/// Export tower's alias for a type-erased error type.
17pub use tower::BoxError;
18
19pub use self::layer::BatchLayer;
20pub use self::service::Batch;
21
22pub mod error;
23pub mod future;
24mod layer;
25mod message;
26mod service;
27mod worker;
28
29/// Signaling mechanism for services that allow processing in batches.
30#[derive(Debug, Eq, PartialEq)]
31pub enum BatchControl<R> {
32 /// Collect a new batch item.
33 Item(R),
34
35 /// The current batch should be processed.
36 Flush,
37}
38
39impl<R> From<R> for BatchControl<R> {
40 fn from(req: R) -> BatchControl<R> {
41 BatchControl::Item(req)
42 }
43}