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

swarm/behaviour: Replace inject_* with on_event #2867

Closed
wants to merge 9 commits into from

Conversation

mxinden
Copy link
Member

@mxinden mxinden commented Sep 4, 2022

Description

Replace the various NetworkBehaviour::inject_* methods with a single
NetworkBehaviour::on_event method and a InEvent enum.

I want to open this for early feedback before I update the many NetworkBehaviour implementations in /protocols.

Links to any relevant issues

Fixes #2832.

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

Replace the various `NetworkBehaviour::inject_*` methods with a single
`NetworkBehaviour::on_event` method and a `InEvent` `enum`.
Comment on lines 50 to 67
fn on_event(&mut self, event: super::InEvent<Self::ConnectionHandler>) {
match self {
Either::Left(a) => a.inject_listener_closed(id, reason),
Either::Right(b) => b.inject_listener_closed(id, reason),
Either::Left(b) => b.on_event(event.map_handler(
|h| h.unwrap_left(),
|h| match h {
Either::Left(h) => h,
Either::Right(_) => unreachable!(),
},
|e| e.unwrap_left(),
)),
Either::Right(b) => b.on_event(event.map_handler(
|h| h.unwrap_right(),
|h| match h {
Either::Right(h) => h,
Either::Left(_) => unreachable!(),
},
|e| e.unwrap_right(),
)),
Copy link
Member Author

Choose a reason for hiding this comment

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

While not the actual goal of this effort, this is a nice simplification for the Either and Toggle NetworkBehaviour.

In general, I expect this pull request to simplify many of these general purpose NetworkBehaviour implementations. (Not that we have that many of them.)

@MarcoPolo
Copy link
Contributor

Exciting!

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.

Did a first pass, looks good overall!

Had an idea how we could deprecate more functions.

swarm/src/behaviour.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/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
Comment on lines +67 to +68
// Taken from https://github.com/bluss/either.
impl<L, R> IntoEitherHandler<L, R> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need IntoEitherHandler?

Copy link
Member Author

Choose a reason for hiding this comment

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

How else would the IntoConnectionHandler::into_handler methods of the left and right branch be called?

Copy link
Contributor

Choose a reason for hiding this comment

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

What I was getting at is: Can we replace it with Either?

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 think we discussed this in the past, see #2192 (comment) @thomaseizinger.

Copy link
Contributor

Choose a reason for hiding this comment

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

You've got much better memory than I do :D

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about adding a comment about it?

Comment on lines +1632 to +1638
InEvent::ConnectionEstablished {
peer_id: _,
connection_id: _,
endpoint: _,
failed_addresses: _,
other_established: _,
} => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why match on all of these in here?

Copy link
Member Author

Choose a reason for hiding this comment

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

To have a compile time error when any of them change, forcing us to revisit each implementation. I guess it is a bit overkill for this one. Let me know if you prefer this to be changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is a good idea but I'd prefer if we do that in a more prominent place. For example, we could have a dedicated public_api.rs in swarm/tests that concerns itself with such things. I find it a bit odd that this particular impl should take care of that.

Thoughts?

swarm/src/behaviour/either.rs Outdated Show resolved Hide resolved
@mxinden mxinden marked this pull request as ready for review September 7, 2022 06:28
@mxinden
Copy link
Member Author

mxinden commented Sep 7, 2022

Thanks @thomaseizinger for the helpful review. Mind taking another look?

@mxinden
Copy link
Member Author

mxinden commented Sep 7, 2022

Once everyone is happy with the changes in libp2p-swarm I will start porting the many users of libp2p-swarm in /protocols to the new NetworkBehaviour::on_event methods. I plan to do so in this pull request, unless folks prefer separate pull request(s).

swarm/src/behaviour.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/behaviour.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

thomaseizinger commented Sep 8, 2022

Once everyone is happy with the changes in libp2p-swarm I will start porting the many users of libp2p-swarm in /protocols to the new NetworkBehaviour::on_event methods. I plan to do so in this pull request, unless folks prefer separate pull request(s).

We would have to litter the rest of the codebase with allow(deprecated) attributes to make CI pass. I think porting the protocols in this PR is better.

@mxinden
Copy link
Member Author

mxinden commented Sep 17, 2022

63b70fe provides default implementations for NetworkBehaviour::on_swarm_event and NetworkBehaviour::on_connection_handler_event, thus this patch set can be released as a non-breaking change.

Once we remove the deprecated NetworkBehaviour::inject_* methods, I think we should remove the default implementations on NetworkBehaviour::on_swarm_event and NetworkBehaviour::on_connection_handler_event as well.

@thomaseizinger
Copy link
Contributor

63b70fe provides default implementations for NetworkBehaviour::on_swarm_event and NetworkBehaviour::on_connection_handler_event, thus this patch set can be released as a non-breaking change.

Once we remove the deprecated NetworkBehaviour::inject_* methods, I think we should remove the default implementations on NetworkBehaviour::on_swarm_event and NetworkBehaviour::on_connection_handler_event as well.

Agreed!

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.

It is nice that we can release this as a non-breaking change.

Couple of questions:

Comment on lines +167 to +168
// TODO: Instead of the type alias THandlerOutEvent the full definition might be more
// intuitive for users outside of libp2p-swarm.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, esp. because the type alias is not public!

) {
self.on_swarm_event(InEvent::ConnectionClosed {
peer_id: *peer_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice that we can fix the references here!

@@ -721,3 +850,182 @@ impl Default for CloseConnection {
CloseConnection::All
}
}

// TODO: Rename to FromSwarm
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we pool the rename together with the release of this change as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also name it FromTransport maybe? I am beginning to more and more take on the notion of libp2p-swarm being a kind of "runtime" that actually doesn't do anything by itself but passes data between other components so in a way, the swarm itself would never produce an event.

Copy link
Contributor

Choose a reason for hiding this comment

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

In favor of naming it FromSwarm. FromTransport imo could be confusing because its not an event emitted by a transport implementation (e.g. in the relay protocol we do have events that are actually produced by the relay client transport). Also, some events are still caused by the swarm and not the transport, e.g. a DialFailure because of swarm connection limits.
That being said, I do generally like the conception of swarm being some kind of "runtime".

Comment on lines +67 to +68
// Taken from https://github.com/bluss/either.
impl<L, R> IntoEitherHandler<L, R> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about adding a comment about it?

@mxinden
Copy link
Member Author

mxinden commented Oct 12, 2022

Closing here in favor of #3011.

@mxinden mxinden closed this Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

swarm/behaviour: Inject events via single method
4 participants