diff --git a/examples/custom-protocol.rs b/examples/custom-protocol.rs index a5127e2dd..41dfc1cc0 100644 --- a/examples/custom-protocol.rs +++ b/examples/custom-protocol.rs @@ -91,7 +91,7 @@ async fn main() -> Result<()> { let local_pool = LocalPool::default(); let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint()); let builder = builder.accept(iroh_blobs::ALPN, blobs.clone()); - let blobs_client = blobs.client(); + let blobs_client = blobs.spawn_rpc(); // Build our custom protocol handler. The `builder` exposes access to various subsystems in the // iroh node. In our case, we need a blobs client and the endpoint. diff --git a/examples/hello-world-fetch.rs b/examples/hello-world-fetch.rs index 0741a5cd9..7806366e0 100644 --- a/examples/hello-world-fetch.rs +++ b/examples/hello-world-fetch.rs @@ -42,7 +42,7 @@ async fn main() -> Result<()> { let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint()); let builder = builder.accept(iroh_blobs::ALPN, blobs.clone()); let node = builder.spawn().await?; - let blobs_client = blobs.client(); + let blobs_client = blobs.spawn_rpc(); println!("fetching hash: {}", ticket.hash()); println!("node id: {}", node.endpoint().node_id()); diff --git a/examples/hello-world-provide.rs b/examples/hello-world-provide.rs index 96fa028c2..10d2aac9a 100644 --- a/examples/hello-world-provide.rs +++ b/examples/hello-world-provide.rs @@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> { let local_pool = LocalPool::default(); let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint()); let builder = builder.accept(iroh_blobs::ALPN, blobs.clone()); - let blobs_client = blobs.client(); + let blobs_client = blobs.spawn_rpc(); let node = builder.spawn().await?; // add some data and remember the hash diff --git a/examples/local-swarm-discovery.rs b/examples/local-swarm-discovery.rs index fd84a8f56..9cad60bf3 100644 --- a/examples/local-swarm-discovery.rs +++ b/examples/local-swarm-discovery.rs @@ -78,7 +78,7 @@ async fn main() -> anyhow::Result<()> { let blobs = Blobs::memory().build(local_pool.handle(), builder.endpoint()); let builder = builder.accept(iroh_blobs::ALPN, blobs.clone()); let node = builder.spawn().await?; - let blobs_client = blobs.client(); + let blobs_client = blobs.spawn_rpc(); match &cli.command { Commands::Accept { path } => { diff --git a/examples/transfer.rs b/examples/transfer.rs index 4e73909ea..1729f5c7b 100644 --- a/examples/transfer.rs +++ b/examples/transfer.rs @@ -26,7 +26,7 @@ async fn main() -> Result<()> { .spawn() .await?; - let blobs = blobs.client(); + let blobs = blobs.spawn_rpc(); let args = std::env::args().collect::>(); match &args.iter().map(String::as_str).collect::>()[..] { diff --git a/src/net_protocol.rs b/src/net_protocol.rs index c02a19acc..5dab29d5b 100644 --- a/src/net_protocol.rs +++ b/src/net_protocol.rs @@ -47,18 +47,21 @@ impl Default for GcState { } } -#[derive(Debug, Clone)] -pub struct Blobs { +#[derive(Debug)] +struct BlobsInner { rt: LocalPoolHandle, pub(crate) store: S, events: EventSender, downloader: Downloader, - #[cfg(feature = "rpc")] - batches: Arc>, endpoint: Endpoint, - gc_state: Arc>, + gc_state: std::sync::Mutex, #[cfg(feature = "rpc")] - pub(crate) rpc_handler: Arc>, + batches: tokio::sync::Mutex, +} + +#[derive(Debug, Clone)] +pub struct Blobs { + inner: Arc>, } /// Keeps track of all the currently active batch operations of the blobs api. @@ -178,40 +181,44 @@ impl Blobs { endpoint: Endpoint, ) -> Self { Self { - rt, - store, - events, - downloader, - endpoint, - #[cfg(feature = "rpc")] - batches: Default::default(), - gc_state: Default::default(), - #[cfg(feature = "rpc")] - rpc_handler: Default::default(), + inner: Arc::new(BlobsInner { + rt, + store, + events, + downloader, + endpoint, + #[cfg(feature = "rpc")] + batches: Default::default(), + gc_state: Default::default(), + }), } } pub fn store(&self) -> &S { - &self.store + &self.inner.store + } + + pub fn events(&self) -> &EventSender { + &self.inner.events } pub fn rt(&self) -> &LocalPoolHandle { - &self.rt + &self.inner.rt } pub fn downloader(&self) -> &Downloader { - &self.downloader + &self.inner.downloader } pub fn endpoint(&self) -> &Endpoint { - &self.endpoint + &self.inner.endpoint } /// Add a callback that will be called before the garbage collector runs. /// /// This can only be called before the garbage collector has started, otherwise it will return an error. pub fn add_protected(&self, cb: ProtectCb) -> Result<()> { - let mut state = self.gc_state.lock().unwrap(); + let mut state = self.inner.gc_state.lock().unwrap(); match &mut *state { GcState::Initial(cbs) => { cbs.push(cb); @@ -225,7 +232,7 @@ impl Blobs { /// Start garbage collection with the given settings. pub fn start_gc(&self, config: GcConfig) -> Result<()> { - let mut state = self.gc_state.lock().unwrap(); + let mut state = self.inner.gc_state.lock().unwrap(); let protected = match state.deref_mut() { GcState::Initial(items) => std::mem::take(items), GcState::Started(_) => bail!("gc already started"), @@ -241,9 +248,9 @@ impl Blobs { set } }; - let store = self.store.clone(); + let store = self.store().clone(); let run = self - .rt + .rt() .spawn(move || async move { store.gc_run(config, protected_cb).await }); *state = GcState::Started(Some(run)); Ok(()) @@ -251,7 +258,7 @@ impl Blobs { #[cfg(feature = "rpc")] pub(crate) async fn batches(&self) -> tokio::sync::MutexGuard<'_, BlobBatches> { - self.batches.lock().await + self.inner.batches.lock().await } pub(crate) async fn download( @@ -268,7 +275,7 @@ impl Blobs { mode, } = req; let hash_and_format = HashAndFormat { hash, format }; - let temp_tag = self.store.temp_tag(hash_and_format); + let temp_tag = self.store().temp_tag(hash_and_format); let stats = match mode { DownloadMode::Queued => { self.download_queued(endpoint, hash_and_format, nodes, progress.clone()) @@ -283,10 +290,10 @@ impl Blobs { progress.send(DownloadProgress::AllDone(stats)).await.ok(); match tag { SetTagOption::Named(tag) => { - self.store.set_tag(tag, Some(hash_and_format)).await?; + self.store().set_tag(tag, Some(hash_and_format)).await?; } SetTagOption::Auto => { - self.store.create_tag(hash_and_format).await?; + self.store().create_tag(hash_and_format).await?; } } drop(temp_tag); @@ -316,7 +323,7 @@ impl Blobs { let can_download = !node_ids.is_empty() && (any_added || endpoint.discovery().is_some()); anyhow::ensure!(can_download, "no way to reach a node for download"); let req = DownloadRequest::new(hash_and_format, node_ids).progress_sender(progress); - let handle = self.downloader.queue(req).await; + let handle = self.downloader().queue(req).await; let stats = handle.await?; Ok(stats) } @@ -334,7 +341,7 @@ impl Blobs { let mut nodes_iter = nodes.into_iter(); 'outer: loop { match crate::get::db::get_to_db_in_steps( - self.store.clone(), + self.store().clone(), hash_and_format, progress.clone(), ) @@ -393,9 +400,9 @@ impl Blobs { impl ProtocolHandler for Blobs { fn accept(&self, conn: Connecting) -> BoxedFuture> { - let db = self.store.clone(); - let events = self.events.clone(); - let rt = self.rt.clone(); + let db = self.store().clone(); + let events = self.events().clone(); + let rt = self.rt().clone(); Box::pin(async move { crate::provider::handle_connection(conn.await?, db, events, rt).await; @@ -404,7 +411,7 @@ impl ProtocolHandler for Blobs { } fn shutdown(&self) -> BoxedFuture<()> { - let store = self.store.clone(); + let store = self.store().clone(); Box::pin(async move { store.shutdown().await; }) diff --git a/src/rpc.rs b/src/rpc.rs index 6f5ee8ba3..33bb57898 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -2,12 +2,13 @@ use std::{ io, + ops::Deref, sync::{Arc, Mutex}, }; use anyhow::anyhow; use client::{ - blobs::{self, BlobInfo, BlobStatus, IncompleteBlobInfo, WrapOption}, + blobs::{BlobInfo, BlobStatus, IncompleteBlobInfo, MemClient, WrapOption}, tags::TagInfo, MemConnector, }; @@ -61,14 +62,10 @@ const RPC_BLOB_GET_CHUNK_SIZE: usize = 1024 * 64; const RPC_BLOB_GET_CHANNEL_CAP: usize = 2; impl Blobs { - /// Get a client for the blobs protocol - pub fn client(&self) -> blobs::MemClient { - let client = self - .rpc_handler - .get_or_init(|| RpcHandler::new(self)) - .client - .clone(); - blobs::Client::new(client) + /// Spawns an in-memory RPC client and server pair. + #[must_use = "Dropping the RpcHandler will stop the client"] + pub fn spawn_rpc(&self) -> RpcHandler { + RpcHandler::new(self) } /// Handle an RPC request @@ -874,22 +871,44 @@ impl Blobs { } } +/// An in memory rpc handler for the blobs rpc protocol +/// +/// This struct contains both a task that handles rpc requests and a client +/// that can be used to send rpc requests. +/// +/// Dropping it will stop the handler task, so you need to put it somewhere +/// where it will be kept alive. This struct will capture a copy of +/// [`crate::net_protocol::Blobs`] and keep it alive. #[derive(Debug)] -pub(crate) struct RpcHandler { +pub struct RpcHandler { /// Client to hand out - client: RpcClient, + client: MemClient, /// Handler task _handler: AbortOnDropHandle<()>, } +impl Deref for RpcHandler { + type Target = MemClient; + + fn deref(&self) -> &Self::Target { + &self.client + } +} + impl RpcHandler { fn new(blobs: &Blobs) -> Self { let blobs = blobs.clone(); let (listener, connector) = quic_rpc::transport::flume::channel(1); let listener = RpcServer::new(listener); let client = RpcClient::new(connector); + let client = MemClient::new(client); let _handler = listener .spawn_accept_loop(move |req, chan| blobs.clone().handle_rpc_request(req, chan)); Self { client, _handler } } + + /// Get a reference to the rpc client api + pub fn client(&self) -> &MemClient { + &self.client + } } diff --git a/tests/blobs.rs b/tests/blobs.rs index ad1198f92..8a9f41468 100644 --- a/tests/blobs.rs +++ b/tests/blobs.rs @@ -13,7 +13,7 @@ async fn blobs_gc_smoke() -> TestResult<()> { let pool = LocalPool::default(); let endpoint = Endpoint::builder().bind().await?; let blobs = Blobs::memory().build(pool.handle(), &endpoint); - let client = blobs.clone().client(); + let client = blobs.spawn_rpc(); blobs.start_gc(GcConfig { period: Duration::from_millis(1), done_callback: None, @@ -32,12 +32,7 @@ async fn blobs_gc_protected() -> TestResult<()> { let pool = LocalPool::default(); let endpoint = Endpoint::builder().bind().await?; let blobs = Blobs::memory().build(pool.handle(), &endpoint); - let client: iroh_blobs::rpc::client::blobs::Client< - quic_rpc::transport::flume::FlumeConnector< - iroh_blobs::rpc::proto::Response, - iroh_blobs::rpc::proto::Request, - >, - > = blobs.clone().client(); + let client = blobs.spawn_rpc(); let h1 = client.add_bytes(b"test".to_vec()).await?; let protected = Arc::new(Mutex::new(Vec::new())); blobs.add_protected(Box::new({ diff --git a/tests/gc.rs b/tests/gc.rs index a703ce5d2..e799febbe 100644 --- a/tests/gc.rs +++ b/tests/gc.rs @@ -20,7 +20,7 @@ use iroh::{protocol::Router, Endpoint, NodeAddr, NodeId}; use iroh_blobs::{ hashseq::HashSeq, net_protocol::Blobs, - rpc::client::{blobs, tags}, + rpc::{client::tags, RpcHandler}, store::{ bao_tree, BaoBatchWriter, ConsistencyCheckProgress, EntryStatus, GcConfig, MapEntryMut, MapMut, ReportLevel, Store, @@ -66,8 +66,8 @@ impl Node { } /// Returns an in-memory blobs client - pub fn blobs(&self) -> blobs::MemClient { - self.blobs.clone().client() + pub fn blobs(&self) -> RpcHandler { + self.blobs.spawn_rpc() } /// Returns an in-memory tags client