swarm/pool: Misc refactoring (#3073)

* Remove unreachable error case

Instead of taking the connection out of the map again, construct
the event to be returned with the data we already have available.

* Remove `Pool::get` and `PoolConnection`

These are effectively not used.

* Replace `iter_pending_info` with its only usage: `is_dialing`

* Add `is_for_same_remote_as` convenience function

* Remove `PendingConnection`

* Rename `PendingConnectionInfo` to `PendingConnection`

With the latter being gone, the name is now free.

* Merge `EstablishedConnectionInfo` and `EstablishedConnection`

This is a leftover from when `Pool` was still in `libp2p-core` and
one of them was a public API and the other one wasn't.

All of this is private to `libp2p-swarm` so we no longer need to
differentiate.

* Don't `pub use` out of `pub(crate)` modules
This commit is contained in:
Thomas Eizinger
2022-11-03 05:47:00 +11:00
committed by GitHub
parent 64e38bd6b4
commit f4ce1fe9ae
3 changed files with 85 additions and 189 deletions

View File

@ -70,9 +70,10 @@ pub mod keep_alive;
pub use behaviour::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
pub use connection::pool::{ConnectionCounters, ConnectionLimits};
pub use connection::{
ConnectionCounters, ConnectionError, ConnectionLimit, ConnectionLimits, PendingConnectionError,
PendingInboundConnectionError, PendingOutboundConnectionError,
ConnectionError, ConnectionLimit, PendingConnectionError, PendingInboundConnectionError,
PendingOutboundConnectionError,
};
pub use handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerSelect, ConnectionHandlerUpgrErr,
@ -81,12 +82,12 @@ pub use handler::{
};
pub use registry::{AddAddressResult, AddressRecord, AddressScore};
use connection::pool::{Pool, PoolConfig, PoolEvent};
use connection::{EstablishedConnection, IncomingInfo};
use connection::pool::{EstablishedConnection, Pool, PoolConfig, PoolEvent};
use connection::IncomingInfo;
use dial_opts::{DialOpts, PeerCondition};
use either::Either;
use futures::{executor::ThreadPoolBuilder, prelude::*, stream::FusedStream};
use libp2p_core::connection::{ConnectionId, PendingPoint};
use libp2p_core::connection::ConnectionId;
use libp2p_core::muxing::SubstreamBox;
use libp2p_core::{
connection::ConnectedPoint,
@ -395,15 +396,7 @@ where
// Check [`PeerCondition`] if provided.
let condition_matched = match condition {
PeerCondition::Disconnected => !self.is_connected(&peer_id),
PeerCondition::NotDialing => {
!self
.pool
.iter_pending_info()
.any(move |(_, endpoint, peer)| {
matches!(endpoint, PendingPoint::Dialer { .. })
&& peer.as_ref() == Some(&peer_id)
})
}
PeerCondition::NotDialing => !self.pool.is_dialing(peer_id),
PeerCondition::Always => true,
};
if !condition_matched {
@ -1042,7 +1035,7 @@ where
Some((peer_id, handler, event)) => match handler {
PendingNotifyHandler::One(conn_id) => {
match this.pool.get_established(conn_id) {
Some(mut conn) => match notify_one(&mut conn, event, cx) {
Some(conn) => match notify_one(conn, event, cx) {
None => continue,
Some(event) => {
this.pending_event = Some((peer_id, handler, event));
@ -1135,8 +1128,8 @@ enum PendingNotifyHandler {
///
/// Returns `None` if the connection is closing or the event has been
/// successfully sent, in either case the event is consumed.
fn notify_one<'a, THandlerInEvent>(
conn: &mut EstablishedConnection<'a, THandlerInEvent>,
fn notify_one<THandlerInEvent>(
conn: &mut EstablishedConnection<THandlerInEvent>,
event: THandlerInEvent,
cx: &mut Context<'_>,
) -> Option<THandlerInEvent> {
@ -1180,7 +1173,7 @@ where
let mut pending = SmallVec::new();
let mut event = Some(event); // (1)
for id in ids.into_iter() {
if let Some(mut conn) = pool.get_established(id) {
if let Some(conn) = pool.get_established(id) {
match conn.poll_ready_notify_handler(cx) {
Poll::Pending => pending.push(id),
Poll::Ready(Err(())) => {} // connection is closing