swarm: Split off "keep alive" functionality from DummyConnectionHandler (#2859)

Previously, the `DummyConnectionHandler` offered a "keep alive" functionality,
i.e. it allowed users to set the value of what is returned from
`ConnectionHandler::keep_alive`. This handler is primarily used in tests or
`NetworkBehaviour`s that don't open any connections (like mDNS). In all of these
cases, it is statically known whether we want to keep connections alive. As
such, this functionality is better represented by a static
`KeepAliveConnectionHandler` that always returns `KeepAlive::Yes` and a
`DummyConnectionHandler` that always returns `KeepAlive::No`.

To follow the naming conventions described in
https://github.com/libp2p/rust-libp2p/issues/2217, we introduce a top-level
`keep_alive` and `dummy` behaviour in `libp2p-swarm` that contains both the
`NetworkBehaviour` and `ConnectionHandler` implementation for either case.
This commit is contained in:
Thomas Eizinger
2022-10-06 03:50:11 +11:00
committed by GitHub
parent da0403dc45
commit bdf9209824
26 changed files with 412 additions and 319 deletions

View File

@ -35,7 +35,7 @@ use futures::stream::StreamExt;
use libp2p_core::connection::{ConnectedPoint, ConnectionId};
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::handler::DummyConnectionHandler;
use libp2p_swarm::dummy;
use libp2p_swarm::{
ConnectionHandlerUpgrErr, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
@ -144,7 +144,7 @@ impl NetworkBehaviour for Client {
peer_id: &PeerId,
connection_id: &ConnectionId,
endpoint: &ConnectedPoint,
_handler: Either<handler::Handler, DummyConnectionHandler>,
_handler: Either<handler::Handler, dummy::ConnectionHandler>,
_remaining_established: usize,
) {
if !endpoint.is_relayed() {

View File

@ -31,12 +31,10 @@ use instant::Instant;
use libp2p_core::either::EitherError;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::handler::{
DummyConnectionHandler, InboundUpgradeSend, OutboundUpgradeSend, SendWrapper,
};
use libp2p_swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
IntoConnectionHandler, KeepAlive, NegotiatedSubstream, SubstreamProtocol,
};
use log::debug;
use std::collections::{HashMap, VecDeque};
@ -125,7 +123,7 @@ impl Prototype {
}
impl IntoConnectionHandler for Prototype {
type Handler = Either<Handler, DummyConnectionHandler>;
type Handler = Either<Handler, dummy::ConnectionHandler>;
fn into_handler(self, remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
if endpoint.is_relayed() {
@ -138,7 +136,7 @@ impl IntoConnectionHandler for Prototype {
}
// Deny all substreams on relayed connection.
Either::Right(DummyConnectionHandler::default())
Either::Right(dummy::ConnectionHandler)
} else {
let mut handler = Handler {
remote_peer_id: *remote_peer_id,

View File

@ -30,9 +30,8 @@ use instant::Instant;
use libp2p_core::connection::{ConnectedPoint, ConnectionId};
use libp2p_core::multiaddr::Protocol;
use libp2p_core::PeerId;
use libp2p_swarm::handler::DummyConnectionHandler;
use libp2p_swarm::{
ConnectionHandlerUpgrErr, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
dummy, ConnectionHandlerUpgrErr, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
@ -234,7 +233,7 @@ impl NetworkBehaviour for Relay {
peer: &PeerId,
connection: &ConnectionId,
_: &ConnectedPoint,
_handler: Either<handler::Handler, DummyConnectionHandler>,
_handler: Either<handler::Handler, dummy::ConnectionHandler>,
_remaining_established: usize,
) {
if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(*peer) {
@ -283,7 +282,7 @@ impl NetworkBehaviour for Relay {
assert!(
!endpoint.is_relayed(),
"`DummyConnectionHandler` handles relayed connections. It \
"`dummy::ConnectionHandler` handles relayed connections. It \
denies all inbound substreams."
);
@ -410,7 +409,7 @@ impl NetworkBehaviour for Relay {
assert!(
!endpoint.is_relayed(),
"`DummyConnectionHandler` handles relayed connections. It \
"`dummy::ConnectionHandler` handles relayed connections. It \
denies all inbound substreams."
);

View File

@ -33,11 +33,11 @@ use instant::Instant;
use libp2p_core::connection::ConnectionId;
use libp2p_core::either::EitherError;
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::handler::{DummyConnectionHandler, SendWrapper};
use libp2p_swarm::handler::SendWrapper;
use libp2p_swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
IntoConnectionHandler, KeepAlive, NegotiatedSubstream, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::fmt;
@ -342,12 +342,12 @@ pub struct Prototype {
}
impl IntoConnectionHandler for Prototype {
type Handler = Either<Handler, DummyConnectionHandler>;
type Handler = Either<Handler, dummy::ConnectionHandler>;
fn into_handler(self, _remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
if endpoint.is_relayed() {
// Deny all substreams on relayed connection.
Either::Right(DummyConnectionHandler::default())
Either::Right(dummy::ConnectionHandler)
} else {
Either::Left(Handler {
endpoint: endpoint.clone(),