-
Notifications
You must be signed in to change notification settings - Fork 5
feat!: Add Docs<S> which wraps the Engine<S> #18
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08ccb28
Add Docs<S> which wraps the Engine<S>
rklaehn 21d4fe6
feature gate the Docs<S> and its builder
rklaehn 5426a9e
use the gossip builder API
rklaehn bcedb9f
add net feature
rklaehn 3512805
remove top level feature flag
rklaehn 7bd0c00
Make Engine !Clone and remove a few nested arcs.
rklaehn f2e32e6
fully qualify all the things
rklaehn 5edc52c
more feature flag madness
rklaehn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,23 +1,127 @@ | ||
| //! [`ProtocolHandler`] implementation for the docs [`Engine`]. | ||
|
|
||
| use std::{path::PathBuf, sync::Arc}; | ||
|
|
||
| use anyhow::Result; | ||
| use futures_lite::future::Boxed as BoxedFuture; | ||
| use iroh::{endpoint::Connecting, protocol::ProtocolHandler}; | ||
| use iroh_blobs::net_protocol::{Blobs, ProtectCb}; | ||
| use iroh_gossip::net::Gossip; | ||
|
|
||
| use crate::engine::Engine; | ||
| use crate::{ | ||
| engine::{DefaultAuthorStorage, Engine}, | ||
| store::Store, | ||
| }; | ||
|
|
||
| impl<D: iroh_blobs::store::Store> ProtocolHandler for Engine<D> { | ||
| impl<S: iroh_blobs::store::Store> ProtocolHandler for Docs<S> { | ||
| fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> { | ||
| let this = self.clone(); | ||
| let this = self.engine.clone(); | ||
| Box::pin(async move { this.handle_connection(conn).await }) | ||
| } | ||
|
|
||
| fn shutdown(&self) -> BoxedFuture<()> { | ||
| let this = self.clone(); | ||
| let this = self.engine.clone(); | ||
| Box::pin(async move { | ||
| if let Err(err) = this.shutdown().await { | ||
| tracing::warn!("shutdown error: {:?}", err); | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Docs protocol. | ||
| #[derive(Debug, Clone)] | ||
| pub struct Docs<S> { | ||
| engine: Arc<Engine<S>>, | ||
| #[cfg(feature = "rpc")] | ||
| pub(crate) rpc_handler: Arc<std::sync::OnceLock<crate::rpc::RpcHandler>>, | ||
| } | ||
|
|
||
| impl Docs<()> { | ||
| /// Create a new [`Builder`] for the docs protocol, using in memory replica and author storage. | ||
| pub fn memory() -> Builder { | ||
| Builder::default() | ||
| } | ||
|
|
||
| /// Create a new [`Builder`] for the docs protocol, using a persistent replica and author storage | ||
| /// in the given directory. | ||
| pub fn persistent(path: PathBuf) -> Builder { | ||
| Builder { path: Some(path) } | ||
| } | ||
| } | ||
|
|
||
| impl<S: iroh_blobs::store::Store> Docs<S> { | ||
| /// Get an in memory client to interact with the docs engine. | ||
| #[cfg(feature = "rpc")] | ||
| pub fn client(&self) -> &crate::rpc::client::docs::MemClient { | ||
| &self | ||
| .rpc_handler | ||
| .get_or_init(|| crate::rpc::RpcHandler::new(self.engine.clone())) | ||
| .client | ||
| } | ||
|
|
||
| /// Create a new docs protocol with the given engine. | ||
| /// | ||
| /// Note that usually you would use the [`Builder`] to create a new docs protocol. | ||
| pub fn new(engine: Engine<S>) -> Self { | ||
| Self { | ||
| engine: Arc::new(engine), | ||
| #[cfg(feature = "rpc")] | ||
| rpc_handler: Default::default(), | ||
| } | ||
| } | ||
|
|
||
| /// Handle a docs request from the RPC server. | ||
| #[cfg(feature = "rpc")] | ||
| pub async fn handle_rpc_request< | ||
| C: quic_rpc::server::ChannelTypes<crate::rpc::proto::RpcService>, | ||
| >( | ||
| self, | ||
| msg: crate::rpc::proto::Request, | ||
| chan: quic_rpc::server::RpcChannel<crate::rpc::proto::RpcService, C>, | ||
| ) -> Result<(), quic_rpc::server::RpcServerError<C>> { | ||
| crate::rpc::Handler(self.engine.clone()) | ||
| .handle_rpc_request(msg, chan) | ||
| .await | ||
| } | ||
|
|
||
| /// Get the protect callback for the docs engine. | ||
| pub fn protect_cb(&self) -> ProtectCb { | ||
| self.engine.protect_cb() | ||
| } | ||
| } | ||
|
|
||
| /// Builder for the docs protocol. | ||
| #[derive(Debug, Default)] | ||
| pub struct Builder { | ||
| path: Option<PathBuf>, | ||
| } | ||
|
|
||
| impl Builder { | ||
| /// Build a [`Docs`] protocol given a [`Blobs`] and [`Gossip`] protocol. | ||
| pub async fn spawn<S: iroh_blobs::store::Store>( | ||
| self, | ||
| blobs: &Blobs<S>, | ||
| gossip: &Gossip, | ||
| ) -> anyhow::Result<Docs<S>> { | ||
| let replica_store = match self.path { | ||
| Some(ref path) => Store::persistent(path.join("docs.redb"))?, | ||
| None => Store::memory(), | ||
| }; | ||
| let author_store = match self.path { | ||
| Some(ref path) => DefaultAuthorStorage::Persistent(path.join("default-author")), | ||
| None => DefaultAuthorStorage::Mem, | ||
| }; | ||
| let engine = Engine::spawn( | ||
| blobs.endpoint().clone(), | ||
| gossip.clone(), | ||
| replica_store, | ||
| blobs.store().clone(), | ||
| blobs.downloader().clone(), | ||
| author_store, | ||
| blobs.rt().clone(), | ||
| ) | ||
| .await?; | ||
| Ok(Docs::new(engine)) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.