mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 23:31:33 +00:00
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:
@ -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;
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user