Skip to content
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

mdns: update if-watch to 3.0.0 #3096

Merged
merged 15 commits into from
Nov 18, 2022
Merged

mdns: update if-watch to 3.0.0 #3096

merged 15 commits into from
Nov 18, 2022

Conversation

jxs
Copy link
Member

@jxs jxs commented Nov 8, 2022

and rename TokioMdns to Mdns living in its own module, and move Mdns to async_io::Mdns.

Description

updates to if-watch 3.0. This was simplest I found to not touch the signature of GenMdns::new wdyt @thomaseizinger?
@mxinden turns out IfWatcher::new returns Result so implementing Default for it wouldn't solve the problem, so I had to create a new TryDefault trait to be able to abstract over it.

We could also try put GenMdns methods and trait impls on a macro and generate it for tokio and async_io code would be probably more complicated, but the doc would be cleaner and GenMdns would no longer be required.

CC @rkuhn

Links to any relevant issues

superseeds #3095

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Commit message body

and rename TokioMdns to Mdns living in its own module,
and move Mdns to async_io::Mdns.
@jxs jxs force-pushed the update-if-watch branch from 9acdedd to 9bd5168 Compare November 8, 2022 14:02
Copy link
Contributor

@rkuhn rkuhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me for mDNS! Shall I do the TCP part or do you want to take it?

@jxs
Copy link
Member Author

jxs commented Nov 8, 2022

Looks good to me for mDNS! Shall I do the TCP part or do you want to take it?

yeah, feel free to :) I didn't go for it yet as I wanted to clear the approach first, so if Thomas approves the approach feel free to implement for tcp Roland :)

@thomaseizinger thomaseizinger changed the title mdns: update if-watch to 3.0, mdns: update if-watch to 3.0.0 Nov 8, 2022
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this @jxs!

@@ -4,6 +4,9 @@

- Update to `libp2p-swarm` `v0.41.0`.

- Update to `if-watch` `0.3.0` and both rename `TokioMdns` to `Mdns` living in `tokio::Mdns`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Update to `if-watch` `0.3.0` and both rename `TokioMdns` to `Mdns` living in `tokio::Mdns`,
- Update to `if-watch` `3.0.0` and both rename `TokioMdns` to `Mdns` living in `tokio::Mdns`,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Thomas, updated.

protocols/mdns/src/lib.rs Show resolved Hide resolved
Comment on lines 70 to 74
impl TryDefault for IfWatcher {
fn try_default() -> Result<Self, std::io::Error> {
Self::new()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way of doing this which does not require a trait would be to say:

impl GenMdns<TokioUdpSocket, TokioTimer, IfWatcher> {
    /// Builds a new `Mdns` behaviour.
    pub fn new(config: MdnsConfig) -> io::Result<Self> {
        Ok(Self {
            config,
            if_watch: IfWatcher::new(),
            iface_states: Default::default(),
            discovered_nodes: Default::default(),
            closest_expiration: Default::default(),
        })
	}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested but I think this should work!

Copy link
Member Author

@jxs jxs Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that works if we remove GenMdns::new otherwise compiler complains regarding duplication. The disadvantage I see with that is that then methods (and its docs) are scattered between {tokio, async_io}::Mdns and GenMdns, would you prefer that approach? Tbh ideally I think it would be great if we could deprecate/hide GenMdns and just present {tokio, async_io}::Mdns to the end user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a similar discussion in libp2p-tcp: #2961 (review)

From what I remember, the functions don't show up for the type alias at all so probably best to not do this at all.

The need for another trait is a bit annoying though.

protocols/mdns/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/mdns/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me minus the outstanding comments.

@mergify
Copy link
Contributor

mergify bot commented Nov 13, 2022

This pull request has merge conflicts. Could you please resolve them @jxs? 🙏

@mergify
Copy link
Contributor

mergify bot commented Nov 14, 2022

This pull request has merge conflicts. Could you please resolve them @jxs? 🙏

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jxs! A few comments.

@@ -4,6 +4,16 @@

- Update to `libp2p-swarm` `v0.41.0`.

- Update to `if-watch` `3.0.0` and both rename `TokioMdns` to `Behaviour` living in `tokio::Behaviour`,
and move and rename `Mdns` to `async_io::Behaviour`. See [PR 3096]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and move and rename `Mdns` to `async_io::Behaviour`. See [PR 3096]
and move and rename `Mdns` to `async_io::Behaviour`. See [PR 3096].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, addressed.


/// A `NetworkBehaviour` for mDNS. Automatically discovers peers on the local network and adds
/// them to the topology.
#[derive(Debug)]
pub struct GenMdns<S, T> {
pub struct Behaviour<S, T>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to combine all type parameters here to a single one, i.e. P and have a Provider trait?

trait Provider {
	type Socket;
	type Timer;
	type IfWatcher;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it does! Thanks Thomas. 👍
We now have more than one Provider trait among the protocols (tcp and mdns) but as they are namedspaced by the crates I don't think it's a problem, it can be considered akin to the tokio and async_io namespaces.

Comment on lines -42 to -48
pub use crate::behaviour::{GenMdns, MdnsEvent};
pub use crate::behaviour::{Behaviour, Event};

#[cfg(feature = "async-io")]
pub use crate::behaviour::Mdns;
pub use crate::behaviour::async_io;

#[cfg(feature = "tokio")]
pub use crate::behaviour::TokioMdns;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'd add type aliases for this too then users would only be presented with deprecation warnings instead of compile errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, makes sense. When putting the types was thinking that since this is a breaking change putting the types would be redundant, but if we also add for Mdns then it doesn't become a breaking change. Thanks Thomas :)

mergify bot pushed a commit that referenced this pull request Nov 15, 2022
Update to `if-watch` version 3.0.0 and pass through features, such that `libp2p-tcp/async-io` selects `if-watch/smol` and `libp2p-tcp/tokio` brings in `if-watch/tokio`.
The mDNS part is already done in #3096.
jxs added 4 commits November 15, 2022 14:03
and set the AsyncSocket and AsyncTimer as associated types, so that the
generic Behaviour only takes one parameter P which provides all these
types.
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

protocols/mdns/CHANGELOG.md Outdated Show resolved Hide resolved
protocols/mdns/CHANGELOG.md Outdated Show resolved Hide resolved
protocols/mdns/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/mdns/src/behaviour.rs Outdated Show resolved Hide resolved
@mergify
Copy link
Contributor

mergify bot commented Nov 17, 2022

This pull request has merge conflicts. Could you please resolve them @jxs? 🙏

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice & clean!

Good to go from my end :)

protocols/mdns/CHANGELOG.md Outdated Show resolved Hide resolved
@mergify mergify bot merged commit 08510dd into libp2p:master Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants