-
Notifications
You must be signed in to change notification settings - Fork 964
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 NetworkBehaviour
s to manage connections
#3254
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 |
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
ConnectionHandler
was immediately requested from theNetworkBehaviour
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 aConnectionId
parameter which uniquely identifies the connection. For example, in case aNetworkBehaviour
issues a dial viaNetworkBehaviourAction::Dial
, it can unambiguously detect this dial in these lifecycle callbacks via theConnectionId
.Finally,
NetworkBehaviour::handle_pending_outbound_connection
also replacesNetworkBehaviour::addresses_of_peer
by 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
#3243RecordStore
trait #3239Transport
#3272ConnectionId
andPendingPoint
tolibp2p-swarm
#3346Handler::new
functions #3334ConnectionId
s globally unique #3327DialOpts
#3335handler
fromNetworkBehaviourAction::Dial
#3328THandlerOutEvent
type alias #3368PendingConnectionError::Io
variant #3373DialError::ConnectionIo
variant #3374ListenError
#3375ProtocolConfig
fromGossipsubConfig
#3381new_handler
#3384Links to any relevant issues
Open Questions
TheConnectionId
is 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 theNetworkBehaviour
in how it can generate a new one to ensure it can track state for its own dial. We could usePollParameters
for that. One downside of this is a slight ergonomic hit because the user can only fully construct aNetworkBehaviourAction::Dial
withinNetworkBehaviour::poll
and not within the other callbacks. Some of our protocols already do that via anActionBuilder
pattern.Change checklist