1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//! Middleware that provides a buffered mpsc channel to a service.
//!
//! Sometimes you want to give out multiple handles to a single service, and allow each handle to
//! enqueue requests. That is, you want a [`Service`] to be [`Clone`]. This module allows you to do
//! that by placing the service behind a multi-producer, single-consumer buffering channel. Clients
//! enqueue requests by sending on the channel from any of the handles ([`Buffer`]), and the single
//! service running elsewhere (usually spawned) receives and services the requests one by one. Each
//! request is enqueued alongside a response channel that allows the service to report the result
//! of the request back to the caller.
//!
//! # Examples
//!
//! ```rust
//! # #[cfg(feature = "util")]
//! use tower::buffer::Buffer;
//! # #[cfg(feature = "util")]
//! use tower::{Service, ServiceExt};
//! # #[cfg(feature = "util")]
//! async fn mass_produce<S: Service<usize>>(svc: S)
//! where
//! S: 'static + Send,
//! S::Error: Send + Sync + std::error::Error,
//! S::Future: Send
//! {
//! let svc = Buffer::new(svc, 10 /* buffer length */);
//! for _ in 0..10 {
//! let mut svc = svc.clone();
//! tokio::spawn(async move {
//! for i in 0usize.. {
//! svc.ready().await.expect("service crashed").call(i).await;
//! }
//! });
//! }
//! }
//! ```
//!
//! [`Service`]: crate::Service
pub mod error;
pub mod future;
mod layer;
mod message;
mod service;
mod worker;
pub use self::layer::BufferLayer;
pub use self::service::Buffer;