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

feat(swarm): allow NetworkBehaviours to create and remove listeners #3292

Merged
merged 50 commits into from
Jun 9, 2023

Conversation

dariusc93
Copy link
Member

@dariusc93 dariusc93 commented Dec 31, 2022

Description

This extends ToSwarm to add ToSwarm::ListenOn and ToSwarm::RemoveListener, which allows creating and removing listeners from a NetworkBehaviour.

Resolves #3291.

Notes

Links to any relevant issues

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

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.

Overall I am in support of this but we should probably wait until we have the connection management story figured out or at least found consensus on #3290.

swarm/src/behaviour.rs Outdated Show resolved Hide resolved
@mxinden
Copy link
Member

mxinden commented Mar 3, 2023

@dariusc93 note that the connection management work, namely #3254, is merged, thus no longer blocking this pull request.

@dariusc93 dariusc93 changed the title feat/: Added ListenOn and RemoveListener to NetworkBehaviourAction feat: Added ListenOn and RemoveListener to NetworkBehaviourAction Mar 4, 2023
@dariusc93
Copy link
Member Author

@dariusc93 note that the connection management work, namely #3254, is merged, thus no longer blocking this pull request.

Thank you for letting me know. I would have to double check, but was it ever decided on rather a ListenerId would be supplied to the Transport through the event or would the current changes in this PR suffice for the time being?

@thomaseizinger
Copy link
Contributor

@dariusc93 note that the connection management work, namely #3254, is merged, thus no longer blocking this pull request.

Thank you for letting me know. I would have to double check, but was it ever decided on rather a ListenerId would be supplied to the Transport through the event or would the current changes in this PR suffice for the time being?

I think we will need to issue the ListenerId as part of the command. Not only will this make things more consistent, it is also the only way how a NetworkBehaviour can effectively manage listeners and not having to reverse-engineer them from the addresses.

@dariusc93
Copy link
Member Author

dariusc93 commented Mar 7, 2023

@dariusc93 note that the connection management work, namely #3254, is merged, thus no longer blocking this pull request.

Thank you for letting me know. I would have to double check, but was it ever decided on rather a ListenerId would be supplied to the Transport through the event or would the current changes in this PR suffice for the time being?

I think we will need to issue the ListenerId as part of the command. Not only will this make things more consistent, it is also the only way how a NetworkBehaviour can effectively manage listeners and not having to reverse-engineer them from the addresses.

Providing the ListenerId should be simple enough in this case (and honestly would be better for tracking, than trying to sort it out, like you said). I would then take it that instead of using Swarm::listen_on in Swarm::handle_behaviour_event in my PR, I would use the transport itself with the provided id, while in Swarm::listen_on, for the sake of compatibility, to generate a ListenerId there and pass it onto the transport?

This can be done outside of this PR too in its own separate PR

EDIT: I made a PR at #3567 just to get your thoughts on the change.

swarm-derive/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated
@@ -1121,6 +1121,12 @@ where
}
}
}
ToSwarm::ListenOn { id, address } => {
self.transport.listen_on(id, address).ok()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this fails, the NetworkBehaviour will never learn about it. I think we need to capture this error and report it back to the NetworkBehaviour as well as issue a ListenerError.

The documentation on ListenerError talks about "non-fatal" errors but I think adding another variant is even more confusing.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about this back during the initial PR, but I assumed it would be alright since ToSwarm::Dial ignores the error. We could do this instead

Suggested change
self.transport.listen_on(id, address).ok()?;
if let Err(e) = self.transport.listen_on(id, address) {
self.behaviour.on_swarm_event(FromSwarm::ListenerError(
behaviour::ListenerError {
listener_id: id,
err: &e,
},
));
}

So it would notify the behaviour of the error instead of ignoring it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking about this back during the initial PR, but I assumed it would be alright since ToSwarm::Dial ignores the error.

Are we? Probably also something that should be fixed then :)

We could do this instead

Suggested change
self.transport.listen_on(id, address).ok()?;
if let Err(e) = self.transport.listen_on(id, address) {
self.behaviour.on_swarm_event(FromSwarm::ListenerError(
behaviour::ListenerError {
listener_id: id,
err: &e,
},
));
}

So it would notify the behaviour of the error instead of ignoring it.

Yep, that looks good to me!

Copy link
Member Author

@dariusc93 dariusc93 May 15, 2023

Choose a reason for hiding this comment

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

I was thinking about this back during the initial PR, but I assumed it would be alright since ToSwarm::Dial ignores the error.

Are we? Probably also something that should be fixed then :)

Looking at Swarm::dial, it does send the error to the behaviour internally before returning the error itself so it doesnt look to be necessary

EDIT: Overlooked the beginning, but it looks like it doesnt send anything for dial_opts.get_or_parse_peer_id().map_err(DialError::InvalidPeerId), but not sure if this would need to be sent to the behaviour. If it does we could do this internally there in the function by doing

let peer_id = match dial_opts
            .get_or_parse_peer_id()
            .map_err(DialError::InvalidPeerId)
        {
            Ok(peer_id) => peer_id,
            Err(e) => {
                self.behaviour
                    .on_swarm_event(FromSwarm::DialFailure(DialFailure {
                        peer_id: None,
                        error: &e,
                        connection_id,
                    }));
                return Err(e);
            }
        };

Copy link
Contributor

Choose a reason for hiding this comment

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

That error will go away once we have multiformats/rust-multiaddr#73.

@thomaseizinger thomaseizinger changed the title feat: Added ListenOn and RemoveListener to NetworkBehaviourAction feat(swarm): add ListenOn and RemoveListener to ToSwarm May 15, 2023
@thomaseizinger thomaseizinger changed the title feat(swarm): add ListenOn and RemoveListener to ToSwarm feat(swarm): all NetworkBehaviours to create and remove listeners May 15, 2023
@thomaseizinger thomaseizinger changed the title feat(swarm): all NetworkBehaviours to create and remove listeners feat(swarm): allow NetworkBehaviours to create and remove listeners May 15, 2023
@mxinden
Copy link
Member

mxinden commented Jun 6, 2023

@thomaseizinger do the latest commits address your concerns?

swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/tests/listener.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
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.

A few minor things but overall, happy to merge this design. Thank you for iterating on this with me @dariusc93 !

swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/listen_opts.rs Outdated Show resolved Hide resolved
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!

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.

@dariusc93 thanks for being patient with all the feedback. Your work on this is valued.

@mxinden mxinden added the send-it label Jun 9, 2023
@mergify mergify bot merged commit c2230f9 into libp2p:master Jun 9, 2023
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.

Extend NetworkBehaviourAction to add and remove listeners
3 participants