Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::{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()
conns.len() as u32
Comment thread
tomaka marked this conversation as resolved.
Outdated
} 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 = e.current as u32;
Comment thread
tomaka marked this conversation as resolved.
Outdated
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(conns.len() as u32 + 1)
.expect("n + 1 is always non-zero; qed");
conns.insert(id, endpoint);
match self.get(id) {
Some(PoolConnection::Established(connection)) =>
Expand Down
2 changes: 1 addition & 1 deletion core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,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 = attempt.next.len() as u32;
Comment thread
tomaka marked this conversation as resolved.
Outdated
let failed_addr = attempt.current.clone();

let opts =
Expand Down
15 changes: 9 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 @@ -98,15 +98,18 @@ where
/// Address used to send back data to the remote.
send_back_addr: Multiaddr,
/// The error that happened.
// TODO: This enum contains `PeerIdMismatch`, which cannot possibly happen for an incoming
// connection. Ideally we should use a different enum.
Comment thread
tomaka marked this conversation as resolved.
Outdated
error: PendingConnectionError<TTrans::Error>,
},

/// A new connection to a peer has been opened.
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 +121,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