-
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.
## Description This makes gossip available in iroh. Using iroh-gossip directly, while not horrible, is a bit verbose. ## Breaking Changes ~~Not sure. It exports a few more things, and adds things. But in theory it should not modify existing things.~~ There should be none, and the semver test seems to agree... ## Notes & open questions - ~~There can be some scenarios where this can cause trouble. E.g. when subscribing and then unsubscribing to a topic that is also used for doc sync.~~ This ^ is taken care of, since docs no longer use subscribe_all! How to deal with missed messages due to slow reader: I think that relying on guaranteed delivery for gossip is not a good idea in any case. so maybe just sending a Lagged but then continuing is best? Forcing the client to resubscribe could be a bit annoying. ## Change checklist - [x] Self-review. - [x] Documentation updates if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
- Loading branch information
Showing
21 changed files
with
828 additions
and
11 deletions.
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 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,94 @@ | ||
use anyhow::{Context, Result}; | ||
use bao_tree::blake3; | ||
use clap::{ArgGroup, Subcommand}; | ||
use futures_lite::StreamExt; | ||
use futures_util::SinkExt; | ||
use iroh::client::gossip::SubscribeOpts; | ||
use iroh::client::Iroh; | ||
use iroh::net::NodeId; | ||
use tokio::io::AsyncBufReadExt; | ||
|
||
#[derive(Subcommand, Debug, Clone)] | ||
#[allow(clippy::large_enum_variant)] | ||
pub enum GossipCommands { | ||
/// Subscribe to a topic | ||
#[command(group( | ||
ArgGroup::new("input") | ||
.required(true) | ||
.args(&["topic", "raw_topic"]) | ||
))] | ||
Subscribe { | ||
/// Topic string to subscribe to. | ||
/// | ||
/// This will be hashed with BLAKE3 to get the actual topic ID. | ||
#[clap(long)] | ||
topic: Option<String>, | ||
/// The raw topic to subscribe to as hex. Needs to be 32 bytes, i.e. 64 hex characters. | ||
#[clap(long)] | ||
raw_topic: Option<String>, | ||
bootstrap: Vec<NodeId>, | ||
#[clap(long, short)] | ||
verbose: bool, | ||
}, | ||
} | ||
|
||
impl GossipCommands { | ||
pub async fn run(self, iroh: &Iroh) -> Result<()> { | ||
match self { | ||
Self::Subscribe { | ||
topic, | ||
raw_topic, | ||
bootstrap, | ||
verbose, | ||
} => { | ||
let bootstrap = bootstrap.into_iter().collect(); | ||
let topic = match (topic, raw_topic) { | ||
(Some(topic), None) => blake3::hash(topic.as_bytes()).into(), | ||
(None, Some(raw_topic)) => { | ||
let mut slice = [0; 32]; | ||
hex::decode_to_slice(raw_topic, &mut slice) | ||
.context("failed to decode raw topic")?; | ||
slice.into() | ||
} | ||
_ => anyhow::bail!("either topic or raw_topic must be provided"), | ||
}; | ||
// blake3::hash(topic.as_ref()).into(); | ||
let opts = SubscribeOpts { | ||
bootstrap, | ||
subscription_capacity: 1024, | ||
}; | ||
|
||
let (mut sink, mut stream) = iroh.gossip().subscribe_with_opts(topic, opts).await?; | ||
let mut input_lines = tokio::io::BufReader::new(tokio::io::stdin()).lines(); | ||
loop { | ||
tokio::select! { | ||
line = input_lines.next_line() => { | ||
let line = line.context("failed to read from stdin")?; | ||
if let Some(line) = line { | ||
sink.send(iroh_gossip::dispatcher::Command::Broadcast(line.into())).await?; | ||
} else { | ||
break; | ||
} | ||
} | ||
res = stream.next() => { | ||
let res = res.context("gossip stream ended")?.context("failed to read gossip stream")?; | ||
match res { | ||
iroh_gossip::dispatcher::Event::Gossip(event) => { | ||
if verbose { | ||
println!("{:?}", event); | ||
} else if let iroh_gossip::dispatcher::GossipEvent::Received(iroh_gossip::dispatcher::Message { content, .. }) = event { | ||
println!("{:?}", content); | ||
} | ||
} | ||
iroh_gossip::dispatcher::Event::Lagged => { | ||
anyhow::bail!("gossip stream lagged"); | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.