Skip to content

Commit eeddbaa

Browse files
committed
spawn_rpc should make it sufficiently clear that this is a thing you need to put away somewhere.
Or maybe spawn_client?
1 parent 09562ce commit eeddbaa

8 files changed

+22
-13
lines changed

examples/custom-protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async fn main() -> Result<()> {
9191
let local_pool = LocalPool::default();
9292
let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint());
9393
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
94-
let blobs_client = blobs.client();
94+
let blobs_client = blobs.spawn_rpc();
9595

9696
// Build our custom protocol handler. The `builder` exposes access to various subsystems in the
9797
// iroh node. In our case, we need a blobs client and the endpoint.

examples/hello-world-fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async fn main() -> Result<()> {
4242
let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint());
4343
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
4444
let node = builder.spawn().await?;
45-
let blobs_client = blobs.client();
45+
let blobs_client = blobs.spawn_rpc();
4646

4747
println!("fetching hash: {}", ticket.hash());
4848
println!("node id: {}", node.endpoint().node_id());

examples/hello-world-provide.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
2828
let local_pool = LocalPool::default();
2929
let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint());
3030
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
31-
let blobs_client = blobs.client();
31+
let blobs_client = blobs.spawn_rpc();
3232
let node = builder.spawn().await?;
3333

3434
// add some data and remember the hash

examples/local-swarm-discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async fn main() -> anyhow::Result<()> {
7878
let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint());
7979
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
8080
let node = builder.spawn().await?;
81-
let blobs_client = blobs.client();
81+
let blobs_client = blobs.spawn_rpc();
8282

8383
match &cli.command {
8484
Commands::Accept { path } => {

examples/transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn main() -> Result<()> {
2626
.spawn()
2727
.await?;
2828

29-
let blobs = blobs.client();
29+
let blobs = blobs.spawn_rpc();
3030

3131
let args = std::env::args().collect::<Vec<_>>();
3232
match &args.iter().map(String::as_str).collect::<Vec<_>>()[..] {

src/rpc.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ const RPC_BLOB_GET_CHUNK_SIZE: usize = 1024 * 64;
6262
const RPC_BLOB_GET_CHANNEL_CAP: usize = 2;
6363

6464
impl<D: crate::store::Store> Blobs<D> {
65-
/// Get a client for the blobs protocol
66-
pub fn client(&self) -> RpcHandler {
65+
/// Spawns an in-memory RPC client and server pair.
66+
#[must_use = "Dropping the RpcHandler will stop the client"]
67+
pub fn spawn_rpc(&self) -> RpcHandler {
6768
RpcHandler::new(self)
6869
}
6970

@@ -870,11 +871,14 @@ impl<D: crate::store::Store> Blobs<D> {
870871
}
871872
}
872873

873-
/// A rpc handler for the blobs rpc protocol
874+
/// An in memory rpc handler for the blobs rpc protocol
874875
///
875876
/// This struct contains both a task that handles rpc requests and a client
876-
/// that can be used to send rpc requests. Dropping it will stop the handler task,
877-
/// so you need to put it somewhere where it will be kept alive.
877+
/// that can be used to send rpc requests.
878+
///
879+
/// Dropping it will stop the handler task, so you need to put it somewhere
880+
/// where it will be kept alive. This struct will capture a copy of
881+
/// [`crate::net_protocol::Blobs`] and keep it alive.
878882
#[derive(Debug)]
879883
pub struct RpcHandler {
880884
/// Client to hand out
@@ -902,4 +906,9 @@ impl RpcHandler {
902906
.spawn_accept_loop(move |req, chan| blobs.clone().handle_rpc_request(req, chan));
903907
Self { client, _handler }
904908
}
909+
910+
/// Get a reference to the rpc client api
911+
pub fn client(&self) -> &MemClient {
912+
&self.client
913+
}
905914
}

tests/blobs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async fn blobs_gc_smoke() -> TestResult<()> {
1313
let pool = LocalPool::default();
1414
let endpoint = Endpoint::builder().bind().await?;
1515
let blobs = Blobs::memory().build(pool.handle(), &endpoint);
16-
let client = blobs.clone().client();
16+
let client = blobs.spawn_rpc();
1717
blobs.start_gc(GcConfig {
1818
period: Duration::from_millis(1),
1919
done_callback: None,
@@ -32,7 +32,7 @@ async fn blobs_gc_protected() -> TestResult<()> {
3232
let pool = LocalPool::default();
3333
let endpoint = Endpoint::builder().bind().await?;
3434
let blobs = Blobs::memory().build(pool.handle(), &endpoint);
35-
let client = blobs.clone().client();
35+
let client = blobs.spawn_rpc();
3636
let h1 = client.add_bytes(b"test".to_vec()).await?;
3737
let protected = Arc::new(Mutex::new(Vec::new()));
3838
blobs.add_protected(Box::new({

tests/gc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<S: Store> Node<S> {
6767

6868
/// Returns an in-memory blobs client
6969
pub fn blobs(&self) -> RpcHandler {
70-
self.blobs.client()
70+
self.blobs.spawn_rpc()
7171
}
7272

7373
/// Returns an in-memory tags client

0 commit comments

Comments
 (0)