From 3e61061a5fce01edd5f75f079104a8ab68afc865 Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Thu, 3 Jul 2025 20:20:27 +0300 Subject: [PATCH 1/4] blog post about irpc --- src/app/blog/irpc/page.mdx | 257 +++++++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 src/app/blog/irpc/page.mdx diff --git a/src/app/blog/irpc/page.mdx b/src/app/blog/irpc/page.mdx new file mode 100644 index 0000000..4fc134a --- /dev/null +++ b/src/app/blog/irpc/page.mdx @@ -0,0 +1,257 @@ +import { BlogPostLayout } from '@/components/BlogPostLayout' +import {ThemeImage} from '@/components/ThemeImage' + +export const post = { + draft: false, + author: 'Rüdiger Klaehn', + date: '2025-07-04', + title: 'IRPC', + description: + "A lightweight rpc crate for iroh protocols", +} + +export const metadata = { + title: post.title, + description: post.description, + openGraph: { + title: post.title, + description: post.description, + images: [{ + url: `/api/og?title=Blog&subtitle=${post.title}`, + width: 1200, + height: 630, + alt: post.title, + type: 'image/png', + }], + type: 'article' + } +} + +export default (props) => + +# IRPC - a lightweight rpc crate for iroh connections + +When writing async rust code such as iroh protocols, you will frequently use message passing to communicate between independent parts of your code. + +You will start by defining a message enum that contains the different requests your task is supposed to handle, and then write a loop inside the handler task, like a very primitive version of an actor. + +Let's do a simple example, an async key value store, with just Set and Get. + +```rust +enum Request { + Set { + key: String, + value: String, + response: oneshot::Sender<()>, + } + Get { + key: String, + response: oneshot::Sender>, + } +} +``` + +Your "client" then is a tokio `mpsc::Sender` or a small wrapper around it that makes it more convenient to use. And your server is a task that contains a handler loop. + +Calling such a service is quite cumbersone, e.g. calling Get: + +```rust +let (tx, rx) = oneshot::channel(); +client.send(Command::Get { key: "a".to_string(), response: tx }).await?; +let res = rx.await?; +``` + +So you will usually write a client struct that is a newtype wrapper around the mpsc Sender to add some syntax candy: + +```rust +struct Client(mpsc::Sender); +impl Client { + ... + async fn get(&self, key: String) -> Result> { + let (tx, rx) = oneshot::channel(); + self.0.send(Request::Get { key, response: tx }).await?; + Ok(rx.await??) + } + ... +} +``` + +If you want to have some more complex requests, no problem. E.g. here is how a request would look like to add an entry from a stream: + +```rust +enum Request { + ... + SetFromStrean { + key: String, + value: mpsc::Receiver, + response: oneshot::Sender<()>, + } + ... +} +``` + +Or a request that gets a value as a stream: + +```rust +enum Request { + ... + GetAsStream { + key: String, + response: mpsc::Sender>, + } + ... +} +``` + +You already have an async boundary and a message passing based protocol, so it seems like it would be easy to also use this protocol across a process boundary. But you still want to retain the ability to use it in-process with zero overhead. + +To cross a process boundary, the commands have to be serializable. But the response or update channels are not. We need to separate the message itself and the update and response channels. + +At this point things start to get quite verbose: + +``` +#[derive(Serialize, Deserialize)] +struct GetRequest { + key: String, +} + +#[derive(Serialize, Deserialize)] +struct SetRequest { + key: String, + value: String, +} + +/// the serializable request. This is what the remote side reads first to know what to do +#[derive(Serialize, Deserialize)] +enum Request { + Get(GetRequest), + Set(SetRequest), +} + +/// the full request. This is what is used in-process. +enum RequestWithChannels { + Get { request: GetRequest, response: oneshot::Sender }, + Set { request: SetRequest, response: oneshot::Sender<()> }, +} + +impl From for Request { ... } +``` + +How does the actual cross process communication look like, for example for get? Let's use postcard for serialization/deserialization: + +```rust +async fn get_remote(connection: Connection, key: String) -> Result> { + let (send, recv) = connection.open_bi().await?; + send.write_all(postcard::to_stdvec(GetRequest { key })?).await?; + let res = recv.read_to_end(1024).await?; + let res = postcard::from_bytes(&res)?; + Ok(res) +} +``` + +The server side looks similar. We read a `Request` from an incoming connection, then based on the enum case decide which request we need to handle: + +``` +async fn server(connection: Connection, store: BTreeMap) -> Result<()> { + while let Ok((send, recv)) = connection.accept_bi().await { + let request = recv.read_to_end(1024).await?; + let request: Request = postcard::from_bytes(&request)?; + match request { + Request::Get(GetRequest { key }) => { + let response = store.get(key); + let response = postcard::to_stdvec(&response)?; + send.write_all(&response).await?; + send.finish(); + } + ... + } + + } +} +``` + +This works well for simple requests where there is no update channel and just a single response. But we also want to support requests with updates like `SetFromStrean` and requests with stream responses like `GetAsStream`. + +To support this efficiently, it is best to length prefix both the initial request, subsequent updates, and responses. Even if a `Request` "knows" its own size, deserializing from an async stream is very inefficient. + +Since we are using postcard for ser/de, and messages will very frequently be small, we have decided to use postcard varints as length prefixes. + + +Now we have a protocol that supports different rpc types (rpc, client streaming, server streaming, bidi streaming) and that can be used both locally (via the `FullRequest` enum) and remotely. + +But we said that we wanted to be able to seamlessly switch between remote or local. So let's do that (length prefixes omitted): + +```rust +enum Client { + Local(mpsc::Sender), + Remote(quinn::Connection), +} + +impl Client { + async fn get(&self, key: String) -> Result> { + let request = GetRequest { key }; + match self { + Self::Local(chan) => { + let (tx, rx) = oneshot::channel(); + let request = FullRequest { request, response: tx }; + chan.send(request).await?; + Ok(rx.await??) + } + Self::Remote(conn) => { + let (send, recv) = connection.open_bi().await?; + send.write_all(postcard::to_stdvec(request)?).await?; + let res = recv.read_to_end(1024).await?; + let res = postcard::from_bytes(&res)?; + Ok(res) + } + } + } +} +``` + +This is all pretty straightforward code, but very tedious to write, especially for a large and complex protocol. + +There is some work that we can't avoid. We have to define the different request types. We have to specify for each request type if there is no response, a single resposne, or a stream of responses. We also have to specify if there is a stream of updates, and make sure that all these types (requests, updates and responses) are serializable, which can sometimes be a pain when it comes to error types. + +But what about all this boilerplate? +- Defining the two different enums for a serializable request and a full request including channels +- Implementing a client with async fns for each request type +- Implementing a server that reads messages and dispatches on them +- serializing and deserializing using postcard with length prefixes + +The irpc crate is meant solely to reduce the tedious boilerplate involved in writing the above manually. + +It does *not* abstract over the connection type - it only supports [iroh-quinn] send- and receive streams out of the box, so the only two possible connection types are iroh p2p QUIC connections and normal QUIC connections. It also does not abstract over the local channel type - a local channel is always a tokio mpsc channel. Serialization is always postcard, and length prefixes are always postcard varints. + +So let's take a look how the kv service looks using irpc: + +The service definition contains just what is absolutely needed. For each request type we have to define what the response item type is (in this case `String` or `()`), and what the response channel type is (none, oneshot or mpsc). + +The rpc_requests macro will store this information and also create the `RequestWithChannels` enum that adds the appropriate channels for each request type. It will also generate a number of `From`-conversions to make working with the requests more pleasant. + +```rust +struct KvService {} +impl Service for KvStoreService {} + +#[rpc_requests(KvService, message = RequestWithChannels)] +#[derive(Serialize, Deserialize)] +enum Request { + #[rpc(tx=oneshot::Sender)] + Get(GetRequest), + #[rpc(tx=oneshot::Sender<()>)] + Put(PutRequest), +} +``` + +Now let's look at the client: + +```rust +struct Client(irpc::Client); +impl Client { + fn get(&self, key: String) -> Result> { + Ok(self.0.rpc(GetRequest { key }).await?) + } +} +``` + +The fn `rpc` on irpc::Client will only be available for messages where the update channel is not set and the response channel is an oneshot channel, so you will get compile errors if you try to use a request in the wrong way. \ No newline at end of file From 06fc73554bd088ccd9df96cae9816dd9691d66ad Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Thu, 3 Jul 2025 20:32:15 +0300 Subject: [PATCH 2/4] Fix code examples --- src/app/blog/irpc/page.mdx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/blog/irpc/page.mdx b/src/app/blog/irpc/page.mdx index 4fc134a..b89a23b 100644 --- a/src/app/blog/irpc/page.mdx +++ b/src/app/blog/irpc/page.mdx @@ -109,7 +109,7 @@ To cross a process boundary, the commands have to be serializable. But the respo At this point things start to get quite verbose: -``` +```rust #[derive(Serialize, Deserialize)] struct GetRequest { key: String, @@ -128,7 +128,7 @@ enum Request { Set(SetRequest), } -/// the full request. This is what is used in-process. +/// the full request including response channels. This is what is used in-process. enum RequestWithChannels { Get { request: GetRequest, response: oneshot::Sender }, Set { request: SetRequest, response: oneshot::Sender<()> }, @@ -151,7 +151,7 @@ async fn get_remote(connection: Connection, key: String) -> Result) -> Result<()> { while let Ok((send, recv)) = connection.accept_bi().await { let request = recv.read_to_end(1024).await?; @@ -174,8 +174,7 @@ This works well for simple requests where there is no update channel and just a To support this efficiently, it is best to length prefix both the initial request, subsequent updates, and responses. Even if a `Request` "knows" its own size, deserializing from an async stream is very inefficient. -Since we are using postcard for ser/de, and messages will very frequently be small, we have decided to use postcard varints as length prefixes. - +Since we are using postcard for ser/de, and messages will very frequently be small, we have decided to use postcard varints as length prefixes. Now we have a protocol that supports different rpc types (rpc, client streaming, server streaming, bidi streaming) and that can be used both locally (via the `FullRequest` enum) and remotely. From d11b4fb20666ff5a8144010dcd11dda0fed99c38 Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Thu, 3 Jul 2025 20:33:53 +0300 Subject: [PATCH 3/4] spelling --- src/app/blog/irpc/page.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/blog/irpc/page.mdx b/src/app/blog/irpc/page.mdx index b89a23b..b82e27c 100644 --- a/src/app/blog/irpc/page.mdx +++ b/src/app/blog/irpc/page.mdx @@ -53,7 +53,7 @@ enum Request { Your "client" then is a tokio `mpsc::Sender` or a small wrapper around it that makes it more convenient to use. And your server is a task that contains a handler loop. -Calling such a service is quite cumbersone, e.g. calling Get: +Calling such a service is quite cumbersome, e.g. calling Get: ```rust let (tx, rx) = oneshot::channel(); From b709a7ea81ac401e8efcc404b5c4c3cbae8bb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cramfox=E2=80=9D?= <“kasey@n0.computer”> Date: Thu, 3 Jul 2025 21:37:13 -0400 Subject: [PATCH 4/4] small nits and added a conclusion --- src/app/blog/irpc/page.mdx | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/app/blog/irpc/page.mdx b/src/app/blog/irpc/page.mdx index b82e27c..dda709a 100644 --- a/src/app/blog/irpc/page.mdx +++ b/src/app/blog/irpc/page.mdx @@ -31,11 +31,11 @@ export default (props) => # IRPC - a lightweight rpc crate for iroh connections -When writing async rust code such as iroh protocols, you will frequently use message passing to communicate between independent parts of your code. +When writing async rust code, like you do when writing iroh protocols, you will frequently use message passing to communicate between independent parts of your code. You will start by defining a message enum that contains the different requests your task is supposed to handle, and then write a loop inside the handler task, like a very primitive version of an actor. -Let's do a simple example, an async key value store, with just Set and Get. +Let's do a simple example, an async key-value store, with just `Set` and `Get` requests. ```rust enum Request { @@ -53,7 +53,7 @@ enum Request { Your "client" then is a tokio `mpsc::Sender` or a small wrapper around it that makes it more convenient to use. And your server is a task that contains a handler loop. -Calling such a service is quite cumbersome, e.g. calling Get: +Calling such a service is quite cumbersome. For example, here's what it takes to call `Get`: ```rust let (tx, rx) = oneshot::channel(); @@ -61,7 +61,7 @@ client.send(Command::Get { key: "a".to_string(), response: tx }).await?; let res = rx.await?; ``` -So you will usually write a client struct that is a newtype wrapper around the mpsc Sender to add some syntax candy: +So you will usually write a client struct that is a newtype wrapper around the `mpsc::Sender` to add some syntax candy: ```rust struct Client(mpsc::Sender); @@ -76,7 +76,7 @@ impl Client { } ``` -If you want to have some more complex requests, no problem. E.g. here is how a request would look like to add an entry from a stream: +If you want to have some more complex requests, no problem. Here is what a request that adds and entry from a stream would look like: ```rust enum Request { @@ -103,7 +103,7 @@ enum Request { } ``` -You already have an async boundary and a message passing based protocol, so it seems like it would be easy to also use this protocol across a process boundary. But you still want to retain the ability to use it in-process with zero overhead. +And since you already have an async boundary and a message passing based protocol, it seems like it would be easy to also use this protocol across a process boundary. But you still want to retain the ability to use it in-process with zero overhead. To cross a process boundary, the commands have to be serializable. But the response or update channels are not. We need to separate the message itself and the update and response channels. @@ -137,7 +137,7 @@ enum RequestWithChannels { impl From for Request { ... } ``` -How does the actual cross process communication look like, for example for get? Let's use postcard for serialization/deserialization: +What does the actual cross-process communication look like? Let's take a look at a `Get` example, using postcard for serialization/deserialization: ```rust async fn get_remote(connection: Connection, key: String) -> Result> { @@ -149,7 +149,7 @@ async fn get_remote(connection: Connection, key: String) -> Result) -> Result<()> { @@ -210,7 +210,7 @@ impl Client { This is all pretty straightforward code, but very tedious to write, especially for a large and complex protocol. -There is some work that we can't avoid. We have to define the different request types. We have to specify for each request type if there is no response, a single resposne, or a stream of responses. We also have to specify if there is a stream of updates, and make sure that all these types (requests, updates and responses) are serializable, which can sometimes be a pain when it comes to error types. +There is some work that we can't avoid. We have to define the different request types. We have to specify for each request type the kind of response we expect (no response, a single response, or a stream of responses). We also have to specify if there is a stream of updates and make sure that all these types (requests, updates and responses) are serializable, which can sometimes be a pain when it comes to error types. But what about all this boilerplate? - Defining the two different enums for a serializable request and a full request including channels @@ -218,15 +218,15 @@ But what about all this boilerplate? - Implementing a server that reads messages and dispatches on them - serializing and deserializing using postcard with length prefixes -The irpc crate is meant solely to reduce the tedious boilerplate involved in writing the above manually. +**The `irpc` crate is meant solely to reduce the tedious boilerplate involved in writing the above manually.** -It does *not* abstract over the connection type - it only supports [iroh-quinn] send- and receive streams out of the box, so the only two possible connection types are iroh p2p QUIC connections and normal QUIC connections. It also does not abstract over the local channel type - a local channel is always a tokio mpsc channel. Serialization is always postcard, and length prefixes are always postcard varints. +It does *not* abstract over the connection type - it only supports [iroh-quinn] send and receive streams out of the box, so the only two possible connection types are `iroh` p2p QUIC connections and normal QUIC connections. It also does not abstract over the local channel type - a local channel is always a `tokio::sync::mpsc` channel. Serialization is always using postcard and length prefixes are always postcard varints. -So let's take a look how the kv service looks using irpc: +So let's see what our kv service looks using `irpc`: The service definition contains just what is absolutely needed. For each request type we have to define what the response item type is (in this case `String` or `()`), and what the response channel type is (none, oneshot or mpsc). -The rpc_requests macro will store this information and also create the `RequestWithChannels` enum that adds the appropriate channels for each request type. It will also generate a number of `From`-conversions to make working with the requests more pleasant. +The `rpc_requests` macro will store this information and also create the `RequestWithChannels` enum that adds the appropriate channels for each request type. It will also generate a number of `From`-conversions to make working with the requests more pleasant. ```rust struct KvService {} @@ -253,4 +253,9 @@ impl Client { } ``` -The fn `rpc` on irpc::Client will only be available for messages where the update channel is not set and the response channel is an oneshot channel, so you will get compile errors if you try to use a request in the wrong way. \ No newline at end of file +The `rpc` method on `irpc::Client` will only be available for messages where the update channel is not set and the response channel is an oneshot channel, so you will get compile errors if you try to use a request in the wrong way. + +## Try it out +If you are writing an `iroh` protocol and have run into the same tedious boiler plate issues around RPC as we have, give `irpc` a shot. We've spent a lot of time iterating on this issue, in fact this is the second crate we've published that takes a stable at easing the RPC burden. Take a look at the (no longer maintained) [`quic-rpc`](https://github.com/n0-computer/quic-rpc) if you are curious. + +Because of this extensive experience, we are confident that `irpc` is a good solution for doing both in-process, cross-process, and cross-machine RPC, especially if you are building an `iroh` protocol. Check it out and you will see why we at number0 use it for all of the `iroh` protocols that we have created and maintained.