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

swarm/src/lib: Prioritize Behaviour over Pool and Pool over Listeners #2627

Merged
merged 7 commits into from
May 5, 2022
118 changes: 20 additions & 98 deletions swarm/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ impl<THandler: IntoConnectionHandler, TTrans: Transport> fmt::Debug for Pool<THa
}

/// Event that can happen on the `Pool`.
pub enum PoolEvent<'a, THandler: IntoConnectionHandler, TTrans>
#[derive(Debug)]
pub enum PoolEvent<THandler: IntoConnectionHandler, TTrans>
where
TTrans: Transport,
{
/// A new connection has been established.
ConnectionEstablished {
connection: EstablishedConnection<'a, <THandler::Handler as ConnectionHandler>::InEvent>,
id: ConnectionId,
peer_id: PeerId,
endpoint: ConnectedPoint,
/// List of other connections to the same peer.
///
/// Note: Does not include the connection reported through this event.
Expand Down Expand Up @@ -190,8 +193,6 @@ where
/// The error that occurred, if any. If `None`, the connection
/// was closed by the local peer.
error: Option<ConnectionError<<THandler::Handler as ConnectionHandler>::Error>>,
/// A reference to the pool that used to manage the connection.
pool: &'a mut Pool<THandler, TTrans>,
/// The remaining established connections to the same peer.
remaining_established_connection_ids: Vec<ConnectionId>,
handler: THandler::Handler,
Expand Down Expand Up @@ -225,90 +226,23 @@ where

/// A node has produced an event.
ConnectionEvent {
/// The connection that has generated the event.
connection: EstablishedConnection<'a, THandlerInEvent<THandler>>,
id: ConnectionId,
peer_id: PeerId,
/// The produced event.
event: THandlerOutEvent<THandler>,
},

/// The connection to a node has changed its address.
AddressChange {
/// The connection that has changed address.
connection: EstablishedConnection<'a, THandlerInEvent<THandler>>,
id: ConnectionId,
peer_id: PeerId,
/// The new endpoint.
new_endpoint: ConnectedPoint,
/// The old endpoint.
old_endpoint: ConnectedPoint,
},
}

impl<'a, THandler: IntoConnectionHandler, TTrans> fmt::Debug for PoolEvent<'a, THandler, TTrans>
where
TTrans: Transport,
TTrans::Error: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
PoolEvent::ConnectionEstablished {
connection,
concurrent_dial_errors,
..
} => f
.debug_tuple("PoolEvent::ConnectionEstablished")
.field(connection)
.field(concurrent_dial_errors)
.finish(),
PoolEvent::ConnectionClosed {
id,
connected,
error,
..
} => f
.debug_struct("PoolEvent::ConnectionClosed")
.field("id", id)
.field("connected", connected)
.field("error", error)
.finish(),
PoolEvent::PendingOutboundConnectionError {
id, error, peer, ..
} => f
.debug_struct("PoolEvent::PendingOutboundConnectionError")
.field("id", id)
.field("error", error)
.field("peer", peer)
.finish(),
PoolEvent::PendingInboundConnectionError {
id,
error,
send_back_addr,
local_addr,
..
} => f
.debug_struct("PoolEvent::PendingInboundConnectionError")
.field("id", id)
.field("error", error)
.field("send_back_addr", send_back_addr)
.field("local_addr", local_addr)
.finish(),
PoolEvent::ConnectionEvent { connection, event } => f
.debug_struct("PoolEvent::ConnectionEvent")
.field("peer", &connection.peer_id())
.field("event", event)
.finish(),
PoolEvent::AddressChange {
connection,
new_endpoint,
old_endpoint,
} => f
.debug_struct("PoolEvent::AddressChange")
.field("peer", &connection.peer_id())
.field("new_endpoint", new_endpoint)
.field("old_endpoint", old_endpoint)
.finish(),
}
}
}

impl<THandler, TTrans> Pool<THandler, TTrans>
where
THandler: IntoConnectionHandler,
Expand Down Expand Up @@ -586,7 +520,7 @@ where
///
/// > **Note**: We use a regular `poll` method instead of implementing `Stream`,
/// > because we want the `Pool` to stay borrowed if necessary.
pub fn poll<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll<PoolEvent<'a, THandler, TTrans>>
pub fn poll<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll<PoolEvent<THandler, TTrans>>
where
TTrans: Transport<Output = (PeerId, StreamMuxerBox)>,
THandler: IntoConnectionHandler + 'static,
Expand All @@ -602,16 +536,7 @@ where
Poll::Ready(None) => unreachable!("Pool holds both sender and receiver."),

Poll::Ready(Some(task::EstablishedConnectionEvent::Notify { id, peer_id, event })) => {
let entry = self
.established
.get_mut(&peer_id)
.expect("Receive `Notify` event for established peer.")
.entry(id)
.expect_occupied("Receive `Notify` event from established connection");
return Poll::Ready(PoolEvent::ConnectionEvent {
connection: EstablishedConnection { entry },
event,
});
return Poll::Ready(PoolEvent::ConnectionEvent { peer_id, id, event });
}
Poll::Ready(Some(task::EstablishedConnectionEvent::AddressChange {
id,
Expand All @@ -629,16 +554,12 @@ where
let old_endpoint =
std::mem::replace(&mut connection.endpoint, new_endpoint.clone());

match self.get(id) {
Some(PoolConnection::Established(connection)) => {
return Poll::Ready(PoolEvent::AddressChange {
connection,
new_endpoint,
old_endpoint,
})
}
_ => unreachable!("since `entry` is an `EstablishedEntry`."),
}
return Poll::Ready(PoolEvent::AddressChange {
peer_id,
id,
new_endpoint,
old_endpoint,
});
}
Poll::Ready(Some(task::EstablishedConnectionEvent::Closed {
id,
Expand All @@ -663,7 +584,6 @@ where
connected: Connected { endpoint, peer_id },
error,
remaining_established_connection_ids,
pool: self,
handler,
});
}
Expand Down Expand Up @@ -841,7 +761,9 @@ where
match self.get(id) {
Some(PoolConnection::Established(connection)) => {
return Poll::Ready(PoolEvent::ConnectionEstablished {
connection,
peer_id: connection.peer_id(),
endpoint: connection.endpoint().clone(),
id: connection.id(),
other_established_connection_ids,
concurrent_dial_errors,
})
Expand Down
Loading