Automatically close useless connections (#816)

* Automatically close useless connections

* Add a timeout before dropping the connection

* Rework the timeout

* Use OR to combine the outcome
This commit is contained in:
Pierre Krieger
2019-01-04 12:02:39 +01:00
committed by GitHub
parent ea0f61366c
commit 7da1a860be
12 changed files with 123 additions and 8 deletions

View File

@ -89,6 +89,11 @@ where
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
#[inline]
fn connection_keep_alive(&self) -> bool {
false
}
#[inline]
fn shutdown(&mut self) {
self.shutdown = true;

View File

@ -49,6 +49,9 @@ pub struct PeriodicIdHandler<TSubstream> {
/// shut down.
next_id: Option<Delay>,
/// If `true`, we have started an identification of the remote at least once in the past.
first_id_happened: bool,
/// Marker for strong typing.
marker: PhantomData<TSubstream>,
}
@ -70,6 +73,7 @@ impl<TSubstream> PeriodicIdHandler<TSubstream> {
config: IdentifyProtocolConfig,
pending_result: None,
next_id: Some(Delay::new(Instant::now() + DELAY_TO_FIRST_ID)),
first_id_happened: false,
marker: PhantomData,
}
}
@ -118,6 +122,11 @@ where
}
}
#[inline]
fn connection_keep_alive(&self) -> bool {
!self.first_id_happened
}
#[inline]
fn shutdown(&mut self) {
self.next_id = None;
@ -151,6 +160,7 @@ where
next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
let upgrade = self.config.clone();
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info: () };
self.first_id_happened = true;
Ok(Async::Ready(ev))
}
}