Skip to content

Commit ab3afef

Browse files
authoredOct 4, 2024··
docs(iroh-net): Add examples to discovery (#2786)
## Description This at least documents how to do the normal setup for discovery. ## 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. - ~~[ ] Tests if relevant.~~ - ~~[ ] All breaking changes documented.~~
1 parent 730f717 commit ab3afef

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed
 

‎iroh-net/src/discovery.rs

+61-3
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,79 @@
2323
//!
2424
//! Some generally useful discovery implementations are provided:
2525
//!
26-
//! - The [`DnsDiscovery`] which supports publishing to a special DNS server and performs
27-
//! lookups via the standard DNS systems. [Number 0] runs a public instance of this which
28-
//! is globally available and a reliable default choice.
26+
//! - The [`DnsDiscovery`] which performs lookups via the standard DNS systems. To publish
27+
//! to this DNS server a [`PkarrPublisher`] is needed. [Number 0] runs a public instance
28+
//! of a [`PkarrPublisher`] with attached DNS server which is globally available and a
29+
//! reliable default choice.
2930
//!
3031
//! - The [`PkarrResolver`] which can perform lookups from designated [pkarr relay servers]
3132
//! using HTTP.
3233
//!
34+
//! - The [`LocalSwarmDiscovery`] discovers iroh-net nodes present on the local network,
35+
//! very similar to mdNS.
36+
//!
37+
//! - The [`DhtDiscovery`] also uses the [`pkarr`] system but can also publish and lookup
38+
//! records to/from the Mainline DHT.
39+
//!
3340
//! To use multiple discovery systems simultaneously use [`ConcurrentDiscovery`] which will
3441
//! perform lookups to all discovery systems at the same time.
3542
//!
43+
//! # Examples
44+
//!
45+
//! A very common setup is to enable DNS discovery, which needs to be done in two parts as a
46+
//! [`PkarrPublisher`] and [`DnsDiscovery`]:
47+
//!
48+
//! ```no_run
49+
//! use iroh_net::discovery::dns::DnsDiscovery;
50+
//! use iroh_net::discovery::pkarr::PkarrPublisher;
51+
//! use iroh_net::discovery::ConcurrentDiscovery;
52+
//! use iroh_net::key::SecretKey;
53+
//! use iroh_net::Endpoint;
54+
//!
55+
//! # async fn wrapper() -> anyhow::Result<()> {
56+
//! let secret_key = SecretKey::generate();
57+
//! let discovery = ConcurrentDiscovery::from_services(vec![
58+
//! Box::new(PkarrPublisher::n0_dns(secret_key.clone())),
59+
//! Box::new(DnsDiscovery::n0_dns()),
60+
//! ]);
61+
//! let ep = Endpoint::builder()
62+
//! .secret_key(secret_key)
63+
//! .discovery(Box::new(discovery))
64+
//! .bind()
65+
//! .await?;
66+
//! # Ok(())
67+
//! # }
68+
//! ```
69+
//!
70+
//! To also enable [`LocalSwarmDiscovery`], it can be added as another service in the
71+
//! [`ConcurrentDiscovery`]:
72+
//!
73+
//! ```no_run
74+
//! # use iroh_net::discovery::dns::DnsDiscovery;
75+
//! # use iroh_net::discovery::local_swarm_discovery::LocalSwarmDiscovery;
76+
//! # use iroh_net::discovery::pkarr::PkarrPublisher;
77+
//! # use iroh_net::discovery::ConcurrentDiscovery;
78+
//! # use iroh_net::key::SecretKey;
79+
//! #
80+
//! # async fn wrapper() -> anyhow::Result<()> {
81+
//! # let secret_key = SecretKey::generate();
82+
//! let discovery = ConcurrentDiscovery::from_services(vec![
83+
//! Box::new(PkarrPublisher::n0_dns(secret_key.clone())),
84+
//! Box::new(DnsDiscovery::n0_dns()),
85+
//! Box::new(LocalSwarmDiscovery::new(secret_key.public())?),
86+
//! ]);
87+
//! # Ok(())
88+
//! # }
89+
//! ```
90+
//!
3691
//! [`RelayUrl`]: crate::relay::RelayUrl
3792
//! [`Builder::discovery`]: crate::endpoint::Builder::discovery
3893
//! [`DnsDiscovery`]: dns::DnsDiscovery
3994
//! [Number 0]: https://n0.computer
4095
//! [`PkarrResolver`]: pkarr::PkarrResolver
96+
//! [`PkarrPublisher`]: pkarr::PkarrPublisher
97+
//! [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery
98+
//! [`DhtDiscovery`]: pkarr::dht::DhtDiscovery
4199
//! [pkarr relay servers]: https://pkarr.org/#servers
42100
43101
use std::time::Duration;

0 commit comments

Comments
 (0)
Please sign in to comment.