mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-25 15:51:34 +00:00
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core. Instead of trying to enforce a single connection per peer, which involves quite a bit of additional complexity e.g. to prioritise simultaneously opened connections and can have other undesirable consequences [1], we now make multiple connections per peer a feature. The gist of these changes is as follows: The concept of a "node" with an implicit 1-1 correspondence to a connection has been replaced with the "first-class" concept of a "connection". The code from `src/nodes` has moved (with varying degrees of modification) to `src/connection`. A `HandledNode` has become a `Connection`, a `NodeHandler` a `ConnectionHandler`, the `CollectionStream` was the basis for the new `connection::Pool`, and so forth. Conceptually, a `Network` contains a `connection::Pool` which in turn internally employs the `connection::Manager` for handling the background `connection::manager::Task`s, one per connection, as before. These are all considered implementation details. On the public API, `Peer`s are managed as before through the `Network`, except now the API has changed with the shift of focus to (potentially multiple) connections per peer. The `NetworkEvent`s have accordingly also undergone changes. The Swarm APIs remain largely unchanged, except for the fact that `inject_replaced` is no longer called. It may now practically happen that multiple `ProtocolsHandler`s are associated with a single `NetworkBehaviour`, one per connection. If implementations of `NetworkBehaviour` rely somehow on communicating with exactly one `ProtocolsHandler`, this may cause issues, but it is unlikely. [1]: https://github.com/paritytech/substrate/issues/4272 * Fix intra-rustdoc links. * Update core/src/connection/pool.rs Co-Authored-By: Max Inden <mail@max-inden.de> * Address some review feedback and fix doc links. * Allow responses to be sent on the same connection. * Remove unnecessary remainders of inject_replaced. * Update swarm/src/behaviour.rs Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Update swarm/src/lib.rs Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Update core/src/connection/manager.rs Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Update core/src/connection/manager.rs Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Update core/src/connection/pool.rs Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Incorporate more review feedback. * Move module declaration below imports. * Update core/src/connection/manager.rs Co-Authored-By: Toralf Wittner <tw@dtex.org> * Update core/src/connection/manager.rs Co-Authored-By: Toralf Wittner <tw@dtex.org> * Simplify as per review. * Fix rustoc link. * Add try_notify_handler and simplify. * Relocate DialingConnection and DialingAttempt. For better visibility constraints. * Small cleanup. * Small cleanup. More robust EstablishedConnectionIter. * Clarify semantics of `DialingPeer::connect`. * Don't call inject_disconnected on InvalidPeerId. To preserve the previous behavior and ensure calls to `inject_disconnected` are always paired with calls to `inject_connected`. * Provide public ConnectionId constructor. Mainly needed for testing purposes, e.g. in substrate. * Move the established connection limit check to the right place. * Clean up connection error handling. Separate connection errors into those occuring during connection setup or upon rejecting a newly established connection (the `PendingConnectionError`) and those errors occurring on previously established connections, i.e. for which a `ConnectionEstablished` event has been emitted by the connection pool earlier. * Revert change in log level and clarify an invariant. * Remove inject_replaced entirely. * Allow notifying all connection handlers. Thereby simplify by introducing a new enum `NotifyHandler`, used with a single constructor `NetworkBehaviourAction::NotifyHandler`. * Finishing touches. Small API simplifications and code deduplication. Some more useful debug logging. Co-authored-by: Max Inden <mail@max-inden.de> Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com> Co-authored-by: Toralf Wittner <tw@dtex.org>
This commit is contained in:
@ -19,7 +19,7 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::protocols_handler::{IntoProtocolsHandler, ProtocolsHandler};
|
||||
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, nodes::ListenerId};
|
||||
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, connection::{ConnectionId, ListenerId}};
|
||||
use std::{error, task::Context, task::Poll};
|
||||
|
||||
/// A behaviour for the network. Allows customizing the swarm.
|
||||
@ -60,7 +60,7 @@ pub trait NetworkBehaviour: Send + 'static {
|
||||
///
|
||||
/// The network behaviour (ie. the implementation of this trait) and the handlers it has
|
||||
/// spawned (ie. the objects returned by `new_handler`) can communicate by passing messages.
|
||||
/// Messages sent from the handler to the behaviour are injected with `inject_node_event`, and
|
||||
/// Messages sent from the handler to the behaviour are injected with `inject_event`, and
|
||||
/// the behaviour can send a message to the handler by making `poll` return `SendEvent`.
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler;
|
||||
|
||||
@ -85,27 +85,15 @@ pub trait NetworkBehaviour: Send + 'static {
|
||||
/// or may not have been processed by the handler.
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint);
|
||||
|
||||
/// Indicates the behaviour that we replace the connection from the node with another.
|
||||
///
|
||||
/// The handler that used to be dedicated to this node has been destroyed and replaced with a
|
||||
/// new one. Any event that has been sent to it may or may not have been processed.
|
||||
///
|
||||
/// The default implementation of this method calls `inject_disconnected` followed with
|
||||
/// `inject_connected`. This is a logically safe way to implement this behaviour. However, you
|
||||
/// may want to overwrite this method in the situations where this isn't appropriate.
|
||||
fn inject_replaced(&mut self, peer_id: PeerId, closed_endpoint: ConnectedPoint, new_endpoint: ConnectedPoint) {
|
||||
self.inject_disconnected(&peer_id, closed_endpoint);
|
||||
self.inject_connected(peer_id, new_endpoint);
|
||||
}
|
||||
|
||||
/// Informs the behaviour about an event generated by the handler dedicated to the peer identified by `peer_id`.
|
||||
/// for the behaviour.
|
||||
///
|
||||
/// The `peer_id` is guaranteed to be in a connected state. In other words, `inject_connected`
|
||||
/// has previously been called with this `PeerId`.
|
||||
fn inject_node_event(
|
||||
fn inject_event(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
connection: ConnectionId,
|
||||
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
|
||||
);
|
||||
|
||||
@ -218,18 +206,27 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
|
||||
peer_id: PeerId,
|
||||
},
|
||||
|
||||
/// Instructs the `Swarm` to send a message to the handler dedicated to the connection with the peer.
|
||||
/// Instructs the `Swarm` to send an event to the handler dedicated to a
|
||||
/// connection with a peer.
|
||||
///
|
||||
/// If the `Swarm` is connected to the peer, the message is delivered to the remote's
|
||||
/// protocol handler. If there is no connection to the peer, the message is ignored.
|
||||
/// To ensure delivery, the `NetworkBehaviour` must keep track of connected peers.
|
||||
/// If the `Swarm` is connected to the peer, the message is delivered to the
|
||||
/// `ProtocolsHandler` instance identified by the peer ID and connection ID.
|
||||
///
|
||||
/// If the specified connection no longer exists, the event is silently dropped.
|
||||
///
|
||||
/// Typically the connection ID given is the same as the one passed to
|
||||
/// [`NetworkBehaviour::inject_event`], i.e. whenever the behaviour wishes to
|
||||
/// respond to a request on the same connection (and possibly the same
|
||||
/// substream, as per the implementation of `ProtocolsHandler`).
|
||||
///
|
||||
/// Note that even if the peer is currently connected, connections can get closed
|
||||
/// at any time and thus the message may not reach its destination.
|
||||
SendEvent {
|
||||
/// The peer to which to send the message.
|
||||
/// at any time and thus the event may not reach a handler.
|
||||
NotifyHandler {
|
||||
/// The peer for whom a `ProtocolsHandler` should be notified.
|
||||
peer_id: PeerId,
|
||||
/// The message to send.
|
||||
/// The ID of the connection whose `ProtocolsHandler` to notify.
|
||||
handler: NotifyHandler,
|
||||
/// The event to send.
|
||||
event: TInEvent,
|
||||
},
|
||||
|
||||
@ -244,3 +241,15 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
|
||||
address: Multiaddr,
|
||||
},
|
||||
}
|
||||
|
||||
/// The options w.r.t. which connection handlers to notify of an event.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NotifyHandler {
|
||||
/// Notify a particular connection handler.
|
||||
One(ConnectionId),
|
||||
/// Notify an arbitrary connection handler.
|
||||
Any,
|
||||
/// Notify all connection handlers.
|
||||
All
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user