Skip to content

Commit

Permalink
feat(swarm): deprecate ConnectionHandlerEvent::Close (#4714)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger authored Oct 24, 2023
1 parent 51070da commit e5cee61
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ libp2p-rendezvous = { version = "0.13.1", path = "protocols/rendezvous" }
libp2p-upnp = { version = "0.1.1", path = "protocols/upnp" }
libp2p-request-response = { version = "0.25.2", path = "protocols/request-response" }
libp2p-server = { version = "0.12.3", path = "misc/server" }
libp2p-swarm = { version = "0.43.6", path = "swarm" }
libp2p-swarm = { version = "0.43.7", path = "swarm" }
libp2p-swarm-derive = { version = "0.33.0", path = "swarm-derive" }
libp2p-swarm-test = { version = "0.2.0", path = "swarm-test" }
libp2p-tcp = { version = "0.40.1", path = "transports/tcp" }
Expand Down
1 change: 1 addition & 0 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ impl<E> From<&libp2p_swarm::ConnectionError<E>> for ConnectionError {
match value {
libp2p_swarm::ConnectionError::IO(_) => ConnectionError::Io,
libp2p_swarm::ConnectionError::KeepAliveTimeout => ConnectionError::KeepAliveTimeout,
#[allow(deprecated)]
libp2p_swarm::ConnectionError::Handler(_) => ConnectionError::Handler,
}
}
Expand Down
7 changes: 6 additions & 1 deletion protocols/dcutr/src/handler/relayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct Handler {
>,
>,
/// Queue of events to return when polled.
#[allow(deprecated)]
queued_events: VecDeque<
ConnectionHandlerEvent<
<Self as ConnectionHandler>::OutboundProtocol,
Expand Down Expand Up @@ -268,6 +269,7 @@ impl ConnectionHandler for Handler {
KeepAlive::No
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand All @@ -282,6 +284,7 @@ impl ConnectionHandler for Handler {
// Check for a pending (fatal) error.
if let Some(err) = self.pending_error.take() {
// The handler will not be polled again by the `Swarm`.
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(err));
}

Expand All @@ -298,7 +301,9 @@ impl ConnectionHandler for Handler {
Event::InboundConnectNegotiated(addresses),
));
}
Err(e) => {
Err(e) =>
{
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Left(e),
)))
Expand Down
2 changes: 2 additions & 0 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl EnabledHandler {
self.outbound_substream = Some(OutboundSubstreamState::WaitingOutput(substream));
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -451,6 +452,7 @@ impl ConnectionHandler for Handler {
}
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ impl ConnectionHandler for Handler {
KeepAlive::No
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ impl ConnectionHandler for Handler {
self.keep_alive
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ impl ConnectionHandler for Handler {
KeepAlive::No
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
6 changes: 6 additions & 0 deletions protocols/relay/src/behaviour/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub struct Handler {
config: Config,

/// Queue of events to return when polled.
#[allow(deprecated)]
queued_events: VecDeque<
ConnectionHandlerEvent<
<Self as ConnectionHandler>::OutboundProtocol,
Expand Down Expand Up @@ -619,6 +620,7 @@ impl ConnectionHandler for Handler {
self.keep_alive
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand All @@ -633,6 +635,7 @@ impl ConnectionHandler for Handler {
// Check for a pending (fatal) error.
if let Some(err) = self.pending_error.take() {
// The handler will not be polled again by the `Swarm`.
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(err));
}

Expand Down Expand Up @@ -712,14 +715,17 @@ impl ConnectionHandler for Handler {
));
}
Poll::Ready(Err(futures_bounded::Timeout { .. })) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout));
}
Poll::Ready(Ok(Either::Left(Err(e)))) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Left(e),
)));
}
Poll::Ready(Ok(Either::Right(Err(e)))) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Right(e),
)));
Expand Down
10 changes: 9 additions & 1 deletion protocols/relay/src/priv_client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub struct Handler {
keep_alive: KeepAlive,

/// Queue of events to return when polled.
#[allow(deprecated)]
queued_events: VecDeque<
ConnectionHandlerEvent<
<Handler as ConnectionHandler>::OutboundProtocol,
Expand Down Expand Up @@ -331,6 +332,7 @@ impl ConnectionHandler for Handler {
self.keep_alive
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand All @@ -345,6 +347,7 @@ impl ConnectionHandler for Handler {
// Check for a pending (fatal) error.
if let Some(err) = self.pending_error.take() {
// The handler will not be polled again by the `Swarm`.
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(err));
}

Expand Down Expand Up @@ -389,12 +392,15 @@ impl ConnectionHandler for Handler {
},
));
}
Poll::Ready(Ok(Err(e))) => {
Poll::Ready(Ok(Err(e))) =>
{
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Right(e),
)))
}
Poll::Ready(Err(Timeout { .. })) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout));
}
Poll::Pending => break,
Expand All @@ -410,6 +416,7 @@ impl ConnectionHandler for Handler {
let res = match worker_res {
Ok(r) => r,
Err(Timeout { .. }) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout));
}
};
Expand Down Expand Up @@ -442,6 +449,7 @@ impl ConnectionHandler for Handler {
}
},
Err(e) => {
#[allow(deprecated)]
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Left(e),
)));
Expand Down
1 change: 1 addition & 0 deletions protocols/request-response/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ where
self.keep_alive
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
11 changes: 8 additions & 3 deletions swarm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use libp2p_core::{
use libp2p_identity::{Keypair, PeerId};
use libp2p_plaintext as plaintext;
use libp2p_swarm::dial_opts::PeerCondition;
use libp2p_swarm::{
self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent, THandlerErr,
};
#[allow(deprecated)]
use libp2p_swarm::THandlerErr;
use libp2p_swarm::{self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_yamux as yamux;
use std::fmt::Debug;
use std::time::Duration;
Expand Down Expand Up @@ -63,6 +63,7 @@ pub trait SwarmExt {
async fn dial_and_wait(&mut self, addr: Multiaddr) -> PeerId;

/// Wait for specified condition to return `Some`.
#[allow(deprecated)]
async fn wait<E, P>(&mut self, predicate: P) -> E
where
P: Fn(
Expand All @@ -78,6 +79,7 @@ pub trait SwarmExt {
/// Returns the next [`SwarmEvent`] or times out after 10 seconds.
///
/// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`.
#[allow(deprecated)]
async fn next_swarm_event(
&mut self,
) -> SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm, THandlerErr<Self::NB>>;
Expand Down Expand Up @@ -121,6 +123,7 @@ pub trait SwarmExt {
/// This can "starve" the other swarm which for example may wait for another message to be sent on a connection.
///
/// Using [`drive`] instead of [`futures::future::join`] ensures that a [`Swarm`] continues to be polled, even after it emitted its events.
#[allow(deprecated)]
pub async fn drive<
TBehaviour1,
const NUM_EVENTS_SWARM_1: usize,
Expand Down Expand Up @@ -279,6 +282,7 @@ where
.await
}

#[allow(deprecated)]
async fn wait<E, P>(&mut self, predicate: P) -> E
where
P: Fn(SwarmEvent<<B as NetworkBehaviour>::ToSwarm, THandlerErr<B>>) -> Option<E>,
Expand Down Expand Up @@ -341,6 +345,7 @@ where
(memory_multiaddr, tcp_multiaddr)
}

#[allow(deprecated)]
async fn next_swarm_event(
&mut self,
) -> SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm, THandlerErr<Self::NB>> {
Expand Down
6 changes: 6 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.43.7

- Deprecate `ConnectionHandlerEvent::Close`.
See [issue 3591](https://github.com/libp2p/rust-libp2p/issues/3591) for details.
See [PR 4714](https://github.com/libp2p/rust-libp2p/pull/4714).

## 0.43.6

- Deprecate `libp2p::swarm::SwarmBuilder`.
Expand Down
2 changes: 1 addition & 1 deletion swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm"
edition = "2021"
rust-version = { workspace = true }
description = "The libp2p swarm"
version = "0.43.6"
version = "0.43.7"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
2 changes: 2 additions & 0 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ where
{
type FromBehaviour = TInner::FromBehaviour;
type ToBehaviour = TInner::ToBehaviour;
#[allow(deprecated)]
type Error = TInner::Error;
type InboundProtocol = Either<SendWrapper<TInner::InboundProtocol>, SendWrapper<DeniedUpgrade>>;
type OutboundProtocol = TInner::OutboundProtocol;
Expand Down Expand Up @@ -300,6 +301,7 @@ where
.unwrap_or(KeepAlive::No)
}

#[allow(deprecated)]
fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
6 changes: 6 additions & 0 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ where

/// Polls the handler and the substream, forwarding events from the former to the latter and
/// vice versa.
#[allow(deprecated)]
pub(crate) fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -267,6 +268,7 @@ where
Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => {
return Poll::Ready(Ok(Event::Handler(event)));
}
#[allow(deprecated)]
Poll::Ready(ConnectionHandlerEvent::Close(err)) => {
return Poll::Ready(Err(ConnectionError::Handler(err)));
}
Expand Down Expand Up @@ -429,6 +431,7 @@ where
}

#[cfg(test)]
#[allow(deprecated)]
fn poll_noop_waker(
&mut self,
) -> Poll<Result<Event<THandler::ToBehaviour>, ConnectionError<THandler::Error>>> {
Expand Down Expand Up @@ -1082,6 +1085,7 @@ mod tests {
KeepAlive::Until(self.until)
}

#[allow(deprecated)]
fn poll(
&mut self,
_: &mut Context<'_>,
Expand Down Expand Up @@ -1312,6 +1316,7 @@ mod tests {
KeepAlive::Yes
}

#[allow(deprecated)]
fn poll(
&mut self,
_: &mut Context<'_>,
Expand Down Expand Up @@ -1389,6 +1394,7 @@ mod tests {
KeepAlive::Yes
}

#[allow(deprecated)]
fn poll(
&mut self,
_: &mut Context<'_>,
Expand Down
5 changes: 5 additions & 0 deletions swarm/src/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum ConnectionError<THandlerErr> {
KeepAliveTimeout,

/// The connection handler produced an error.
#[deprecated(
note = "Will be removed together with `ConnectionHandlerEvent::Close`. See <https://github.com/libp2p/rust-libp2p/issues/3591> for details."
)]
Handler(THandlerErr),
}

Expand All @@ -47,6 +50,7 @@ where
ConnectionError::KeepAliveTimeout => {
write!(f, "Connection closed due to expired keep-alive timeout.")
}
#[allow(deprecated)]
ConnectionError::Handler(err) => write!(f, "Connection error: Handler error: {err}"),
}
}
Expand All @@ -60,6 +64,7 @@ where
match self {
ConnectionError::IO(err) => Some(err),
ConnectionError::KeepAliveTimeout => None,
#[allow(deprecated)]
ConnectionError::Handler(err) => Some(err),
}
}
Expand Down
1 change: 1 addition & 0 deletions swarm/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl<THandler: ConnectionHandler> fmt::Debug for Pool<THandler> {

/// Event that can happen on the `Pool`.
#[derive(Debug)]
#[allow(deprecated)]
pub(crate) enum PoolEvent<THandler: ConnectionHandler> {
/// A new connection has been established.
ConnectionEstablished {
Expand Down
1 change: 1 addition & 0 deletions swarm/src/connection/pool/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) enum PendingConnectionEvent {
}

#[derive(Debug)]
#[allow(deprecated)]
pub(crate) enum EstablishedConnectionEvent<THandler: ConnectionHandler> {
/// A node we are connected to has changed its address.
AddressChange {
Expand Down
1 change: 1 addition & 0 deletions swarm/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
KeepAlive::No
}

#[allow(deprecated)]
fn poll(
&mut self,
_: &mut Context<'_>,
Expand Down
Loading

0 comments on commit e5cee61

Please sign in to comment.