Cleaner shutdown process (#992)

* Cleaner shutdown process

* Finish

* Fix Yamux panic

* Remove irrelevant tests

* Update core/src/nodes/handled_node_tasks.rs

Co-Authored-By: tomaka <pierre.krieger1708@gmail.com>

* Fix yamux error handling

* Update yamux
This commit is contained in:
Pierre Krieger
2019-03-11 17:19:50 +01:00
committed by GitHub
parent cb93c822f1
commit 8059a693a3
32 changed files with 434 additions and 1555 deletions

View File

@ -27,7 +27,7 @@ use libp2p_core::{
transport::{TransportError, upgrade::TransportUpgradeError},
upgrade::{self, OutboundUpgradeApply, UpgradeError}
};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::io::Error as IoError;
use std::mem;
use std::sync::Arc;
@ -149,13 +149,10 @@ where TMuxer: muxing::StreamMuxer + Send + Sync + 'static,
match mem::replace(&mut self.state, IdRetrieverState::Poisoned) {
IdRetrieverState::OpeningSubstream(muxer, mut opening, config) => {
match opening.poll() {
Ok(Async::Ready(Some(substream))) => {
Ok(Async::Ready(substream)) => {
let upgrade = upgrade::apply_outbound(substream, config);
self.state = IdRetrieverState::NegotiatingIdentify(muxer, upgrade)
},
Ok(Async::Ready(None)) => {
return Err(UpgradeError::Apply(IoError::new(IoErrorKind::Other, "remote refused our identify attempt")))
}
Ok(Async::NotReady) => {
self.state = IdRetrieverState::OpeningSubstream(muxer, opening, config);
return Ok(Async::NotReady);

View File

@ -35,9 +35,6 @@ pub struct IdentifyListenHandler<TSubstream> {
/// List of senders to yield to the user.
pending_result: SmallVec<[IdentifySender<TSubstream>; 4]>,
/// True if `shutdown` has been called.
shutdown: bool,
}
impl<TSubstream> IdentifyListenHandler<TSubstream> {
@ -47,7 +44,6 @@ impl<TSubstream> IdentifyListenHandler<TSubstream> {
IdentifyListenHandler {
config: IdentifyProtocolConfig,
pending_result: SmallVec::new(),
shutdown: false,
}
}
}
@ -83,9 +79,6 @@ where
#[inline]
fn inject_event(&mut self, _: Self::InEvent) {}
#[inline]
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
@ -94,11 +87,6 @@ where
KeepAlive::Now
}
#[inline]
fn shutdown(&mut self) {
self.shutdown = true;
}
fn poll(
&mut self,
) -> Poll<
@ -115,10 +103,6 @@ where
)));
}
if self.shutdown {
Ok(Async::Ready(ProtocolsHandlerEvent::Shutdown))
} else {
Ok(Async::NotReady)
}
Ok(Async::NotReady)
}
}

View File

@ -45,9 +45,8 @@ pub struct PeriodicIdHandler<TSubstream> {
/// it the next time `poll()` is invoked.
pending_result: Option<PeriodicIdHandlerEvent>,
/// Future that fires when we need to identify the node again. If `None`, means that we should
/// shut down.
next_id: Option<Delay>,
/// Future that fires when we need to identify the node again.
next_id: Delay,
/// If `true`, we have started an identification of the remote at least once in the past.
first_id_happened: bool,
@ -72,7 +71,7 @@ impl<TSubstream> PeriodicIdHandler<TSubstream> {
PeriodicIdHandler {
config: IdentifyProtocolConfig,
pending_result: None,
next_id: Some(Delay::new(Instant::now() + DELAY_TO_FIRST_ID)),
next_id: Delay::new(Instant::now() + DELAY_TO_FIRST_ID),
first_id_happened: false,
marker: PhantomData,
}
@ -112,16 +111,11 @@ where
#[inline]
fn inject_event(&mut self, _: Self::InEvent) {}
#[inline]
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, err: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
self.pending_result = Some(PeriodicIdHandlerEvent::IdentificationError(err));
self.first_id_happened = true;
if let Some(ref mut next_id) = self.next_id {
next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);
}
self.next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);
}
#[inline]
@ -133,11 +127,6 @@ where
}
}
#[inline]
fn shutdown(&mut self) {
self.next_id = None;
}
fn poll(
&mut self,
) -> Poll<
@ -154,16 +143,11 @@ where
)));
}
let next_id = match self.next_id {
Some(ref mut nid) => nid,
None => return Ok(Async::Ready(ProtocolsHandlerEvent::Shutdown)),
};
// Poll the future that fires when we need to identify the node again.
match next_id.poll()? {
match self.next_id.poll()? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(()) => {
next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
self.next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
let upgrade = self.config.clone();
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info: () };
Ok(Async::Ready(ev))