forked from rs-ipfs/rust-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement idle connection timeout (#98)
- Loading branch information
Showing
6 changed files
with
188 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
mod handler; | ||
|
||
use std::{ | ||
task::{Context, Poll}, | ||
time::Duration, | ||
}; | ||
|
||
use libp2p::{ | ||
core::Endpoint, | ||
swarm::{ | ||
ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, PollParameters, THandler, | ||
THandlerInEvent, THandlerOutEvent, ToSwarm, | ||
}, | ||
Multiaddr, PeerId, | ||
}; | ||
|
||
pub struct Behaviour { | ||
idle: Duration, | ||
} | ||
|
||
impl Behaviour { | ||
pub fn new(idle: Duration) -> Behaviour { | ||
Behaviour { idle } | ||
} | ||
} | ||
|
||
impl NetworkBehaviour for Behaviour { | ||
type ConnectionHandler = handler::Handler; | ||
type ToSwarm = void::Void; | ||
|
||
fn handle_pending_inbound_connection( | ||
&mut self, | ||
_: ConnectionId, | ||
_: &Multiaddr, | ||
_: &Multiaddr, | ||
) -> Result<(), ConnectionDenied> { | ||
Ok(()) | ||
} | ||
|
||
fn handle_pending_outbound_connection( | ||
&mut self, | ||
_: ConnectionId, | ||
_: Option<PeerId>, | ||
_: &[Multiaddr], | ||
_: Endpoint, | ||
) -> Result<Vec<Multiaddr>, ConnectionDenied> { | ||
Ok(vec![]) | ||
} | ||
|
||
fn handle_established_inbound_connection( | ||
&mut self, | ||
_: ConnectionId, | ||
_: PeerId, | ||
_: &Multiaddr, | ||
_: &Multiaddr, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
Ok(handler::Handler::new(self.idle)) | ||
} | ||
|
||
fn handle_established_outbound_connection( | ||
&mut self, | ||
_: ConnectionId, | ||
_: PeerId, | ||
_: &Multiaddr, | ||
_: Endpoint, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
Ok(handler::Handler::new(self.idle)) | ||
} | ||
|
||
fn on_connection_handler_event( | ||
&mut self, | ||
_: PeerId, | ||
_: ConnectionId, | ||
_: THandlerOutEvent<Self>, | ||
) { | ||
} | ||
|
||
fn on_swarm_event(&mut self, _: FromSwarm<Self::ConnectionHandler>) {} | ||
|
||
fn poll( | ||
&mut self, | ||
_: &mut Context, | ||
_: &mut impl PollParameters, | ||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
Poll::Pending | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{ | ||
task::{Context, Poll}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use libp2p::{ | ||
core::upgrade::DeniedUpgrade, | ||
swarm::{handler::ConnectionEvent, ConnectionHandlerEvent, KeepAlive, SubstreamProtocol}, | ||
}; | ||
use void::Void; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Handler { | ||
keep_alive: KeepAlive, | ||
} | ||
|
||
impl Handler { | ||
pub fn new(idle: Duration) -> Self { | ||
Self { | ||
keep_alive: KeepAlive::Until(Instant::now() + idle), | ||
} | ||
} | ||
} | ||
|
||
impl libp2p::swarm::ConnectionHandler for Handler { | ||
type FromBehaviour = Void; | ||
type ToBehaviour = Void; | ||
type Error = Void; | ||
type InboundProtocol = DeniedUpgrade; | ||
type OutboundProtocol = DeniedUpgrade; | ||
type InboundOpenInfo = (); | ||
type OutboundOpenInfo = Void; | ||
|
||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> { | ||
SubstreamProtocol::new(DeniedUpgrade, ()) | ||
} | ||
|
||
fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { | ||
void::unreachable(v) | ||
} | ||
|
||
fn connection_keep_alive(&self) -> KeepAlive { | ||
self.keep_alive | ||
} | ||
|
||
fn poll( | ||
&mut self, | ||
_: &mut Context<'_>, | ||
) -> Poll< | ||
ConnectionHandlerEvent< | ||
Self::OutboundProtocol, | ||
Self::OutboundOpenInfo, | ||
Self::ToBehaviour, | ||
Self::Error, | ||
>, | ||
> { | ||
Poll::Pending | ||
} | ||
|
||
fn on_connection_event( | ||
&mut self, | ||
_: ConnectionEvent< | ||
Self::InboundProtocol, | ||
Self::OutboundProtocol, | ||
Self::InboundOpenInfo, | ||
Self::OutboundOpenInfo, | ||
>, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters