Available on crate feature buffer only.
Expand description

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

use tower::buffer::Buffer;
use tower::{Service, ServiceExt};
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;
            }
        });
    }
}

Modules

Error types for the Buffer middleware.

Future types for the Buffer middleware.

Structs

Adds an mpsc buffer in front of an inner service.

Adds an mpsc buffer in front of an inner service.