Skip to content

feat: Add a lazily initialized in mem client for ffi etc. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ parking_lot = { version = "0.12.1", optional = true }
pin-project = "1.1.5"
portable-atomic = { version = "1", optional = true }
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"] }
quic-rpc = { version = "0.15.0", optional = true }
quic-rpc = { version = "0.15.1", optional = true }
quic-rpc-derive = { version = "0.15.0", optional = true }
quinn = { package = "iroh-quinn", version = "0.12", features = ["ring"] }
rand = "0.8"
Expand Down Expand Up @@ -131,3 +131,4 @@ iroh-router = { git = "https://github.com/n0-computer/iroh", branch = "main" }
iroh-net = { git = "https://github.com/n0-computer/iroh", branch = "main" }
iroh-metrics = { git = "https://github.com/n0-computer/iroh", branch = "main" }
iroh-base = { git = "https://github.com/n0-computer/iroh", branch = "main" }

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ license-files = [
[advisories]
ignore = [
"RUSTSEC-2024-0370", # unmaintained, no upgrade available
"RUSTSEC-2024-0384", # unmaintained, no upgrade available
]

[sources]
Expand Down
1 change: 0 additions & 1 deletion src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,6 @@ impl<G: Getter<Connection = D::Connection>, D: Dialer> Service<G, D> {
}

/// Handle receiving a [`Message`].
///
// This is called in the actor loop, and only async because subscribing to an existing transfer
// sends the initial state.
async fn handle_message(&mut self, msg: Message) {
Expand Down
9 changes: 8 additions & 1 deletion src/net_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// TODO: reduce API surface and add documentation
#![allow(missing_docs)]

use std::{collections::BTreeMap, sync::Arc};
use std::{
collections::BTreeMap,
sync::{Arc, OnceLock},
};

use anyhow::{anyhow, Result};
use futures_lite::future::Boxed as BoxedFuture;
Expand Down Expand Up @@ -36,6 +39,8 @@ pub struct Blobs<S> {
downloader: Downloader,
batches: tokio::sync::Mutex<BlobBatches>,
endpoint: Endpoint,
#[cfg(feature = "rpc")]
pub(crate) rpc_handler: Arc<OnceLock<crate::rpc::RpcHandler>>,
}

/// Name used for logging when new node addresses are added from gossip.
Expand Down Expand Up @@ -107,6 +112,8 @@ impl<S: crate::store::Store> Blobs<S> {
downloader,
endpoint,
batches: Default::default(),
#[cfg(feature = "rpc")]
rpc_handler: Arc::new(OnceLock::new()),
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
//! # use bao_tree::{ChunkNum, ChunkRanges};
//! # use iroh_blobs::protocol::{GetRequest, RangeSpecSeq};
//! # let hash: iroh_blobs::Hash = [0; 32].into();
//! let ranges = &ChunkRanges::from(..ChunkNum(10)) | &ChunkRanges::from(ChunkNum(100)..ChunkNum(110));
//! let ranges =
//! &ChunkRanges::from(..ChunkNum(10)) | &ChunkRanges::from(ChunkNum(100)..ChunkNum(110));
//! let spec = RangeSpecSeq::from_ranges([ranges]);
//! let request = GetRequest::new(hash, spec);
//! ```
Expand Down Expand Up @@ -236,8 +237,8 @@
//! # use iroh_blobs::protocol::{GetRequest, RangeSpecSeq};
//! # let hash: iroh_blobs::Hash = [0; 32].into();
//! let spec = RangeSpecSeq::from_ranges_infinite([
//! ChunkRanges::all(), // the collection itself
//! ChunkRanges::from(..ChunkNum(1)), // the first chunk of each child
//! ChunkRanges::all(), // the collection itself
//! ChunkRanges::from(..ChunkNum(1)), // the first chunk of each child
//! ]);
//! let request = GetRequest::new(hash, spec);
//! ```
Expand All @@ -252,9 +253,9 @@
//! # use iroh_blobs::protocol::{GetRequest, RangeSpecSeq};
//! # let hash: iroh_blobs::Hash = [0; 32].into();
//! let spec = RangeSpecSeq::from_ranges([
//! ChunkRanges::empty(), // we don't need the collection itself
//! ChunkRanges::empty(), // we don't need the first child either
//! ChunkRanges::all(), // we need the second child completely
//! ChunkRanges::empty(), // we don't need the collection itself
//! ChunkRanges::empty(), // we don't need the first child either
//! ChunkRanges::all(), // we need the second child completely
//! ]);
//! let request = GetRequest::new(hash, spec);
//! ```
Expand Down
39 changes: 37 additions & 2 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::{

use anyhow::anyhow;
use client::{
blobs::{BlobInfo, BlobStatus, IncompleteBlobInfo, WrapOption},
blobs::{self, BlobInfo, BlobStatus, IncompleteBlobInfo, WrapOption},
tags::TagInfo,
MemConnector,
};
use futures_buffered::BufferedStreamExt;
use futures_lite::StreamExt;
Expand All @@ -32,7 +33,11 @@ use proto::{
},
Request, RpcError, RpcResult, RpcService,
};
use quic_rpc::server::{ChannelTypes, RpcChannel, RpcServerError};
use quic_rpc::{
server::{ChannelTypes, RpcChannel, RpcServerError},
RpcClient, RpcServer,
};
use tokio_util::task::AbortOnDropHandle;

use crate::{
export::ExportProgress,
Expand All @@ -56,6 +61,16 @@ const RPC_BLOB_GET_CHUNK_SIZE: usize = 1024 * 64;
const RPC_BLOB_GET_CHANNEL_CAP: usize = 2;

impl<D: crate::store::Store> Blobs<D> {
/// Get a client for the blobs protocol
pub fn client(self: Arc<Self>) -> blobs::MemClient {
let client = self
.rpc_handler
.get_or_init(|| RpcHandler::new(&self))
.client
.clone();
blobs::Client::new(client)
}

/// Handle an RPC request
pub async fn handle_rpc_request<C>(
self: Arc<Self>,
Expand Down Expand Up @@ -871,3 +886,23 @@ impl<D: crate::store::Store> Blobs<D> {
Ok(CreateCollectionResponse { hash, tag })
}
}

#[derive(Debug)]
pub(crate) struct RpcHandler {
/// Client to hand out
client: RpcClient<RpcService, MemConnector>,
/// Handler task
_handler: AbortOnDropHandle<()>,
}

impl RpcHandler {
fn new<D: crate::store::Store>(blobs: &Arc<Blobs<D>>) -> 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 _handler = listener
.spawn_accept_loop(move |req, chan| blobs.clone().handle_rpc_request(req, chan));
Self { client, _handler }
}
}
5 changes: 5 additions & 0 deletions src/rpc/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! Iroh blobs and tags client
use anyhow::Result;
use futures_util::{Stream, StreamExt};
use quic_rpc::transport::flume::FlumeConnector;

pub mod blobs;
pub mod tags;

/// Type alias for a memory-backed client.
pub(crate) type MemConnector =
FlumeConnector<crate::rpc::proto::Response, crate::rpc::proto::Request>;

fn flatten<T, E1, E2>(
s: impl Stream<Item = Result<Result<T, E1>, E2>>,
) -> impl Stream<Item = Result<T>>
Expand Down
8 changes: 8 additions & 0 deletions src/rpc/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub struct Client<C = BoxedConnector<RpcService>> {
pub(super) rpc: RpcClient<RpcService, C>,
}

/// Type alias for a memory-backed client.
pub type MemClient = Client<crate::rpc::MemConnector>;

impl<C> Client<C>
where
C: Connector<RpcService>,
Expand All @@ -120,6 +123,11 @@ where
Self { rpc }
}

/// Get a tags client.
pub fn tags(&self) -> tags::Client<C> {
tags::Client::new(self.rpc.clone())
}

/// Check if a blob is completely stored on the node.
///
/// Note that this will return false for blobs that are partially stored on
Expand Down
1 change: 0 additions & 1 deletion src/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ pub struct PathContent {
}

/// Walks the directory to get the total size and number of files in directory or file
///
// TODO: possible combine with `scan_dir`
pub fn path_content_info(path: impl AsRef<Path>) -> anyhow::Result<PathContent> {
path_content_info0(path)
Expand Down
Loading