Skip to content

Commit

Permalink
Make the SwarmEvent report everything (#1515)
Browse files Browse the repository at this point in the history
* Improve the SwarmEvent to report everything

* Address review

* Update swarm/src/lib.rs

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
  • Loading branch information
tomaka and romanb committed Mar 26, 2020
1 parent 28ea62d commit 7220877
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 54 deletions.
13 changes: 7 additions & 6 deletions core/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use either::Either;
use fnv::FnvHashMap;
use futures::prelude::*;
use smallvec::SmallVec;
use std::{error, fmt, hash::Hash, task::Context, task::Poll};
use std::{convert::TryFrom as _, error, fmt, hash::Hash, num::NonZeroU32, task::Context, task::Poll};

/// A connection `Pool` manages a set of connections for each peer.
pub struct Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo = PeerId, TPeerId = PeerId> {
Expand Down Expand Up @@ -86,7 +86,7 @@ pub enum PoolEvent<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TC
/// A new connection has been established.
ConnectionEstablished {
connection: EstablishedConnection<'a, TInEvent, TConnInfo, TPeerId>,
num_established: usize,
num_established: NonZeroU32,
},

/// An established connection has encountered an error.
Expand All @@ -99,7 +99,7 @@ pub enum PoolEvent<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TC
/// A reference to the pool that used to manage the connection.
pool: &'a mut Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId>,
/// The remaining number of established connections to the same peer.
num_established: usize,
num_established: u32,
},

/// A connection attempt failed.
Expand Down Expand Up @@ -580,7 +580,7 @@ where
let num_established =
if let Some(conns) = self.established.get_mut(connected.peer_id()) {
conns.remove(&id);
conns.len()
u32::try_from(conns.len()).unwrap()
} else {
0
};
Expand All @@ -600,7 +600,7 @@ where
.map_or(0, |conns| conns.len());
if let Err(e) = self.limits.check_established(current) {
let connected = entry.close();
let num_established = e.current;
let num_established = u32::try_from(e.current).unwrap();
return Poll::Ready(PoolEvent::ConnectionError {
id,
connected,
Expand All @@ -623,7 +623,8 @@ where
// Add the connection to the pool.
let peer = entry.connected().peer_id().clone();
let conns = self.established.entry(peer).or_default();
let num_established = conns.len() + 1;
let num_established = NonZeroU32::new(u32::try_from(conns.len() + 1).unwrap())
.expect("n + 1 is always non-zero; qed");
conns.insert(id, endpoint);
match self.get(id) {
Some(PoolConnection::Established(connection)) =>
Expand Down
3 changes: 2 additions & 1 deletion core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use fnv::{FnvHashMap};
use futures::{prelude::*, future};
use std::{
collections::hash_map,
convert::TryFrom as _,
error,
fmt,
hash::Hash,
Expand Down Expand Up @@ -517,7 +518,7 @@ where
// A pending outgoing connection to a known peer failed.
let mut attempt = dialing.remove(&peer_id).expect("by (1)");

let num_remain = attempt.next.len();
let num_remain = u32::try_from(attempt.next.len()).unwrap();
let failed_addr = attempt.current.clone();

let opts =
Expand Down
13 changes: 7 additions & 6 deletions core/src/network/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
transport::{Transport, TransportError},
};
use futures::prelude::*;
use std::{error, fmt, hash::Hash};
use std::{error, fmt, hash::Hash, num::NonZeroU32};

/// Event that can happen on the `Network`.
pub enum NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
Expand Down Expand Up @@ -88,7 +88,7 @@ where
/// A new connection arrived on a listener.
IncomingConnection(IncomingConnectionEvent<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>),

/// A new connection was arriving on a listener, but an error happened when negotiating it.
/// An error happened on a connection during its initial handshake.
///
/// This can include, for example, an error during the handshake of the encryption layer, or
/// the connection unexpectedly closed.
Expand All @@ -105,8 +105,9 @@ where
ConnectionEstablished {
/// The newly established connection.
connection: EstablishedConnection<'a, TInEvent, TConnInfo, TPeerId>,
/// The total number of established connections to the same peer.
num_established: usize,
/// The total number of established connections to the same peer, including the one that
/// has just been opened.
num_established: NonZeroU32,
},

/// An established connection to a peer has encountered an error.
Expand All @@ -118,13 +119,13 @@ where
/// The error that occurred.
error: ConnectionError<<THandler::Handler as ConnectionHandler>::Error>,
/// The remaining number of established connections to the same peer.
num_established: usize,
num_established: u32,
},

/// A dialing attempt to an address of a peer failed.
DialError {
/// The number of remaining dialing attempts.
attempts_remaining: usize,
attempts_remaining: u32,

/// Id of the peer we were trying to dial.
peer_id: TPeerId,
Expand Down
2 changes: 1 addition & 1 deletion core/tests/network_dial_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn multiple_addresses_err() {
assert_eq!(attempts_remaining, 0);
return Poll::Ready(Ok(()));
} else {
assert_eq!(attempts_remaining, addresses.len());
assert_eq!(attempts_remaining, addresses.len() as u32);
}
},
Poll::Ready(_) => unreachable!(),
Expand Down
Loading

0 comments on commit 7220877

Please sign in to comment.