Skip to content

Commit eb90bfc

Browse files
authored
docs(iroh): Update discovery docs, mostly StaticProvider (#3109)
## Description Updates a bunch of docs around StaticProvider and links from the right places. This also removes the ability to build docs without using --all-features. The conditionals made things build but are not correct, items that should be documented on docs.rs were not. This goes back to the simpler solution and documents for everyone how they should build docs. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 65cf688 commit eb90bfc

File tree

5 files changed

+117
-34
lines changed

5 files changed

+117
-34
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ jobs:
228228
- name: Docs
229229
run: cargo doc --workspace --all-features --no-deps --document-private-items
230230

231-
- name: Docs (default features)
232-
run: cargo doc --workspace --no-deps
233-
234231
clippy_check:
235232
timeout-minutes: 30
236233
runs-on: ubuntu-latest

iroh/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ event!(
7373
);
7474
```
7575

76+
## Building documentation
77+
78+
Building the documentation is only supported when using
79+
`--all-features`.
80+
81+
Additionally you might want to enable documenting the cargo features
82+
required for certain APIs, which is done by also passing the `--cfg
83+
iroh_docsrs` flag to rustdoc when building the documentation. This
84+
also requires using nightly rust, e.g.:
85+
86+
```sh
87+
RUSTDOCFLAGS="--cfg iroh_docsrs" cargo +nightly doc --workspace --no-deps --all-features
88+
```
89+
7690
# License
7791

7892
This project is licensed under either of

iroh/src/discovery.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@
2323
//!
2424
//! Some generally useful discovery implementations are provided:
2525
//!
26+
//! - [`StaticProvider`] which allows application to add and remove out-of-band addressing
27+
//! information.
28+
//!
2629
//! - The [`DnsDiscovery`] which performs lookups via the standard DNS systems. To publish
2730
//! to this DNS server a [`PkarrPublisher`] is needed. [Number 0] runs a public instance
2831
//! of a [`PkarrPublisher`] with attached DNS server which is globally available and a
2932
//! reliable default choice.
3033
//!
3134
//! - The [`PkarrResolver`] which can perform lookups from designated [pkarr relay servers]
3235
//! using HTTP.
33-
#![cfg_attr(
34-
feature = "discovery-local-network",
35-
doc = "- [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery
36-
very similar to mDNS."
37-
)]
36+
//!
37+
//! - [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery which is an mDNS
38+
//! implementation.
3839
//!
3940
//! - The [`DhtDiscovery`] also uses the [`pkarr`] system but can also publish and lookup
4041
//! records to/from the Mainline DHT.
@@ -68,13 +69,7 @@
6869
//! # }
6970
//! ```
7071
//!
71-
//! To also enable
72-
#![cfg_attr(feature = "discovery-local-network", doc = "[`LocalSwarmDiscovery`]")]
73-
#![cfg_attr(
74-
not(feature = "discovery-local-network"),
75-
doc = "`LocalSwarmDiscovery`"
76-
)]
77-
//! it can be added as another service in the
72+
//! To also enable [`LocalSwarmDiscovery`] it can be added as another service in the
7873
//! [`ConcurrentDiscovery`]:
7974
//!
8075
//! ```no_run
@@ -106,10 +101,8 @@
106101
//! [`PkarrPublisher`]: pkarr::PkarrPublisher
107102
//! [`DhtDiscovery`]: pkarr::dht::DhtDiscovery
108103
//! [pkarr relay servers]: https://pkarr.org/#servers
109-
#![cfg_attr(
110-
feature = "discovery-local-network",
111-
doc = "[`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery"
112-
)]
104+
//! [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery
105+
//! [`StaticProvider`]: static_provider::StaticProvider
113106
114107
use std::{collections::BTreeSet, net::SocketAddr, sync::Arc, time::Duration};
115108

iroh/src/discovery/static_provider.rs

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
//! A static discovery implementation that allows adding info for nodes manually.
1+
//! A static node discovery to manually add node addressing information.
2+
//!
3+
//! Often an application might get node addressing information out-of-band in an
4+
//! application-specific way. [`NodeTicket`]'s are one common way used to achieve this.
5+
//! This "static" addressing information is often only usable for a limited time so needs to
6+
//! be able to be removed again once know it is no longer useful.
7+
//!
8+
//! This is where the [`StaticProvider`] is useful: it allows applications to add and
9+
//! retract node addressing information that is otherwise out-of-band to iroh.
10+
//!
11+
//! [`NodeTicket`]: https://docs.rs/iroh-base/latest/iroh_base/ticket/struct.NodeTicket
12+
213
use std::{
314
collections::{btree_map::Entry, BTreeMap, BTreeSet},
415
net::SocketAddr,
@@ -11,7 +22,48 @@ use iroh_base::{NodeAddr, NodeId, RelayUrl};
1122

1223
use super::{Discovery, DiscoveryItem};
1324

14-
/// A static discovery implementation that allows providing info for nodes manually.
25+
/// A static node discovery to manually add node addressing information.
26+
///
27+
/// Often an application might get node addressing information out-of-band in an
28+
/// application-specific way. [`NodeTicket`]'s are one common way used to achieve this.
29+
/// This "static" addressing information is often only usable for a limited time so needs to
30+
/// be able to be removed again once know it is no longer useful.
31+
///
32+
/// This is where the [`StaticProvider`] is useful: it allows applications to add and
33+
/// retract node addressing information that is otherwise out-of-band to iroh.
34+
///
35+
/// # Examples
36+
///
37+
/// ```rust
38+
/// use iroh::{discovery::static_provider::StaticProvider, Endpoint, NodeAddr};
39+
/// use iroh_base::SecretKey;
40+
///
41+
/// # #[tokio::main]
42+
/// # async fn main() -> anyhow::Result<()> {
43+
/// // Create the discovery service and endpoint.
44+
/// let discovery = StaticProvider::new();
45+
///
46+
/// let _ep = Endpoint::builder()
47+
/// .add_discovery({
48+
/// let discovery = discovery.clone();
49+
/// move |_| Some(discovery)
50+
/// })
51+
/// .bind()
52+
/// .await?;
53+
///
54+
/// /// Sometime later add a RelayUrl for a fake NodeId.
55+
/// let key = SecretKey::from_bytes(&[0u8; 32]); // Do not use fake secret keys!
56+
/// discovery.add_node_addr(NodeAddr {
57+
/// node_id: key.public(),
58+
/// relay_url: Some("https://example.com".parse()?),
59+
/// direct_addresses: Default::default(),
60+
/// });
61+
///
62+
/// # Ok(())
63+
/// # }
64+
/// ```
65+
///
66+
/// [`NodeTicket`]: https://docs.rs/iroh-base/latest/iroh_base/ticket/struct.NodeTicket
1567
#[derive(Debug, Default, Clone)]
1668
#[repr(transparent)]
1769
pub struct StaticProvider {
@@ -27,16 +79,22 @@ struct NodeInfo {
2779

2880
impl StaticProvider {
2981
/// The provenance string for this discovery implementation.
82+
///
83+
/// This is mostly used for debugging information and allows understanding the origin of
84+
/// addressing information used by an iroh [`Endpoint`].
85+
///
86+
/// [`Endpoint`]: crate::Endpoint
3087
pub const PROVENANCE: &'static str = "static_discovery";
3188

32-
/// Create a new static discovery instance.
89+
/// Creates a new static discovery instance.
3390
pub fn new() -> Self {
3491
Self::default()
3592
}
3693

37-
/// Creates a static discovery instance from something that can be converted into node addresses.
94+
/// Creates a static discovery instance from node addresses.
95+
///
96+
/// # Examples
3897
///
39-
/// Example:
4098
/// ```rust
4199
/// use std::{net::SocketAddr, str::FromStr};
42100
///
@@ -68,7 +126,7 @@ impl StaticProvider {
68126
res
69127
}
70128

71-
/// Add node info for the given node id.
129+
/// Sets node addressing information for the given node ID.
72130
///
73131
/// This will completely overwrite any existing info for the node.
74132
pub fn set_node_addr(&self, info: impl Into<NodeAddr>) -> Option<NodeAddr> {
@@ -90,9 +148,11 @@ impl StaticProvider {
90148
})
91149
}
92150

93-
/// Add node info for the given node id, combining it with any existing info.
151+
/// Augments node addressing information for the given node ID.
94152
///
95-
/// This will add any new direct addresses and overwrite the relay url.
153+
/// The provided addressing information is combined with the existing info in the static
154+
/// provider. Any new direct addresses are added to those already present while the
155+
/// relay URL is overwritten.
96156
pub fn add_node_addr(&self, info: impl Into<NodeAddr>) {
97157
let info: NodeAddr = info.into();
98158
let last_updated = SystemTime::now();
@@ -114,7 +174,7 @@ impl StaticProvider {
114174
}
115175
}
116176

117-
/// Get node info for the given node id.
177+
/// Returns node addressing information for the given node ID.
118178
pub fn get_node_addr(&self, node_id: NodeId) -> Option<NodeAddr> {
119179
let guard = self.nodes.read().expect("poisoned");
120180
let info = guard.get(&node_id)?;
@@ -125,7 +185,9 @@ impl StaticProvider {
125185
})
126186
}
127187

128-
/// Remove node info for the given node id.
188+
/// Removes all node addressing information for the given node ID.
189+
///
190+
/// Any removed information is returned.
129191
pub fn remove_node_addr(&self, node_id: NodeId) -> Option<NodeAddr> {
130192
let mut guard = self.nodes.write().expect("poisoned");
131193
let info = guard.remove(&node_id)?;

iroh/src/endpoint.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,14 @@ pub fn make_server_config(
493493
/// while still remaining independent connections. This will result in more optimal network
494494
/// behaviour.
495495
///
496-
/// New connections are typically created using the [`Endpoint::connect`] and
497-
/// [`Endpoint::accept`] methods. Once established, the [`Connection`] gives access to most
498-
/// [QUIC] features. Individual streams to send data to the peer are created using the
499-
/// [`Connection::open_bi`], [`Connection::accept_bi`], [`Connection::open_uni`] and
500-
/// [`Connection::open_bi`] functions.
496+
/// The endpoint is created using the [`Builder`], which can be created using
497+
/// [`Endpoint::builder`].
498+
///
499+
/// Once an endpoint exists, new connections are typically created using the
500+
/// [`Endpoint::connect`] and [`Endpoint::accept`] methods. Once established, the
501+
/// [`Connection`] gives access to most [QUIC] features. Individual streams to send data to
502+
/// the peer are created using the [`Connection::open_bi`], [`Connection::accept_bi`],
503+
/// [`Connection::open_uni`] and [`Connection::open_bi`] functions.
501504
///
502505
/// Note that due to the light-weight properties of streams a stream will only be accepted
503506
/// once the initiating peer has sent some data on it.
@@ -713,10 +716,17 @@ impl Endpoint {
713716
///
714717
/// See also [`Endpoint::add_node_addr_with_source`].
715718
///
719+
/// # Using node discovery instead
720+
///
721+
/// It is strongly advised to use node discovery using the [`StaticProvider`] instead.
722+
/// This provides more flexibility and future proofing.
723+
///
716724
/// # Errors
717725
///
718726
/// Will return an error if we attempt to add our own [`PublicKey`] to the node map or if the
719727
/// direct addresses are a subset of ours.
728+
///
729+
/// [`StaticProvider`]: crate::discovery::static_provider::StaticProvider
720730
pub fn add_node_addr(&self, node_addr: NodeAddr) -> Result<()> {
721731
self.add_node_addr_inner(node_addr, magicsock::Source::App)
722732
}
@@ -729,10 +739,17 @@ impl Endpoint {
729739
/// address that matches this node's direct addresses will be silently ignored. The *source* is
730740
/// used for logging exclusively and will not be stored.
731741
///
742+
/// # Using node discovery instead
743+
///
744+
/// It is strongly advised to use node discovery using the [`StaticProvider`] instead.
745+
/// This provides more flexibility and future proofing.
746+
///
732747
/// # Errors
733748
///
734749
/// Will return an error if we attempt to add our own [`PublicKey`] to the node map or if the
735750
/// direct addresses are a subset of ours.
751+
///
752+
/// [`StaticProvider`]: crate::discovery::static_provider::StaticProvider
736753
pub fn add_node_addr_with_source(
737754
&self,
738755
node_addr: NodeAddr,

0 commit comments

Comments
 (0)