#async-io #service #io #future #non-blocking #async #error-response

motore

Motore is a library of modular and reusable components for building robust clients and servers. Motore is greatly inspired by Tower.

10 releases

0.4.1 Apr 3, 2024
0.4.0 Oct 23, 2023
0.3.3 Apr 13, 2023
0.3.2 Feb 15, 2023
0.0.0 Jun 7, 2022

#40 in Asynchronous

Download history 2100/week @ 2023-12-19 1577/week @ 2023-12-26 2234/week @ 2024-01-02 2078/week @ 2024-01-09 3098/week @ 2024-01-16 2691/week @ 2024-01-23 2543/week @ 2024-01-30 1114/week @ 2024-02-06 1122/week @ 2024-02-13 2060/week @ 2024-02-20 3188/week @ 2024-02-27 2430/week @ 2024-03-05 2562/week @ 2024-03-12 3335/week @ 2024-03-19 2894/week @ 2024-03-26 2424/week @ 2024-04-02

11,624 downloads per month
Used in 13 crates (5 directly)

MIT/Apache

57KB
1K SLoC

Motore

Crates.io Documentation License Build Status

Motore is an async middleware abstraction powered by AFIT and RPITIT.

Around Motore, we build modular and reusable components for building robust networking clients and servers.

Motore is greatly inspired by Tower.

Overview

Motore uses AFIT and RPITIT to reduce the mental burden of writing asynchronous code, especially to avoid the overhead of Box to make people less anxious.

The core abstraciton of Motore is the Service trait:

pub trait Service<Cx, Request> {
    /// Responses given by the service.
    type Response;
    /// Errors produced by the service.
    type Error;

    /// Process the request and return the response asynchronously.
    async fn call(&self, cx: &mut Cx, req: Request) -> Result<Self::Response, Self::Error>;
}

Getting Started

Combing AFIT and RPITIT together, we can write asynchronous code in a very concise and readable way.

pub struct Timeout<S> {
    inner: S,
    duration: Duration,
}

impl<Cx, Req, S> Service<Cx, Req> for Timeout<S>
where
    Req: 'static + Send,
    S: Service<Cx, Req> + 'static + Send + Sync,
    Cx: 'static + Send,
    S::Error: Send + Sync + Into<BoxError>,
{
    type Response = S::Response;

    type Error = BoxError;

    async fn call(&self, cx: &mut Cx, req: Req) -> Result<Self::Response, Self::Error> {
        let sleep = tokio::time::sleep(self.duration);
        tokio::select! {
            r = self.inner.call(cx, req) => {
                r.map_err(Into::into)
            },
            _ = sleep => Err(std::io::Error::new(std::io::ErrorKind::TimedOut, "service time out").into()),
        }
    }
}

We also provided the #[motore::service] macro to make writing a Serivce more async-native:

use motore::service;

pub struct S<I> {
    inner: I,
}

#[service]
impl<Cx, Req, I> Service<Cx, Req> for S<I>
where
   Req: Send + 'static,
   I: Service<Cx, Req> + Send + 'static + Sync,
   Cx: Send + 'static,
{
    async fn call(&self, cx: &mut Cx, req: Req) -> Result<I::Response, I::Error> {
        self.inner.call(cx, req).await
    }
}

FAQ

Where's the poll_ready(a.k.a. backpressure)?

https://www.cloudwego.io/zh/docs/motore/faq/q1_pull_ready/

License

Motore is dual-licensed under the MIT license and the Apache License (Version 2.0).

See LICENSE-MIT and LICENSE-APACHE for details.

Credits

We have used some third party components, and we thank them for their work.

For the full list, you may refer to the CREDITS.md file.

Community

Dependencies

~3.5–5.5MB
~96K SLoC