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

@ -220,6 +220,7 @@ impl NetworkBehaviour for Identify {
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
) {
let addr = match endpoint {
ConnectedPoint::Dialer { address } => address.clone(),
@ -230,6 +231,16 @@ impl NetworkBehaviour for Identify {
.entry(*peer_id)
.or_default()
.insert(*conn, addr);
if let Some(entry) = self.discovered_peers.get_mut(peer_id) {
for addr in failed_addresses
.into_iter()
.map(|addresses| addresses.into_iter())
.flatten()
{
entry.remove(addr);
}
}
}
fn inject_connection_closed(
@ -244,9 +255,24 @@ impl NetworkBehaviour for Identify {
}
}
fn inject_dial_failure(&mut self, peer_id: &PeerId, _: Self::ProtocolsHandler, _: DialError) {
if !self.connected.contains_key(peer_id) {
self.pending_push.remove(peer_id);
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
_: Self::ProtocolsHandler,
error: &DialError,
) {
if let Some(peer_id) = peer_id {
if !self.connected.contains_key(&peer_id) {
self.pending_push.remove(&peer_id);
}
}
if let Some(entry) = peer_id.and_then(|id| self.discovered_peers.get_mut(&id)) {
if let DialError::Transport(errors) = error {
for (addr, _error) in errors {
entry.remove(addr);
}
}
}
}
@ -421,19 +447,6 @@ impl NetworkBehaviour for Identify {
.map(|addr| Vec::from_iter(addr))
.unwrap_or_default()
}
fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
_: &dyn std::error::Error,
) {
if let Some(peer) = peer_id {
if let Some(entry) = self.discovered_peers.get_mut(peer) {
entry.remove(addr);
}
}
}
}
/// Event emitted by the `Identify` behaviour.