rust-libp2p icon indicating copy to clipboard operation
rust-libp2p copied to clipboard

protocols/identify: Move I/O to ConnectionHandler

Open mxinden opened this issue 3 years ago • 1 comments

Description

I/O should not happen in the NetworkBehaviour implementation. It should happen in the ConnectionHandler implementation.

Motivation

All NetworkBehaviour implementations are driven by a single future task. Opposite to that we spawn a new future task for every connection. Doing as much as possible, especially I/O, on the connection tasks and not on the main task allows logic to potentially run in parallel.

Current Implementation

Today libp2p-identify drives the future returning a response to a remote in the NetworkBehaviour::poll implementation. https://github.com/libp2p/rust-libp2p/blob/66c275520d8cf56152cce9a1c24bb4e2c42e0833/protocols/identify/src/identify.rs#L53-L54

Are you planning to do it yourself in a pull request?

No

mxinden avatar Sep 12 '22 04:09 mxinden

The problem with today's implementation of identify is that we only know the observed address in the NetworkBehaviour. To fix this, we will have to implement IntoConnectionHandler for the identify protocol and grab the observed address out of the ConnectedPoint. Going further, this information can be passed into the InboundUpgrade implementation via the OpenInboundInfo type in ConnectedHandler.

thomaseizinger avatar Sep 12 '22 06:09 thomaseizinger

@thomaseizinger could you shed some light on how you envisioned passing the observed address from Behaviour to Handler ?

I can't really see where to do that as Behaviour::new_handler() doesn't have access to the MultiAddress.
Meanwhile I started implementing this using NetworkBehaviourAction::NotifyHandler to pass the MultiAddress and the Substream to the Handler, but it doesn't seem semantically correct passing the Substream from the Handler to the Behaviour to then get it back from the Behaviour again, I feel this should be encapsulated in the Handler.

Thanks :)

jxs avatar Nov 25 '22 22:11 jxs

You need to implement IntoConnectionHandler instead of ConnectionHandler. That will give you access to the ConnectedPoint.

thomaseizinger avatar Nov 26 '22 03:11 thomaseizinger

right, thanks Thomas. What about listen_addresses and supported_protocols needed to create the Identify Info?
We only get them from PollParameters on NetworkBehaviour::poll not ConnectionHandler:poll and we need . This maybe also affects the development https://github.com/libp2p/rust-libp2p/pull/3153.

jxs avatar Nov 26 '22 16:11 jxs

right, thanks Thomas. What about listen_addresses and supported_protocols needed to create the Identify Info?
We only get them from PollParameters on NetworkBehaviour::poll not ConnectionHandler:poll and we need . This maybe also affects the development https://github.com/libp2p/rust-libp2p/pull/3153.

I think you will need to store those in the NetworkBehaviour and pass them on in new_handler.

A couple of refactorings coming in will affect this but overall, this should still work I believe!

thomaseizinger avatar Nov 26 '22 20:11 thomaseizinger

right, thanks Thomas. What about listen_addresses and supported_protocols needed to create the Identify Info? We only get them from PollParameters on NetworkBehaviour::poll not ConnectionHandler:poll and we need . This maybe also affects the development #3153.

I think you will need to store those in the NetworkBehaviour and pass them on in new_handler.

Discussed out-of-band with @jxs today. Passing them in new_handler is not possible as the set of supported protocols might change during the lifetime of a ConnectionHandler. I know it doesn't today, but it will in the future, see e.g. https://github.com/libp2p/rust-libp2p/pull/2521.

Instead I suggest passing an event up from the ConnectionHandler when identify information like supported protocols is needed. The NetworkBehaviour would respond to the event, retrieving the list of supported protocols from the PollParameters in NetworkBehaviour::poll.

mxinden avatar Dec 01 '22 20:12 mxinden

right, thanks Thomas. What about listen_addresses and supported_protocols needed to create the Identify Info? We only get them from PollParameters on NetworkBehaviour::poll not ConnectionHandler:poll and we need . This maybe also affects the development #3153.

I think you will need to store those in the NetworkBehaviour and pass them on in new_handler.

Discussed out-of-band with @jxs today. Passing them in new_handler is not possible as the set of supported protocols might change during the lifetime of a ConnectionHandler. I know it doesn't today, but it will in the future, see e.g. https://github.com/libp2p/rust-libp2p/pull/2521.

Instead I suggest passing an event up from the ConnectionHandler when identify information like supported protocols is needed. The NetworkBehaviour would respond to the event, retrieving the list of supported protocols from the PollParameters in NetworkBehaviour::poll.

Even once we have a mechanism where they change, I'd expect the behaviour to be notified. It feels more appropriate to pass it down when it changes instead of requesting it every time. Esp. because that allows the handler to answer incoming streams without interacting with the behaviour which simplifies things a lot.

thomaseizinger avatar Dec 01 '22 20:12 thomaseizinger

I'd expect the behaviour to be notified.

:+1: like we do for new listen addresses via FromSwarm::NewListenAddr I think we should have an event informing a NetworkBehaviour of a new listen protocol. The NetworkBehaviour can then pass that event down to the ConnectionHandler.

Given that we don't have these mechanisms yet, I suggest proceeding with the above, that is have the ConnectionHandler request the information from the NetworkBehaviour. We do the same in libp2p-relay where one needs to send ones external addresses to a remote.

mxinden avatar Dec 02 '22 16:12 mxinden

It makes the implementation of the handler more complicated because you now have to suspend the substream and can't use async-await to implement the protocol (unless you use a channel).

At the moment, we know that the protocols don't change so we just only send the update once. Later, once they can change, we can send the update every time it is modified.

Unless we disagree on the bigger idea that we don't want to push state into the handler, this should be preferred where possible because it is IMO a simpler design.

thomaseizinger avatar Dec 04 '22 23:12 thomaseizinger

Closing here with #3208 merged.

mxinden avatar Dec 13 '22 20:12 mxinden