-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(swarm)!: allow NetworkBehaviours to manage connections
#3254
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
Conversation
Use a static Atomic counter instead. That gives us the best of both worlds! |
|
Before we merge this, are we happy with the |
I am in favor of |
mxinden
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready for merge from my end. Please add the send-it label once you want this merged @thomaseizinger. Including it in v0.51.0 sounds good to me.
|
This pull request has merge conflicts. Could you please resolve them @thomaseizinger? 🙏 |
# Conflicts: # swarm/CHANGELOG.md
|
Sorry I missed this.
Why not keep the |
In general, events typically don't have return values, they just state what happened. Event handlers are thus often named with |
Q: what's the preferred way of getting a list of known addresses for a peer? Before 0.51 we're using |
See also #3633. I consider all functions of At the moment, there is no replacement for your usecase. Which behaviours do you need to gather addresses from? I am open to add public functions to e.g. |
Makes sense.
mdns, kad.
Sounds great 👍 |
Are you open to send a PR for mdns? I think kad already exposes the necessary functionality, see linked ticket above. |
sure |
libp2p#3328) We create the `ConnectionId` for the new connection as part of `DialOpts`. This allows `NetworkBehaviour`s to accurately track state regarding their own dial attempts. This patch is the main enabler of libp2p#3254. Removing the `handler` field will allow us to deprecate the `NetworkBehaviour::new_handler` function in favor of four new ones that give more control over the connection lifecycle.
Previously, a `ConnectionHandler` was immediately requested from the `NetworkBehaviour` as soon as a new dial was initiated or a new incoming connection accepted. With this patch, we delay the creation of the handler until the connection is actually established and fully upgraded, i.e authenticated and multiplexed. As a consequence, `NetworkBehaviour::new_handler` is now deprecated in favor of a new set of callbacks: - `NetworkBehaviour::handle_pending_inbound_connection` - `NetworkBehaviour::handle_pending_outbound_connection` - `NetworkBehaviour::handle_established_inbound_connection` - `NetworkBehaviour::handle_established_outbound_connection` All callbacks are fallible, allowing the `NetworkBehaviour` to abort the connection either immediately or after it is fully established. All callbacks also receive a `ConnectionId` parameter which uniquely identifies the connection. For example, in case a `NetworkBehaviour` issues a dial via `NetworkBehaviourAction::Dial`, it can unambiguously detect this dial in these lifecycle callbacks via the `ConnectionId`. Finally, `NetworkBehaviour::handle_pending_outbound_connection` also replaces `NetworkBehaviour::addresses_of_peer` by allowing the behaviour to return more addresses to be used for the dial. Resolves libp2p#2824. Pull-Request: libp2p#3254.
Description
Previously, a
ConnectionHandlerwas immediately requested from theNetworkBehaviouras soon as a new dial was initiated or a new incoming connection accepted.With this patch, we delay the creation of the handler until the connection is actually established and fully upgraded, i.e authenticated and multiplexed.
As a consequence,
NetworkBehaviour::new_handleris now deprecated in favor of a new set of callbacks:NetworkBehaviour::handle_pending_inbound_connectionNetworkBehaviour::handle_pending_outbound_connectionNetworkBehaviour::handle_established_inbound_connectionNetworkBehaviour::handle_established_outbound_connectionAll callbacks are fallible, allowing the
NetworkBehaviourto abort the connection either immediately or after it is fully established. All callbacks also receive aConnectionIdparameter which uniquely identifies the connection. For example, in case aNetworkBehaviourissues a dial viaNetworkBehaviourAction::Dial, it can unambiguously detect this dial in these lifecycle callbacks via theConnectionId.Finally,
NetworkBehaviour::handle_pending_outbound_connectionalso replacesNetworkBehaviour::addresses_of_peerby allowing the behaviour to return more addresses to be used for the dial.Resolves #2824.
Notes
This is a variant of #3099 with less breaking changes and more deprecations.
Arc#3243RecordStoretrait #3239Transport#3272ConnectionIdandPendingPointtolibp2p-swarm#3346Handler::newfunctions #3334ConnectionIds globally unique #3327DialOpts#3335handlerfromNetworkBehaviourAction::Dial#3328THandlerOutEventtype alias #3368PendingConnectionError::Iovariant #3373DialError::ConnectionIovariant #3374ListenError#3375ProtocolConfigfromGossipsubConfig#3381new_handler#3384Links to any relevant issues
Open Questions
TheConnectionIdis currently generated through a random number generator. This is not ideal and could in theory lead to collisions:Should we use a UUID instead? I think those are less likely to produce a collision.Should we manage the connection IDs centrally, i.e. in theSwarm? This would require us to provide a facility to theNetworkBehaviourin how it can generate a new one to ensure it can track state for its own dial. We could usePollParametersfor that. One downside of this is a slight ergonomic hit because the user can only fully construct aNetworkBehaviourAction::DialwithinNetworkBehaviour::polland not within the other callbacks. Some of our protocols already do that via anActionBuilderpattern.Change checklist