Commit 7f2cdd1
authored
refactor(iroh)!: Add
## Description
Discovery services may want to access the endpoint they are attached to
for various reasons.
* Currently, we pass a reference to the `&Endpoint` in
`Discovery::resolve`, but not in `Discovery::publish`.
* All pkarr-based publishers need access to the endpoint's secret key
for signing packets. For this reason, we allow passing a closure to
`endpoint::Builder::add_discovery` that gets the secret key as an
argument
* The pkarr publisher *should* get hold of the endpoint's DNS resolver
to resolve the pkarr relay URL (but doesn't atm, which is a bug - it
uses reqwest's default resolver, always)
So this PR changes the construction by adding an `IntoDiscovery` trait
with a single method `into_discovery(self, context: &DiscoveryContext)
-> impl Discovery`. And `endpoint::Builder::add_discovery` now takes any
`IntoDiscovery`. Through this, discovery service construction is a
two-step process where users create a builder and set any options
available, and then during endpoint building we call `into_discovery`
and pass a discovery context.
We can't pass a full endpoint to discovery services, because if they'd
clone it, the Endpoint could never be dropped due to a reference cycle:
`Endpoint -> Magicsock -> Box<dyn Discovery> -> Endpoint`. Therefore,
this PR adds a `DiscoveryContext` struct that contains the endpoint's
secret key and DNS resolver. We can add more things later, if needed.
There's also a blanket impl of `IntoDiscovery` for any `T: Discovery`,
so discovery services that do not need an endpoint can skip the builder
and don't have to implement `IntoDiscovery` manually at all, users can
just pass the constructed discovery struct to `Endpoint::add_discovery`.
This means *writing* discovery services is a little bit more involved,
as you have to create a builder and implement a second trait. However,
writing discovery services is a rather rare task, and with good docs the
additional work is very minor. On the upside, this creates a clean way
for discovery services to access the endpoint they are mounted on, which
also opens the door for discovery services using iroh itself.
## Breaking Changes
Changed:
* `iroh::endpoint::Builder::add_discovery` now takes an `impl
iroh::discovery::IntoDiscovery` argument instead of a closure that
returns a `Discovery`. You can implement that on a builder struct, and
any `T: Discovery` has an auto-impl of `IntoDiscovery`.
* `iroh::discovery::Discovery::resolve` no longer gets a `&Endpoint`
argument. If you need an endpoint in your discovery service, add a
builder struct and implement `IntoDiscovery` for it, which gives you an
endpoint that you can clone into your service
* Removed `iroh::discovery::dns::DnsDiscovery::new`, use
`DnsDiscovery::builder` instead
* Removed `iroh::discovery::pkarr::PkarrPublisher::new`, use
`PkarrPublisher::builder` instead
* Removed `iroh::discovery::pkarr::PkarrPublisher::with_options`, use
`PkarrPublisher::builder` instead
* Removed `iroh::discovery::pkarr::PkarrResolver::new`, use
`PkarrResolver::builder` instead
* `iroh::discovery::pkarr::PkarrPublisher::n0_dns` now takes no
arguments and returns a `PkarrPublisherBuilder`. The secret key is set
on `PkarrPublisherBuilder::build` instead.
## Notes & open questions
<!-- Any notes, remarks or open questions you have to make about the PR.
-->
## Change checklist
<!-- Remove any that are not relevant. -->
- [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.
- [x] List all breaking changes in the above "Breaking Changes" section.
- [ ] Open an issue or PR on any number0 repos that are affected by this
breaking change. Give guidance on how the updates should be handled or
do the actual updates themselves. The major ones are:
- [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc)
- [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip)
- [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs)
- [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe)
- [ ] [`sendme`](https://github.com/n0-computer/sendme)IntoDiscovery trait (#3327)1 parent 19323e6 commit 7f2cdd1
File tree
12 files changed
+546
-238
lines changed- iroh-relay/src
- iroh
- examples
- src
- discovery
- pkarr
- tests
12 files changed
+546
-238
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
392 | 395 | | |
393 | 396 | | |
394 | 397 | | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
395 | 419 | | |
396 | 420 | | |
397 | 421 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
69 | | - | |
| 67 | + | |
70 | 68 | | |
71 | 69 | | |
72 | 70 | | |
73 | | - | |
| 71 | + | |
74 | 72 | | |
75 | 73 | | |
76 | 74 | | |
| |||
111 | 109 | | |
112 | 110 | | |
113 | 111 | | |
114 | | - | |
| 112 | + | |
115 | 113 | | |
116 | 114 | | |
117 | 115 | | |
118 | | - | |
| 116 | + | |
119 | 117 | | |
120 | 118 | | |
121 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
210 | | - | |
211 | | - | |
| 210 | + | |
212 | 211 | | |
213 | 212 | | |
214 | 213 | | |
215 | 214 | | |
216 | 215 | | |
217 | 216 | | |
218 | | - | |
| 217 | + | |
219 | 218 | | |
220 | 219 | | |
221 | 220 | | |
222 | 221 | | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 222 | + | |
229 | 223 | | |
230 | 224 | | |
231 | 225 | | |
| |||
0 commit comments