Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat connection limit errors as pending connection errors. #1546

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
14 changes: 7 additions & 7 deletions core/src/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ pub enum ConnectionError<THandlerErr> {
// TODO: Eventually this should also be a custom error?
IO(io::Error),

/// The connection was dropped because the connection limit
/// for a peer has been reached.
ConnectionLimit(ConnectionLimit),

/// The connection handler produced an error.
Handler(THandlerErr),
}
Expand All @@ -48,8 +44,6 @@ where
write!(f, "Connection error: I/O error: {}", err),
ConnectionError::Handler(err) =>
write!(f, "Connection error: Handler error: {}", err),
ConnectionError::ConnectionLimit(l) =>
write!(f, "Connection error: Connection limit: {}.", l)
}
}
}
Expand All @@ -63,7 +57,6 @@ where
match self {
ConnectionError::IO(err) => Some(err),
ConnectionError::Handler(err) => Some(err),
ConnectionError::ConnectionLimit(..) => None,
}
}
}
Expand All @@ -78,6 +71,10 @@ pub enum PendingConnectionError<TTransErr> {
/// match the one that was expected or is otherwise invalid.
InvalidPeerId,

/// The connection was dropped because the connection limit
/// for a peer has been reached.
ConnectionLimit(ConnectionLimit),

/// An I/O error occurred on the connection.
// TODO: Eventually this should also be a custom error?
IO(io::Error),
Expand All @@ -96,6 +93,8 @@ where
write!(f, "Pending connection: Transport error: {}", err),
PendingConnectionError::InvalidPeerId =>
write!(f, "Pending connection: Invalid peer ID."),
PendingConnectionError::ConnectionLimit(l) =>
write!(f, "Connection error: Connection limit: {}.", l),
}
}
}
Expand All @@ -110,6 +109,7 @@ where
PendingConnectionError::IO(err) => Some(err),
PendingConnectionError::Transport(err) => Some(err),
PendingConnectionError::InvalidPeerId => None,
PendingConnectionError::ConnectionLimit(..) => None,
}
}
}
16 changes: 8 additions & 8 deletions core/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub enum PoolEvent<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TC
error: PendingConnectionError<TTransErr>,
/// The handler that was supposed to handle the connection,
/// if the connection failed before the handler was consumed.
handler: THandler,
handler: Option<THandler>,
/// The (expected) peer of the failed connection.
peer: Option<TPeerId>,
/// A reference to the pool that managed the connection.
Expand Down Expand Up @@ -558,7 +558,7 @@ where
id,
endpoint,
error,
handler,
handler: Some(handler),
peer,
pool: self
})
Expand Down Expand Up @@ -588,13 +588,13 @@ where
.map_or(0, |conns| conns.len());
if let Err(e) = self.limits.check_established(current) {
let connected = entry.close();
let num_established = u32::try_from(e.current).unwrap();
return Poll::Ready(PoolEvent::ConnectionError {
return Poll::Ready(PoolEvent::PendingConnectionError {
id,
connected,
error: ConnectionError::ConnectionLimit(e),
num_established,
pool: self,
endpoint: connected.endpoint,
error: PendingConnectionError::ConnectionLimit(e),
handler: None,
peer,
pool: self
})
}
// Peer ID checks must already have happened. See `add_pending`.
Expand Down
32 changes: 19 additions & 13 deletions core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn on_connection_failed<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TP
id: ConnectionId,
endpoint: ConnectedPoint,
error: PendingConnectionError<TTrans::Error>,
handler: THandler,
handler: Option<THandler>,
) -> (Option<DialingOpts<TPeerId, THandler>>, NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>)
where
TTrans: Transport,
Expand All @@ -533,22 +533,29 @@ where
let num_remain = u32::try_from(attempt.next.len()).unwrap();
let failed_addr = attempt.current.clone();

let opts =
let (opts, attempts_remaining) =
if num_remain > 0 {
let next_attempt = attempt.next.remove(0);
let opts = DialingOpts {
peer: peer_id.clone(),
handler,
address: next_attempt,
remaining: attempt.next
};
Some(opts)
if let Some(handler) = handler {
let next_attempt = attempt.next.remove(0);
let opts = DialingOpts {
peer: peer_id.clone(),
handler,
address: next_attempt,
remaining: attempt.next
};
(Some(opts), num_remain)
} else {
// The error is "fatal" for the dialing attempt, since
// the handler was already consumed. All potential
// remaining connection attempts are thus void.
(None, 0)
}
} else {
None
(None, 0)
};

(opts, NetworkEvent::DialError {
attempts_remaining: num_remain,
attempts_remaining,
peer_id,
multiaddr: failed_addr,
error,
Expand All @@ -560,7 +567,6 @@ where
(None, NetworkEvent::UnknownPeerDialError {
multiaddr: address,
error,
handler,
}),
ConnectedPoint::Listener { local_addr, send_back_addr } =>
(None, NetworkEvent::IncomingConnectionError {
Expand Down
4 changes: 0 additions & 4 deletions core/src/network/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ where

/// The error that happened.
error: PendingConnectionError<TTrans::Error>,

/// The handler that was passed to `dial()`, if the
/// connection failed before the handler was consumed.
handler: THandler,
},

/// An established connection produced an event.
Expand Down