#rpc #async #network #microservices #api

srpc

simple and extremely performant RPC server built on top of Canary

8 releases

0.1.6 Jan 25, 2022
0.1.4 Jan 23, 2022
0.1.0 Dec 30, 2021
0.0.3 Dec 29, 2021

#23 in #rpc-library

47 downloads per month
Used in saildb

MIT license

7KB
92 lines

SRPC

SRPC is a simple RPC system built on top of Canary, designed to be as ergonomic and zero-cost as possible.

A simple example of an SRPC service

#[srpc::rpc] // other options include rpc(mutex), rpc(none)
#[derive(Default)]
struct DistributedList<T> {
    list: Vec<T>
}

#[srpc::rpc]
impl<T: Clone> DistributedList<T> {
    async fn push(&mut self, value: T) {
        self.list.push(value);
    }
    async fn get(&self, index: usize) -> Option<T> {
        self.list.get(index).and_then(|val| Some(val.clone()))
    }
    async fn remove(&mut self, index: usize) -> T {
        self.list.remove(index)
    }
}
// database
async fn main() -> Result<()> {
    GLOBAL_ROUTE.add_service_at::<DistributedList<i32>>("list", Arc::new(RwLock::new(Default::default())))?;
    let addr = "tcp@127.0.0.1:8080".parse::<Addr>()?;
    // listen in the following address
    addr.bind().await?;
    std::future::pending().await
}

// peer
async fn main() -> Result<()> {
    let addr = "list://tcp@127.0.0.1:8080".parse::<ServiceAddr>()?;
    let connection = addr.connect().await?;
    let mut db = connection.client::<DistributedList<i32>>();
    db.push(1).await?;
    db.push(5).await?;
    db.push(6).await?;

    let value = db.get(1).await?;
    println!("{:?}", value); // index 1 == 5
    Ok(())
}

Dependencies

~13–27MB
~405K SLoC