mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-03 11:41:34 +00:00
feat(swarm): make stream uprade errors more ergonomic
The currently provided `ConnectionHandlerUpgrErr` is very hard to use. Not only does it have a long name, it also features 3 levels of nesting which results in a lot of boilerplate. Last but not least, it exposes `multistream-select` as a dependency to all protocols. We fix all of the above by renaming the type to `StreamUpgradeError` and flattening out its interface. Unrecoverable errors during protocol selection are hidden within the `Io` variant. Related: #3759. Pull-Request: #3882.
This commit is contained in:
@ -19,8 +19,8 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::handler::{
|
||||
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
|
||||
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive,
|
||||
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
|
||||
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, StreamUpgradeError,
|
||||
SubstreamProtocol,
|
||||
};
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
@ -37,7 +37,7 @@ where
|
||||
/// The upgrade for inbound substreams.
|
||||
listen_protocol: SubstreamProtocol<TInbound, ()>,
|
||||
/// If `Some`, something bad happened and we should shut down the handler with an error.
|
||||
pending_error: Option<ConnectionHandlerUpgrErr<<TOutbound as OutboundUpgradeSend>::Error>>,
|
||||
pending_error: Option<StreamUpgradeError<<TOutbound as OutboundUpgradeSend>::Error>>,
|
||||
/// Queue of events to produce in `poll()`.
|
||||
events_out: SmallVec<[TEvent; 4]>,
|
||||
/// Queue of outbound substreams to open.
|
||||
@ -123,7 +123,7 @@ where
|
||||
{
|
||||
type InEvent = TOutbound;
|
||||
type OutEvent = TEvent;
|
||||
type Error = ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>;
|
||||
type Error = StreamUpgradeError<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>;
|
||||
type InboundProtocol = TInbound;
|
||||
type OutboundProtocol = TOutbound;
|
||||
type OutboundOpenInfo = ();
|
||||
|
@ -19,14 +19,14 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::handler::{
|
||||
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent,
|
||||
ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
||||
InboundUpgradeSend, KeepAlive, ListenUpgradeError, OutboundUpgradeSend, SubstreamProtocol,
|
||||
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
|
||||
FullyNegotiatedInbound, FullyNegotiatedOutbound, InboundUpgradeSend, KeepAlive,
|
||||
ListenUpgradeError, OutboundUpgradeSend, StreamUpgradeError, SubstreamProtocol,
|
||||
};
|
||||
use crate::upgrade::SendWrapper;
|
||||
use either::Either;
|
||||
use futures::future;
|
||||
use libp2p_core::upgrade::{SelectUpgrade, UpgradeError};
|
||||
use libp2p_core::upgrade::SelectUpgrade;
|
||||
use std::{cmp, task::Context, task::Poll};
|
||||
|
||||
/// Implementation of [`ConnectionHandler`] that combines two protocols into one.
|
||||
@ -110,47 +110,32 @@ where
|
||||
match self {
|
||||
DialUpgradeError {
|
||||
info: Either::Left(info),
|
||||
error: ConnectionHandlerUpgrErr::Timeout,
|
||||
error: StreamUpgradeError::Apply(Either::Left(err)),
|
||||
} => Either::Left(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Timeout,
|
||||
error: StreamUpgradeError::Apply(err),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Right(info),
|
||||
error: StreamUpgradeError::Apply(Either::Right(err)),
|
||||
} => Either::Right(DialUpgradeError {
|
||||
info,
|
||||
error: StreamUpgradeError::Apply(err),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Left(info),
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
|
||||
error: e,
|
||||
} => Either::Left(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Left(info),
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(err))),
|
||||
} => Either::Left(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)),
|
||||
error: e.map_upgrade_err(|_| panic!("already handled above")),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Right(info),
|
||||
error: ConnectionHandlerUpgrErr::Timeout,
|
||||
error: e,
|
||||
} => Either::Right(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Timeout,
|
||||
error: e.map_upgrade_err(|_| panic!("already handled above")),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Right(info),
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
|
||||
} => Either::Right(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
|
||||
}),
|
||||
DialUpgradeError {
|
||||
info: Either::Right(info),
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(err))),
|
||||
} => Either::Right(DialUpgradeError {
|
||||
info,
|
||||
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)),
|
||||
}),
|
||||
_ => panic!("Wrong API usage; the upgrade error doesn't match the outbound open info"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user