core/: Concurrent dial attempts (#2248)

Concurrently dial address candidates within a single dial attempt.

Main motivation for this feature is to increase success rate on hole punching
(see https://github.com/libp2p/rust-libp2p/issues/1896#issuecomment-885894496
for details). Though, as a nice side effect, as one would expect, it does
improve connection establishment time.

Cleanups and fixes done along the way:

- Merge `pool.rs` and `manager.rs`.

- Instead of manually implementing state machines in `task.rs` use
  `async/await`.

- Fix bug where `NetworkBehaviour::inject_connection_closed` is called without a
  previous `NetworkBehaviour::inject_connection_established` (see
  https://github.com/libp2p/rust-libp2p/issues/2242).

- Return handler to behaviour on incoming connection limit error. Missed in
  https://github.com/libp2p/rust-libp2p/issues/2242.
This commit is contained in:
Max Inden
2021-10-14 18:05:07 +02:00
committed by GitHub
parent c0d7d4a9eb
commit 40c5335e3b
36 changed files with 2242 additions and 2319 deletions

View File

@ -36,8 +36,8 @@ use libp2p_plaintext::PlainText2Config;
use libp2p_relay::{Relay, RelayConfig};
use libp2p_swarm::protocols_handler::KeepAlive;
use libp2p_swarm::{
DummyBehaviour, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
PollParameters, Swarm, SwarmEvent,
DialError, DummyBehaviour, NetworkBehaviour, NetworkBehaviourAction,
NetworkBehaviourEventProcess, PollParameters, Swarm, SwarmEvent,
};
use std::task::{Context, Poll};
use std::time::Duration;
@ -391,10 +391,11 @@ fn src_try_connect_to_offline_dst() {
loop {
match src_swarm.select_next_some().await {
SwarmEvent::UnreachableAddr {
address, peer_id, ..
} if address == dst_addr_via_relay => {
assert_eq!(peer_id, dst_peer_id);
SwarmEvent::OutgoingConnectionError {
error: DialError::Transport(addresses),
peer_id,
} if *addresses.iter().map(|(a, _)| a).next().unwrap() == dst_addr_via_relay => {
assert_eq!(peer_id, Some(dst_peer_id));
break;
}
SwarmEvent::Behaviour(CombinedEvent::Ping(_)) => {}
@ -448,10 +449,11 @@ fn src_try_connect_to_unsupported_dst() {
loop {
match src_swarm.select_next_some().await {
SwarmEvent::UnreachableAddr {
address, peer_id, ..
} if address == dst_addr_via_relay => {
assert_eq!(peer_id, dst_peer_id);
SwarmEvent::OutgoingConnectionError {
error: DialError::Transport(addresses),
peer_id,
} if *addresses.iter().map(|(a, _)| a).next().unwrap() == dst_addr_via_relay => {
assert_eq!(peer_id, Some(dst_peer_id));
break;
}
SwarmEvent::ConnectionClosed { peer_id, .. } if peer_id == relay_peer_id => {}
@ -492,16 +494,18 @@ fn src_try_connect_to_offline_dst_via_offline_relay() {
// Source Node fail to reach Relay.
match src_swarm.select_next_some().await {
SwarmEvent::UnreachableAddr { peer_id, .. } if peer_id == relay_peer_id => {}
SwarmEvent::OutgoingConnectionError { peer_id, .. }
if peer_id == Some(relay_peer_id) => {}
e => panic!("{:?}", e),
}
// Source Node fail to reach Destination Node due to failure reaching Relay.
match src_swarm.select_next_some().await {
SwarmEvent::UnreachableAddr {
address, peer_id, ..
} if address == dst_addr_via_relay => {
assert_eq!(peer_id, dst_peer_id);
SwarmEvent::OutgoingConnectionError {
error: DialError::Transport(addresses),
peer_id,
} if *addresses.iter().map(|(a, _)| a).next().unwrap() == dst_addr_via_relay => {
assert_eq!(peer_id, Some(dst_peer_id));
}
e => panic!("{:?}", e),
}
@ -1063,10 +1067,13 @@ fn yield_incoming_connection_through_correct_listener() {
e => panic!("{:?}", e),
},
event = src_3_swarm.select_next_some() => match event {
SwarmEvent::UnreachableAddr { address, peer_id, .. }
if address == dst_addr_via_relay_3 =>
SwarmEvent::OutgoingConnectionError {
error: DialError::Transport(addresses),
peer_id,
} if *addresses.iter().map(|(a, _)| a).next().unwrap()
== dst_addr_via_relay_3 =>
{
assert_eq!(peer_id, dst_peer_id);
assert_eq!(peer_id, Some(dst_peer_id));
break;
}
SwarmEvent::Dialing { .. } => {}