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/: include ListenerId in SwarmEvents #2123

Merged
merged 3 commits into from
Jul 8, 2021

Conversation

elenaf9
Copy link
Contributor

@elenaf9 elenaf9 commented Jul 6, 2021

This PR drafts a change that I'd like to suggest:
I think it would be useful if in case of SwarmEvents associated with a listener (e.g. SwarmEvent::NewListenAddr), the event would include the id of the associated listener.
It allows to map the event back to a previously called swarm.listen_on(). This is especially useful if swarm.listen_on() was called with an zeroed Multiaddress (e.g. /ip4/0.0.0.0/tcp/0), and the actual new address should be mapped back to the newly created listener, without blocking.
Since this information is already in the NetworkEvent that the ExpandedSwarm receives, I don't see a reason to not include it, especially since the user already has to deal with ListenerIds.

@elenaf9
Copy link
Contributor Author

elenaf9 commented Jul 7, 2021

Note: one of the the relay tests (test inactive_connection_timeout) failed, even though my changes should not have affected any logic related to it. The test passes if I test it locally, I'd guess it's a flaky test that only occasionally fails, but from a first quick look I can't find the reason for this.

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.

Implementation looks good to me! I actually have a direct use for this in the tests for the rendezvous protocol! :)

When glancing over the code, I noticed that the relay tests might benefit from this aswell.

@elenaf9
Copy link
Contributor Author

elenaf9 commented Jul 7, 2021

When glancing over the code, I noticed that the relay tests might benefit from this aswell.

@thomaseizinger do you mean by doing something like this?:

let new_listener = dst_swarm.listen_on(dst_addr_via_relay.clone()).unwrap();
...
match dst_swarm.select_next_some().await {
    SwarmEvent::NewListenAddr { address, listener_id} if listener_id == new_listener {
        assert_eq!(address, dst_addr_via_relay);
        break;
   }
...

I could add that, just wanted to know first whether the change in this PR is generally desired.

What do you think @mxinden?

@thomaseizinger
Copy link
Contributor

Yep, that is what I was thinking.

To be fair, for this case it doesn't matter much because comparing the address also works :)

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.

Exposing ListenerId on NewListenAddr, ExpiredListenAddr and ListenerClosed sounds good to me. Thanks for the patch @elenaf9!

It allows to map the event back to a previously called swarm.listen_on(). This is especially useful if swarm.listen_on() was called with an zeroed Multiaddress (e.g. /ip4/0.0.0.0/tcp/0), and the actual new address should be mapped back to the newly created listener, without blocking.

Just a note: a single listen_on might cause multiple NewListenAddr events.

Would you mind adding a note to Swarm::listen_on and Swarm::remove_listener mentioning that they cause one or more NewListenAddr or ExpiredListenAddr events with the same ListenerId. I could imagine this being helpful for newcomers.

Also, could you add an entry to swarm/CHANGELOG.md?

swarm/src/lib.rs Outdated
ExpiredListenAddr{
/// The listener that is no longer listening on the address.
listener_id: ListenerId,
/// The new address that is being listened on.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// The new address that is being listened on.
/// The expired address.

@elenaf9
Copy link
Contributor Author

elenaf9 commented Jul 7, 2021

Would you mind adding a note to Swarm::listen_on and Swarm::remove_listener mentioning that they cause one or more NewListenAddr or ExpiredListenAddr events with the same ListenerId. I could imagine this being helpful for newcomers.

Sounds good, I'll do that. Just one note (related to Discussion #2113): If I'm not mistaken Swarm::remove_listener currently does not create any SwarmEvent::ExpiredListenAddr, hence adding such a note to this method would be misleading? But I'll add it for Swarm::listen_on.

@mxinden
Copy link
Member

mxinden commented Jul 7, 2021

Sounds good, I'll do that. Just one note (related to Discussion #2113): If I'm not mistaken Swarm::remove_listener currently does not create any SwarmEvent::ExpiredListenAddr, hence adding such a note to this method would be misleading? But I'll add it for Swarm::listen_on.

Right. My bad. Thanks @elenaf9!

@elenaf9 elenaf9 marked this pull request as ready for review July 7, 2021 17:03
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.

Thanks @elenaf9!

Just a couple of nit picks on naming in relay/tests/lib to be consistent with the other variable names. Other than that this is good to go from my side.

protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
protocols/relay/tests/lib.rs Outdated Show resolved Hide resolved
Co-authored-by: Max Inden <mail@max-inden.de>
@mxinden mxinden merged commit a414afd into libp2p:master Jul 8, 2021
@elenaf9 elenaf9 deleted the swarm/listeners-events branch July 8, 2021 09:51
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.

3 participants