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

@@ -8,8 +8,11 @@
- Update to `libp2p-swarm` `v0.40.0`.
- Deprecate `Config::with_keep_alive`. See [PR 2859].
[PR 2857]: https://github.com/libp2p/rust-libp2p/pull/2857
[PR 2937]: https://github.com/libp2p/rust-libp2p/pull/2937
[PR 2859]: https://github.com/libp2p/rust-libp2p/pull/2859/
# 0.39.0

View File

@@ -111,6 +111,10 @@ impl Config {
/// If the maximum number of allowed ping failures is reached, the
/// connection is always terminated as a result of [`ConnectionHandler::poll`]
/// returning an error, regardless of the keep-alive setting.
#[deprecated(
since = "0.40.0",
note = "Use `libp2p::swarm::behaviour::KeepAlive` if you need to keep connections alive unconditionally."
)]
pub fn with_keep_alive(mut self, b: bool) -> Self {
self.keep_alive = b;
self

View File

@@ -30,24 +30,24 @@ use libp2p::core::{
use libp2p::mplex;
use libp2p::noise;
use libp2p::ping;
use libp2p::swarm::{DummyBehaviour, KeepAlive, Swarm, SwarmEvent};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::tcp::{GenTcpConfig, TcpTransport};
use libp2p::yamux;
use libp2p::NetworkBehaviour;
use libp2p_swarm::keep_alive;
use quickcheck::*;
use std::{num::NonZeroU8, time::Duration};
#[test]
fn ping_pong() {
fn prop(count: NonZeroU8, muxer: MuxerChoice) {
let cfg = ping::Config::new()
.with_keep_alive(true)
.with_interval(Duration::from_millis(10));
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
let (peer1_id, trans) = mk_transport(muxer);
let mut swarm1 = Swarm::new(trans, ping::Behaviour::new(cfg.clone()), peer1_id);
let mut swarm1 = Swarm::new(trans, Behaviour::new(cfg.clone()), peer1_id);
let (peer2_id, trans) = mk_transport(muxer);
let mut swarm2 = Swarm::new(trans, ping::Behaviour::new(cfg), peer2_id);
let mut swarm2 = Swarm::new(trans, Behaviour::new(cfg), peer2_id);
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
@@ -62,16 +62,19 @@ fn ping_pong() {
loop {
match swarm1.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => tx.send(address).await.unwrap(),
SwarmEvent::Behaviour(ping::Event {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
peer,
result: Ok(ping::Success::Ping { rtt }),
}) => {
})) => {
count1 -= 1;
if count1 == 0 {
return (pid1, peer, rtt);
}
}
SwarmEvent::Behaviour(ping::Event { result: Err(e), .. }) => {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Err(e),
..
})) => {
panic!("Ping failure: {:?}", e)
}
_ => {}
@@ -85,16 +88,19 @@ fn ping_pong() {
loop {
match swarm2.select_next_some().await {
SwarmEvent::Behaviour(ping::Event {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
peer,
result: Ok(ping::Success::Ping { rtt }),
}) => {
})) => {
count2 -= 1;
if count2 == 0 {
return (pid2, peer, rtt);
}
}
SwarmEvent::Behaviour(ping::Event { result: Err(e), .. }) => {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Err(e),
..
})) => {
panic!("Ping failure: {:?}", e)
}
_ => {}
@@ -117,16 +123,15 @@ fn ping_pong() {
fn max_failures() {
fn prop(max_failures: NonZeroU8, muxer: MuxerChoice) {
let cfg = ping::Config::new()
.with_keep_alive(true)
.with_interval(Duration::from_millis(10))
.with_timeout(Duration::from_millis(0))
.with_max_failures(max_failures.into());
let (peer1_id, trans) = mk_transport(muxer);
let mut swarm1 = Swarm::new(trans, ping::Behaviour::new(cfg.clone()), peer1_id);
let mut swarm1 = Swarm::new(trans, Behaviour::new(cfg.clone()), peer1_id);
let (peer2_id, trans) = mk_transport(muxer);
let mut swarm2 = Swarm::new(trans, ping::Behaviour::new(cfg), peer2_id);
let mut swarm2 = Swarm::new(trans, Behaviour::new(cfg), peer2_id);
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
@@ -139,13 +144,16 @@ fn max_failures() {
loop {
match swarm1.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => tx.send(address).await.unwrap(),
SwarmEvent::Behaviour(ping::Event {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Ok(ping::Success::Ping { .. }),
..
}) => {
})) => {
count1 = 0; // there may be an occasional success
}
SwarmEvent::Behaviour(ping::Event { result: Err(_), .. }) => {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Err(_),
..
})) => {
count1 += 1;
}
SwarmEvent::ConnectionClosed { .. } => return count1,
@@ -161,13 +169,16 @@ fn max_failures() {
loop {
match swarm2.select_next_some().await {
SwarmEvent::Behaviour(ping::Event {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Ok(ping::Success::Ping { .. }),
..
}) => {
})) => {
count2 = 0; // there may be an occasional success
}
SwarmEvent::Behaviour(ping::Event { result: Err(_), .. }) => {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Err(_),
..
})) => {
count2 += 1;
}
SwarmEvent::ConnectionClosed { .. } => return count2,
@@ -187,18 +198,10 @@ fn max_failures() {
#[test]
fn unsupported_doesnt_fail() {
let (peer1_id, trans) = mk_transport(MuxerChoice::Mplex);
let mut swarm1 = Swarm::new(
trans,
DummyBehaviour::with_keep_alive(KeepAlive::Yes),
peer1_id,
);
let mut swarm1 = Swarm::new(trans, keep_alive::Behaviour, peer1_id);
let (peer2_id, trans) = mk_transport(MuxerChoice::Mplex);
let mut swarm2 = Swarm::new(
trans,
ping::Behaviour::new(ping::Config::new().with_keep_alive(true)),
peer2_id,
);
let mut swarm2 = Swarm::new(trans, Behaviour::default(), peer2_id);
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
@@ -218,10 +221,10 @@ fn unsupported_doesnt_fail() {
loop {
match swarm2.select_next_some().await {
SwarmEvent::Behaviour(ping::Event {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Err(ping::Failure::Unsupported),
..
}) => {
})) => {
swarm2.disconnect_peer_id(peer1_id).unwrap();
}
SwarmEvent::ConnectionClosed { cause: Some(e), .. } => {
@@ -265,3 +268,18 @@ impl Arbitrary for MuxerChoice {
*g.choose(&[MuxerChoice::Mplex, MuxerChoice::Yamux]).unwrap()
}
}
#[derive(NetworkBehaviour, Default)]
struct Behaviour {
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}
impl Behaviour {
fn new(config: ping::Config) -> Self {
Self {
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour::new(config),
}
}
}