Skip to content

Commit

Permalink
Merge pull request #162 from rustaceanrob/duration-10-13
Browse files Browse the repository at this point in the history
feat(core): set connection timeout with client message
  • Loading branch information
rustaceanrob authored Oct 13, 2024
2 parents f6d59ce + 36755d3 commit ac6d43d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/core/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "silent-payments")]
use bitcoin::BlockHash;
use bitcoin::ScriptBuf;
use std::time::Duration;
use tokio::sync::broadcast;
pub use tokio::sync::broadcast::Receiver;
use tokio::sync::mpsc::Sender;
Expand Down Expand Up @@ -185,6 +186,20 @@ macro_rules! impl_core_client {
.await
.map_err(|_| ClientError::SendError)
}
/// Set a new connection timeout for peers to respond to messages.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn set_response_timeout(
&self,
duration: Duration,
) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::SetDuration(duration))
.await
.map_err(|_| ClientError::SendError)
}

/// Explicitly start the block filter syncing process. Note that the node will automatically download and check
/// filters unless the policy is to explicitly halt.
Expand Down
4 changes: 3 additions & 1 deletion src/core/messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, time::Duration};

#[cfg(feature = "silent-payments")]
use bitcoin::BlockHash;
Expand Down Expand Up @@ -111,6 +111,8 @@ pub(crate) enum ClientMessage {
/// Explicitly request a block from the node.
#[cfg(feature = "silent-payments")]
GetBlock(BlockHash),
/// Set a new connection timeout.
SetDuration(Duration),
}

/// Warnings a node may issue while running.
Expand Down
4 changes: 4 additions & 0 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
let mut chain = self.chain.lock().await;
chain.get_block(hash);
},
ClientMessage::SetDuration(duration) => {
let mut peer_map = self.peer_map.lock().await;
peer_map.set_duration(duration);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ impl<P: PeerStore> PeerMap<P> {
}
}

// Set a new timeout duration
pub fn set_duration(&mut self, duration: Duration) {
self.response_timeout = duration;
}

// Send out a TCP connection to a new peer and begin tracking the task
pub async fn dispatch(&mut self, loaded_peer: PersistedPeer) -> Result<(), PeerError> {
let (ptx, prx) = mpsc::channel::<MainThreadMessage>(32);
Expand Down

0 comments on commit ac6d43d

Please sign in to comment.