Skip to content

Commit

Permalink
config: add a keep_alive_timeout configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Aug 13, 2024
1 parent 8f2eac8 commit 4ecff68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::transport::websocket::config::Config as WebSocketConfig;

use multiaddr::Multiaddr;

use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, sync::Arc, time::Duration};

/// Connection role.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -121,6 +121,9 @@ pub struct ConfigBuilder {

/// Connection limits config.
connection_limits: ConnectionLimitsConfig,

/// Close the connection if no substreams are open within this time frame.
keep_alive_timeout: Duration,
}

impl Default for ConfigBuilder {
Expand Down Expand Up @@ -153,6 +156,7 @@ impl ConfigBuilder {
request_response_protocols: HashMap::new(),
known_addresses: Vec::new(),
connection_limits: ConnectionLimitsConfig::default(),
keep_alive_timeout: Duration::from_secs(5),
}
}

Expand Down Expand Up @@ -268,6 +272,12 @@ impl ConfigBuilder {
self
}

/// Set keep alive timeout for connections.
pub fn set_keep_alive_timeout(mut self, timeout: Duration) -> Self {
self.keep_alive_timeout = timeout;
self
}

/// Build [`Litep2pConfig`].
pub fn build(mut self) -> Litep2pConfig {
let keypair = match self.keypair {
Expand Down Expand Up @@ -296,6 +306,7 @@ impl ConfigBuilder {
request_response_protocols: self.request_response_protocols,
known_addresses: self.known_addresses,
connection_limits: self.connection_limits,
keep_alive_timeout: self.keep_alive_timeout,
}
}
}
Expand Down Expand Up @@ -355,4 +366,7 @@ pub struct Litep2pConfig {

/// Connection limits config.
pub(crate) connection_limits: ConnectionLimitsConfig,

/// Close the connection if no substreams are open within this time frame.
pub(crate) keep_alive_timeout: Duration,
}
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use multihash::Multihash;
use transport::Endpoint;
use types::ConnectionId;

use std::{collections::HashSet, sync::Arc, time::Duration};
use std::{collections::HashSet, sync::Arc};

pub use bandwidth::BandwidthSink;
pub use error::Error;
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Litep2p {
protocol,
config.fallback_names.clone(),
config.codec,
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
let executor = Arc::clone(&litep2p_config.executor);
litep2p_config.executor.run(Box::pin(async move {
Expand All @@ -191,7 +191,7 @@ impl Litep2p {
protocol,
config.fallback_names.clone(),
config.codec,
config.timeout,
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
RequestResponseProtocol::new(service, config).run().await
Expand All @@ -206,7 +206,7 @@ impl Litep2p {
protocol_name,
Vec::new(),
protocol.codec(),
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
let _ = protocol.run(service).await;
Expand All @@ -225,7 +225,7 @@ impl Litep2p {
ping_config.protocol.clone(),
Vec::new(),
ping_config.codec,
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
Ping::new(service, ping_config).run().await
Expand All @@ -248,7 +248,7 @@ impl Litep2p {
main_protocol.clone(),
fallback_names,
kademlia_config.codec,
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
let _ = Kademlia::new(service, kademlia_config).run().await;
Expand All @@ -269,7 +269,7 @@ impl Litep2p {
identify_config.protocol.clone(),
Vec::new(),
identify_config.codec,
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
identify_config.public = Some(litep2p_config.keypair.public().into());

Expand All @@ -289,7 +289,7 @@ impl Litep2p {
bitswap_config.protocol.clone(),
Vec::new(),
bitswap_config.codec,
Duration::from_secs(5),
litep2p_config.keep_alive_timeout,
);
litep2p_config.executor.run(Box::pin(async move {
Bitswap::new(service, bitswap_config).run().await
Expand Down

0 comments on commit 4ecff68

Please sign in to comment.