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: Split off "keep alive" functionality from DummyConnectionHandler #2859

Merged
merged 42 commits into from
Oct 5, 2022

Conversation

thomaseizinger
Copy link
Contributor

@thomaseizinger thomaseizinger commented Aug 31, 2022

Description

Previously, the DummyConnectionHandler offered a "keep alive" functionality, i.e. it allowed users to set the value of what is returned from ConnectionHandler::keep_alive. This handler is primarily used in tests or NetworkBehaviours that don't open any connections (like mDNS). In all of these cases, it is statically known whether we want to keep connections alive. As such, this functionality is better represented by a static KeepAliveConnectionHandler that always returns KeepAlive::Yes and a DummyConnectionHandler that always returns KeepAlive::No.

To follow the naming conventions described in #2217, we introduce a top-level keep_alive and dummy behaviour in libp2p-swarm that contains both the NetworkBehaviour and ConnectionHandler implementation for either case.

Links to any relevant issues

  • swarm: Add FromFn ConnectionHandler #2852: To actually replace "Ping"'s ConnectionHandler with a FromFn ConnectionHandler, we need to decouple the "Keep Alive" functionality from the Ping protocol. If this PR is accepted, I'd send a follow-up to remove the keep-alive functionality from ping in favor of using the KeepAliveBehaviour.

Open Questions

  • Implementations on () are not as easy to discover. Should we retain DummyBehaviour and DummyConnectionHandler but strip the keep-alive logic from them?
  • Should we rename the KeepAlive to KeepAlivePolicy (or something else) to avoid ambiguity with behaviour::KeepAlive?
  • Should we strip the keep alive functionality from libp2p-ping as part of this PR or in a follow-up?

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

() is the default associated type when creating implementations of
traits with IDEs like IntelliJ Rust or rust-analyzer.

Providing a `ConnectionHandler` implementation for those should make
it easier for users to get started.
@thomaseizinger thomaseizinger changed the title RFC: Implement NetworkBehaviour and ConnectionHandler for () swarm: Implement NetworkBehaviour and ConnectionHandler for () Sep 2, 2022
@thomaseizinger thomaseizinger marked this pull request as ready for review September 2, 2022 18:08
@mxinden
Copy link
Member

mxinden commented Sep 9, 2022

Implementations on () are not as easy to discover.

I have to give this more thought. I agree that it makes it harder to discover. Also I don't find the fact that () implements a dummy NetworkBehaviour intuitive.

Should we retain DummyBehaviour and DummyConnectionHandler but strip the keep-alive logic from them?

I think this is a good idea.

@thomaseizinger
Copy link
Contributor Author

Should we retain DummyBehaviour and DummyConnectionHandler but strip the keep-alive logic from them?

I think this is a good idea.

I'll progress in this direction then!

@thomaseizinger thomaseizinger changed the title swarm: Implement NetworkBehaviour and ConnectionHandler for () swarm: Split off "keep alive" functionality from DummyConnectionHandler Sep 12, 2022
@thomaseizinger
Copy link
Contributor Author

This is ready for another review @mxinden !

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 for cleaning this up. Small suggestions on the naming of the two NetworkBehaviour implementations.

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
@mxinden
Copy link
Member

mxinden commented Sep 15, 2022

Can you update the pull request description?

@thomaseizinger
Copy link
Contributor Author

Can you update the pull request description?

Updated. Also added new questions.

@thomaseizinger
Copy link
Contributor Author

This is ready to merge from my end. Feel free to merge in case you want to keep the naming as is or want that to happen in a follow-up @thomaseizinger.

I ended up introducing top-level dummy and keep_alive modules in libp2p_swarm that export both, the ConnectionHandler and the corresponding NetworkBehaviour.

@thomaseizinger
Copy link
Contributor Author

Changelog and PR description are up to date. This is ready to be merged from my end.

@thomaseizinger thomaseizinger linked an issue Sep 27, 2022 that may be closed by this pull request
@mxinden
Copy link
Member

mxinden commented Sep 29, 2022

@thomaseizinger can you take a look at the clippy failure?

error: unused import: `NetworkBehaviour`
Error:   --> examples/ping.rs:44:21
   |
44 | use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
   |                     ^^^^^^^^^^^^^^^^
   |
   = note: `-D unused-imports` implied by `-D warnings`

@thomaseizinger
Copy link
Contributor Author

@thomaseizinger can you take a look at the clippy failure?

error: unused import: `NetworkBehaviour`
Error:   --> examples/ping.rs:44:21
   |
44 | use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
   |                     ^^^^^^^^^^^^^^^^
   |
   = note: `-D unused-imports` implied by `-D warnings`

Ah yes, I expected that one to happen eventually. I originally got the warning because #2932 wasn't yet fixed!

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.

Ready to merge from my end otherwise.

use std::task::{Context, Poll};
use void::Void;

/// Implementation of [`NetworkBehaviour`] that doesn't do anything other than keep all connections alive.
Copy link
Member

Choose a reason for hiding this comment

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

It does not keep connections alive, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely correct. Thanks for spotting the typo!

@mxinden mxinden merged commit bdf9209 into master Oct 5, 2022
@thomaseizinger thomaseizinger deleted the unit-connection-handler branch October 5, 2022 20:44
binarybaron added a commit to UnstoppableSwap/core that referenced this pull request Oct 24, 2024
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/src/behaviour: Add KeepAlive Behaviour
2 participants