Part 4. Add a Middleware

Next, let’s look at how to add middleware to Volo.

For example, if we need a middleware that prints out the received requests, the returned responses and the elapsed time, we could write a Service in

pub struct LogService<S>(S);

impl<Cx, Req, S> volo::Service<Cx, Req> for LogService<S>
    Req: Send + 'static,
    S: Send + 'static + volo::Service<Cx, Req> + Sync,
    Cx: Send + 'static,
    async fn call(&self, cx: &mut Cx, req: Req) -> Result<S::Response, S::Error> {
        let now = std::time::Instant::now();
        let resp =, req).await;
        tracing::info!("Request took {}ms", now.elapsed().as_millis());

Then we wrap a Layer around the Service:

pub struct LogLayer;

impl<S> volo::Layer<S> for LogLayer {
    type Service = LogService<S>;

    fn layer(self, inner: S) -> Self::Service {

Finally, we add this Layer to client and server:

use volo_example::LogLayer;

static ref CLIENT: volo_gen::volo::example::ItemServiceClient = {
    let addr: SocketAddr = "".parse().unwrap();


At this point, it prints out how long the request took at the INFO log level.