Skip to content
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

feat! Remove S type parameter #24

Closed
wants to merge 3 commits into from
Closed
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
220 changes: 134 additions & 86 deletions src/net_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use crate::{
Stats,
},
provider::EventSender,
store::GcConfig,
rpc::{client::blobs::MemClient, MemRpcHandler, RpcHandler},
store::{GcConfig, Store},
util::{
local_pool::{self, LocalPoolHandle},
progress::{AsyncChannelProgressSender, ProgressSender},
Expand Down Expand Up @@ -53,16 +54,16 @@ impl Default for GcState {
}

#[derive(Debug)]
pub struct Blobs<S> {
rt: LocalPoolHandle,
pub(crate) struct BlobsInner<S> {
pub(crate) rt: LocalPoolHandle,
pub(crate) store: S,
events: EventSender,
downloader: Downloader,
batches: tokio::sync::Mutex<BlobBatches>,
endpoint: Endpoint,
gc_state: Arc<std::sync::Mutex<GcState>>,
#[cfg(feature = "rpc")]
pub(crate) rpc_handler: Arc<OnceLock<crate::rpc::RpcHandler>>,
pub(crate) rpc_handler: Arc<OnceLock<crate::rpc::MemRpcHandler>>,
}

/// Name used for logging when new node addresses are added from gossip.
Expand Down Expand Up @@ -135,9 +136,21 @@ impl<S: crate::store::Store> Builder<S> {

/// Build the Blobs protocol handler.
/// You need to provide a local pool handle and an endpoint.
pub fn build(self, rt: &LocalPoolHandle, endpoint: &Endpoint) -> Arc<Blobs<S>> {
pub fn build(self, rt: &LocalPoolHandle, endpoint: &Endpoint) -> Arc<Blobs> {
let inner = self.build_inner(rt, endpoint);
Arc::new(Blobs { inner })
}

pub fn build_rpc_handler(self, rt: &LocalPoolHandle, endpoint: &Endpoint) -> RpcHandler<S> {
let inner = self.build_inner(rt, endpoint);
RpcHandler::from_blobs(inner)
}

/// Build the Blobs protocol handler.
/// You need to provide a local pool handle and an endpoint.
fn build_inner(self, rt: &LocalPoolHandle, endpoint: &Endpoint) -> Arc<BlobsInner<S>> {
let downloader = Downloader::new(self.store.clone(), endpoint.clone(), rt.clone());
Arc::new(Blobs::new(
Arc::new(BlobsInner::new(
self.store,
rt.clone(),
self.events.unwrap_or_default(),
Expand All @@ -147,24 +160,20 @@ impl<S: crate::store::Store> Builder<S> {
}
}

impl<S> Blobs<S> {
impl Blobs {
/// Create a new Blobs protocol handler builder, given a store.
pub fn builder(store: S) -> Builder<S> {
pub fn builder<S>(store: S) -> Builder<S> {
Builder {
store,
events: None,
}
}
}

impl Blobs<crate::store::mem::Store> {
/// Create a new memory-backed Blobs protocol handler.
pub fn memory() -> Builder<crate::store::mem::Store> {
Self::builder(crate::store::mem::Store::new())
}
}

impl Blobs<crate::store::fs::Store> {
/// Load a persistent Blobs protocol handler from a path.
pub async fn persistent(
path: impl AsRef<std::path::Path>,
Expand All @@ -173,8 +182,8 @@ impl Blobs<crate::store::fs::Store> {
}
}

impl<S: crate::store::Store> Blobs<S> {
pub fn new(
impl<S: crate::store::Store> BlobsInner<S> {
fn new(
store: S,
rt: LocalPoolHandle,
events: EventSender,
Expand All @@ -194,18 +203,6 @@ impl<S: crate::store::Store> Blobs<S> {
}
}

pub fn store(&self) -> &S {
&self.store
}

pub fn rt(&self) -> &LocalPoolHandle {
&self.rt
}

pub fn downloader(&self) -> &Downloader {
&self.downloader
}

pub fn endpoint(&self) -> &Endpoint {
&self.endpoint
}
Expand Down Expand Up @@ -390,66 +387,71 @@ impl<S: crate::store::Store> Blobs<S> {
}
}

// trait BlobsInner: Debug + Send + Sync + 'static {
// fn shutdown(self: Arc<Self>) -> BoxedFuture<()>;
// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>>;
// fn client(self: Arc<Self>) -> MemClient;
// fn local_pool_handle(&self) -> &LocalPoolHandle;
// fn downloader(&self) -> &Downloader;
// }

// #[derive(Debug)]
// struct Blobs2 {
// inner: Arc<dyn BlobsInner>,
// }

// impl Blobs2 {
// fn client(&self) -> MemClient {
// self.inner.clone().client()
// }

// fn local_pool_handle(&self) -> &LocalPoolHandle {
// self.inner.local_pool_handle()
// }

// fn downloader(&self) -> &Downloader {
// self.inner.downloader()
// }
// }

// impl<S: crate::store::Store> BlobsInner for Blobs<S> {
// fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
// ProtocolHandler::shutdown(self)
// }

// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
// ProtocolHandler::accept(self, conn)
// }

// fn client(self: Arc<Self>) -> MemClient {
// Blobs::client(self)
// }

// fn local_pool_handle(&self) -> &LocalPoolHandle {
// self.rt()
// }

// fn downloader(&self) -> &Downloader {
// self.downloader()
// }
// }

// impl ProtocolHandler for Blobs2 {
// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
// self.inner.clone().accept(conn)
// }

// fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
// self.inner.clone().shutdown()
// }
// }

impl<S: crate::store::Store> ProtocolHandler for Blobs<S> {
trait DynBlobs: Debug + Send + Sync + 'static {
fn shutdown(self: Arc<Self>) -> BoxedFuture<()>;
fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>>;
fn client(self: Arc<Self>) -> MemClient;
fn local_pool_handle(&self) -> &LocalPoolHandle;
fn downloader(&self) -> &Downloader;
fn endpoint(&self) -> &Endpoint;
fn start_gc(&self, config: GcConfig) -> Result<()>;
fn add_protected(&self, cb: ProtectCb) -> Result<()>;
fn stop_rpc_task(&self);
}

#[derive(Debug)]
pub struct Blobs {
inner: Arc<dyn DynBlobs>,
}

impl Blobs {
pub(crate) fn from_inner<S: Store>(inner: Arc<BlobsInner<S>>) -> Self {
Self { inner }
}

pub fn client(&self) -> MemClient {
self.inner.clone().client()
}

pub fn local_pool_handle(&self) -> &LocalPoolHandle {
self.inner.local_pool_handle()
}

pub fn downloader(&self) -> &Downloader {
self.inner.downloader()
}

pub fn endpoint(&self) -> &Endpoint {
self.inner.endpoint()
}

pub fn add_protected(&self, cb: ProtectCb) -> Result<()> {
self.inner.add_protected(cb)
}

pub fn start_gc(&self, config: GcConfig) -> Result<()> {
self.inner.start_gc(config)
}

pub fn new<S: Store>(
store: S,
rt: LocalPoolHandle,
events: EventSender,
downloader: Downloader,
endpoint: Endpoint,
) -> Self {
let inner = Arc::new(BlobsInner::new(store, rt, events, downloader, endpoint));
Self { inner }
}
}

impl Drop for Blobs {
fn drop(&mut self) {
self.inner.stop_rpc_task();
}
}

impl<S: crate::store::Store> DynBlobs for BlobsInner<S> {
fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
Box::pin(async move {
crate::provider::handle_connection(
Expand All @@ -465,9 +467,55 @@ impl<S: crate::store::Store> ProtocolHandler for Blobs<S> {

fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
Box::pin(async move {
self.stop_rpc_task();
self.store.shutdown().await;
})
}

fn stop_rpc_task(&self) {
if let Some(rpc_handler) = self.rpc_handler.get() {
rpc_handler.shutdown();
}
}

fn client(self: Arc<Self>) -> MemClient {
let client = self
.rpc_handler
.get_or_init(|| MemRpcHandler::new(&self))
.client
.clone();
MemClient::new(client)
}

fn local_pool_handle(&self) -> &LocalPoolHandle {
&self.rt
}

fn downloader(&self) -> &Downloader {
&self.downloader
}

fn start_gc(&self, config: GcConfig) -> Result<()> {
self.start_gc(config)
}

fn add_protected(&self, cb: ProtectCb) -> Result<()> {
self.add_protected(cb)
}

fn endpoint(&self) -> &Endpoint {
&self.endpoint
}
}

impl ProtocolHandler for Blobs {
fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
self.inner.clone().accept(conn)
}

fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
self.inner.clone().shutdown()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to clone, shouldn't this already be owned?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self is Arc (has to be to comply with the ProtocolHandler trait, but I need an Arc, not an &Arc.

This is what I mean when I complain about the Arcs being contagious.

}
}

/// A request to the node to download and share the data specified by the hash.
Expand Down
Loading
Loading