swarm/: Rename ProtocolsHandler to ConnectionHandler (#2527)

A `ProtocolsHandler`, now `ConnectionHandler`, handels a connection, not
a protocol. Thus the name `CONNECTIONHandler` is more appropriate.

Next to the rename of `ProtocolsHandler` this commit renames the `mod
protocols_handler` to `mod handler`. Finally all combinators (e.g.
`ProtocolsHandlerSelect`) are renamed appropriately.
This commit is contained in:
Max Inden
2022-02-21 13:32:24 +01:00
committed by GitHub
parent 6511e6ba45
commit fd2be38faf
50 changed files with 924 additions and 910 deletions

View File

@ -48,7 +48,7 @@
//!
//! # Protocols Handler
//!
//! The [`ProtocolsHandler`] trait defines how each active connection to a
//! The [`ConnectionHandler`] trait defines how each active connection to a
//! remote should behave: how to handle incoming substreams, which protocols
//! are supported, when to open a new outbound substream, etc.
//!
@ -61,7 +61,7 @@ mod upgrade;
pub mod behaviour;
pub mod dial_opts;
pub mod protocols_handler;
pub mod handler;
pub use behaviour::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
@ -71,10 +71,10 @@ pub use connection::{
ConnectionCounters, ConnectionError, ConnectionLimit, ConnectionLimits, PendingConnectionError,
PendingInboundConnectionError, PendingOutboundConnectionError,
};
pub use protocols_handler::{
IntoProtocolsHandler, IntoProtocolsHandlerSelect, KeepAlive, OneShotHandler,
OneShotHandlerConfig, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerSelect,
ProtocolsHandlerUpgrErr, SubstreamProtocol,
pub use handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerSelect, ConnectionHandlerUpgrErr,
IntoConnectionHandler, IntoConnectionHandlerSelect, KeepAlive, OneShotHandler,
OneShotHandlerConfig, SubstreamProtocol,
};
pub use registry::{AddAddressResult, AddressRecord, AddressScore};
@ -115,22 +115,22 @@ pub type NegotiatedSubstream = Negotiated<Substream<StreamMuxerBox>>;
/// Event generated by the [`NetworkBehaviour`] that the swarm will report back.
type TBehaviourOutEvent<TBehaviour> = <TBehaviour as NetworkBehaviour>::OutEvent;
/// [`ProtocolsHandler`] of the [`NetworkBehaviour`] for all the protocols the [`NetworkBehaviour`]
/// [`ConnectionHandler`] of the [`NetworkBehaviour`] for all the protocols the [`NetworkBehaviour`]
/// supports.
type THandler<TBehaviour> = <TBehaviour as NetworkBehaviour>::ProtocolsHandler;
type THandler<TBehaviour> = <TBehaviour as NetworkBehaviour>::ConnectionHandler;
/// Custom event that can be received by the [`ProtocolsHandler`] of the
/// Custom event that can be received by the [`ConnectionHandler`] of the
/// [`NetworkBehaviour`].
type THandlerInEvent<TBehaviour> =
<<THandler<TBehaviour> as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent;
<<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
/// Custom event that can be produced by the [`ProtocolsHandler`] of the [`NetworkBehaviour`].
/// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].
type THandlerOutEvent<TBehaviour> =
<<THandler<TBehaviour> as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent;
<<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
/// Custom error that can be produced by the [`ProtocolsHandler`] of the [`NetworkBehaviour`].
/// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].
type THandlerErr<TBehaviour> =
<<THandler<TBehaviour> as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error;
<<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;
/// Event generated by the `Swarm`.
#[derive(Debug)]
@ -370,7 +370,7 @@ where
fn dial_with_handler(
&mut self,
swarm_dial_opts: DialOpts,
handler: <TBehaviour as NetworkBehaviour>::ProtocolsHandler,
handler: <TBehaviour as NetworkBehaviour>::ConnectionHandler,
) -> Result<(), DialError> {
let (peer_id, addresses, dial_concurrency_factor_override, role_override) =
match swarm_dial_opts.0 {
@ -604,11 +604,11 @@ where
/// Returns `Ok(())` if there was one or more established connections to the peer.
///
/// Note: Closing a connection via [`Swarm::disconnect_peer_id`] does
/// not inform the corresponding [`ProtocolsHandler`].
/// Closing a connection via a [`ProtocolsHandler`] can be done either in a
/// collaborative manner across [`ProtocolsHandler`]s
/// with [`ProtocolsHandler::connection_keep_alive`] or directly with
/// [`ProtocolsHandlerEvent::Close`].
/// not inform the corresponding [`ConnectionHandler`].
/// Closing a connection via a [`ConnectionHandler`] can be done either in a
/// collaborative manner across [`ConnectionHandler`]s
/// with [`ConnectionHandler::connection_keep_alive`] or directly with
/// [`ConnectionHandlerEvent::Close`].
pub fn disconnect_peer_id(&mut self, peer_id: PeerId) -> Result<(), ()> {
let was_connected = self.pool.is_connected(peer_id);
self.pool.disconnect(peer_id);
@ -1120,8 +1120,8 @@ where
TTrans: Transport,
TTrans::Error: Send + 'static,
TBehaviour: NetworkBehaviour,
THandler: IntoProtocolsHandler,
THandler::Handler: ProtocolsHandler<
THandler: IntoConnectionHandler,
THandler::Handler: ConnectionHandler<
InEvent = THandlerInEvent<TBehaviour>,
OutEvent = THandlerOutEvent<TBehaviour>,
>,
@ -1253,7 +1253,7 @@ where
}
/// Configures the number of events from the [`NetworkBehaviour`] in
/// destination to the [`ProtocolsHandler`] that can be buffered before
/// destination to the [`ConnectionHandler`] that can be buffered before
/// the [`Swarm`] has to wait. An individual buffer with this number of
/// events exists for each individual connection.
///
@ -1266,14 +1266,14 @@ where
self
}
/// Configures the number of extra events from the [`ProtocolsHandler`] in
/// Configures the number of extra events from the [`ConnectionHandler`] in
/// destination to the [`NetworkBehaviour`] that can be buffered before
/// the [`ProtocolsHandler`] has to go to sleep.
/// the [`ConnectionHandler`] has to go to sleep.
///
/// There exists a buffer of events received from [`ProtocolsHandler`]s
/// There exists a buffer of events received from [`ConnectionHandler`]s
/// that the [`NetworkBehaviour`] has yet to process. This buffer is
/// shared between all instances of [`ProtocolsHandler`]. Each instance of
/// [`ProtocolsHandler`] is guaranteed one slot in this buffer, meaning
/// shared between all instances of [`ConnectionHandler`]. Each instance of
/// [`ConnectionHandler`] is guaranteed one slot in this buffer, meaning
/// that delivering an event for the first time is guaranteed to be
/// instantaneous. Any extra event delivery, however, must wait for that
/// first event to be delivered or for an "extra slot" to be available.
@ -1284,7 +1284,7 @@ where
///
/// The ideal value depends on the executor used, the CPU speed, the
/// average number of connections, and the volume of events. If this value
/// is too low, then the [`ProtocolsHandler`]s will be sleeping more often
/// is too low, then the [`ConnectionHandler`]s will be sleeping more often
/// than necessary. Increasing this value increases the overall memory
/// usage, and more importantly the latency between the moment when an
/// event is emitted and the moment when it is received by the
@ -1478,11 +1478,11 @@ impl Default for DummyBehaviour {
}
impl NetworkBehaviour for DummyBehaviour {
type ProtocolsHandler = protocols_handler::DummyProtocolsHandler;
type ConnectionHandler = handler::DummyConnectionHandler;
type OutEvent = void::Void;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
protocols_handler::DummyProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
handler::DummyConnectionHandler {
keep_alive: self.keep_alive,
}
}
@ -1491,7 +1491,7 @@ impl NetworkBehaviour for DummyBehaviour {
&mut self,
_: PeerId,
_: ConnectionId,
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
event: <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
) {
void::unreachable(event)
}
@ -1500,7 +1500,7 @@ impl NetworkBehaviour for DummyBehaviour {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
Poll::Pending
}
}
@ -1530,7 +1530,7 @@ impl NetworkInfo {
#[cfg(test)]
mod tests {
use super::*;
use crate::protocols_handler::DummyProtocolsHandler;
use crate::handler::DummyConnectionHandler;
use crate::test::{CallTraceBehaviour, MockBehaviour};
use futures::executor::block_on;
use futures::future::poll_fn;
@ -1557,7 +1557,7 @@ mod tests {
handler_proto: T,
) -> SwarmBuilder<CallTraceBehaviour<MockBehaviour<T, O>>>
where
T: ProtocolsHandler + Clone,
T: ConnectionHandler + Clone,
T::OutEvent: Clone,
O: Send + 'static,
{
@ -1581,7 +1581,7 @@ mod tests {
) -> bool
where
TBehaviour: NetworkBehaviour,
<<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone,
<<TBehaviour::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent: Clone,
{
[swarm1, swarm2]
.iter()
@ -1595,7 +1595,7 @@ mod tests {
) -> bool
where
TBehaviour: NetworkBehaviour,
<<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone
<<TBehaviour::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent: Clone
{
[swarm1, swarm2]
.iter()
@ -1621,7 +1621,7 @@ mod tests {
fn test_connect_disconnect_ban() {
// Since the test does not try to open any substreams, we can
// use the dummy protocols handler.
let handler_proto = DummyProtocolsHandler {
let handler_proto = DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
};
@ -1742,7 +1742,7 @@ mod tests {
fn test_swarm_disconnect() {
// Since the test does not try to open any substreams, we can
// use the dummy protocols handler.
let handler_proto = DummyProtocolsHandler {
let handler_proto = DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
};
@ -1811,7 +1811,7 @@ mod tests {
fn test_behaviour_disconnect_all() {
// Since the test does not try to open any substreams, we can
// use the dummy protocols handler.
let handler_proto = DummyProtocolsHandler {
let handler_proto = DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
};
@ -1883,7 +1883,7 @@ mod tests {
fn test_behaviour_disconnect_one() {
// Since the test does not try to open any substreams, we can
// use the dummy protocols handler.
let handler_proto = DummyProtocolsHandler {
let handler_proto = DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
};
@ -1970,7 +1970,7 @@ mod tests {
fn prop(concurrency_factor: DialConcurrencyFactor) {
block_on(async {
let mut swarm = new_test_swarm::<_, ()>(DummyProtocolsHandler {
let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
})
.dial_concurrency_factor(concurrency_factor.0)
@ -2039,7 +2039,7 @@ mod tests {
let outgoing_limit = rand::thread_rng().gen_range(1, 10);
let limits = ConnectionLimits::default().with_max_pending_outgoing(Some(outgoing_limit));
let mut network = new_test_swarm::<_, ()>(DummyProtocolsHandler {
let mut network = new_test_swarm::<_, ()>(DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
})
.connection_limits(limits)
@ -2102,12 +2102,12 @@ mod tests {
fn prop(limit: Limit) {
let limit = limit.0;
let mut network1 = new_test_swarm::<_, ()>(DummyProtocolsHandler {
let mut network1 = new_test_swarm::<_, ()>(DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
})
.connection_limits(limits(limit))
.build();
let mut network2 = new_test_swarm::<_, ()>(DummyProtocolsHandler {
let mut network2 = new_test_swarm::<_, ()>(DummyConnectionHandler {
keep_alive: KeepAlive::Yes,
})
.connection_limits(limits(limit))
@ -2216,8 +2216,8 @@ mod tests {
// Checks whether dialing an address containing the wrong peer id raises an error
// for the expected peer id instead of the obtained peer id.
let mut swarm1 = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut swarm2 = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut swarm1 = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let mut swarm2 = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
swarm1.listen_on("/memory/0".parse().unwrap()).unwrap();
@ -2276,7 +2276,7 @@ mod tests {
//
// The last two can happen in any order.
let mut swarm = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
swarm.listen_on("/memory/0".parse().unwrap()).unwrap();
let local_address =
@ -2334,7 +2334,7 @@ mod tests {
fn dial_self_by_id() {
// Trying to dial self by passing the same `PeerId` shouldn't even be possible in the first
// place.
let swarm = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let peer_id = *swarm.local_peer_id();
assert!(!swarm.is_connected(&peer_id));
}
@ -2345,7 +2345,7 @@ mod tests {
let target = PeerId::random();
let mut swarm = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let mut addresses = Vec::new();
for _ in 0..3 {
@ -2399,8 +2399,8 @@ mod tests {
fn aborting_pending_connection_surfaces_error() {
let _ = env_logger::try_init();
let mut dialer = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut listener = new_test_swarm::<_, ()>(DummyProtocolsHandler::default()).build();
let mut dialer = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let mut listener = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let listener_peer_id = *listener.local_peer_id();
listener.listen_on(multiaddr![Memory(0u64)]).unwrap();