Removes the events system from Network and ProtocolsHandler #685
Labels
difficulty:hard
priority:important
The changes needed are critical for libp2p, or are blocking another project
Right now each node gets assigned its own background task, and the
Network
asynchronously collects events that happen on these background tasks.In order to avoid locking, what the
Network
knows is delayed compared to the actual state. For example theNetwork
may think that we have a connection to a peer, while in fact this connection has already been closed a few hundred microseconds earlier.Because of that, the only way the
Network
/Swarm
/NetworkBehaviour
and theProtocolsHandler
can communicate is by sending and receiving asynchronous events.While this looks like a good idea, it makes writing correct APIs much more tedious. Instead of, for example, having an implementation of
ProtocolsHandler
with a method that doesfn start_query() -> Result<(), Error>
and calling it (which would be idiomatic Rust), you need to send aStartQuery
event with some identifier, then process the fact that the handler can return aStartQueryError
later. This also means that you need to handle the clean up in case the node gets its connection closed, and a timeout system.In terms of efficiency, this also makes the API much more clone-heavy. Events, since they have to be
'static
, need to hold clones of data, and cannot hold references.Instead, I think that we should remove the inbound events entirely (events sent from the
Network
towards theProtocolsHandler
) and let the user ofNetwork
access theNodeHandler
(and thereforeProtocolsHandler
) by holding aMutex
when callingNetwork::peer()
. However, in order to avoid logic errors, it should be possible to obtain a mutex to theNodeHandler
only once we have processed all the pending outbound events coming from the handler of that node. This guarantees that the inside and the outside are successfully synchronized in terms of state.In practice, a
PeerConnected
(in theraw_swarm
module) should have a way to poll the events specific to that peer, and once all the events have been polled you obtain an object thatDeref
s to theNodeHandler
.The
NetworkBehaviour
should eventually be adjusted for that in the future, but for now I think it is enough to only modify theNetwork
and not touch theNetworkBehaviour
trait nor the API ofSwarm
.The text was updated successfully, but these errors were encountered: