-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do gossip and blobs as custom handlers
- Loading branch information
Showing
4 changed files
with
86 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use anyhow::Result; | ||
use futures_lite::future; | ||
use iroh_net::endpoint::Connecting; | ||
use std::ops::Deref; | ||
use tracing::warn; | ||
|
||
use super::{DocsEngine, Protocol}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct BlobsProtocol<S> { | ||
rt: tokio_util::task::LocalPoolHandle, | ||
store: S, | ||
} | ||
|
||
impl<S: iroh_blobs::store::Store> BlobsProtocol<S> { | ||
pub fn new(store: S, rt: tokio_util::task::LocalPoolHandle) -> Self { | ||
Self { rt, store } | ||
} | ||
} | ||
|
||
impl<S: iroh_blobs::store::Store> Protocol for BlobsProtocol<S> { | ||
fn accept(&self, conn: Connecting) -> future::Boxed<Result<()>> { | ||
let store = self.store.clone(); | ||
let rt = self.rt.clone(); | ||
Box::pin(async move { | ||
iroh_blobs::provider::handle_connection(conn.await?, store, MockEventSender, rt).await; | ||
Ok(()) | ||
}) | ||
} | ||
|
||
fn shutdown(&self) -> future::Boxed<()> { | ||
let store = self.store.clone(); | ||
Box::pin(async move { | ||
store.shutdown().await; | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct MockEventSender; | ||
|
||
impl iroh_blobs::provider::EventSender for MockEventSender { | ||
fn send(&self, _event: iroh_blobs::provider::Event) -> futures_lite::future::Boxed<()> { | ||
Box::pin(std::future::ready(())) | ||
} | ||
} | ||
|
||
impl Protocol for iroh_gossip::net::Gossip { | ||
fn accept(&self, conn: Connecting) -> future::Boxed<Result<()>> { | ||
let this = self.clone(); | ||
Box::pin(async move { this.handle_connection(conn.await?).await }) | ||
} | ||
} | ||
|
||
impl Protocol for DocsEngine { | ||
fn accept(&self, conn: Connecting) -> future::Boxed<Result<()>> { | ||
let this = self.clone(); | ||
Box::pin(async move { this.handle_connection(conn).await }) | ||
} | ||
|
||
fn shutdown(&self) -> future::Boxed<()> { | ||
let this = self.clone(); | ||
Box::pin(async move { | ||
if let Err(err) = this.deref().shutdown().await { | ||
warn!("Error while shutting down docs engine: {err:?}"); | ||
} | ||
}) | ||
} | ||
} |