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