Skip to content

Commit 49dbc03

Browse files
committed
Add timeout for RGS updates
.. even though we don't expect this to block, we're better safe than sorry and start to introduce timeouts for any calls we make to remote servers.
1 parent d9eeca3 commit 49dbc03

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ enum NodeError {
165165
"TxSyncFailed",
166166
"TxSyncTimeout",
167167
"GossipUpdateFailed",
168+
"GossipUpdateTimeout",
168169
"LiquidityRequestFailed",
169170
"InvalidAddress",
170171
"InvalidSocketAddress",

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ pub(crate) const LDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 30;
5555
// The timeout after which we abort a fee rate cache update operation.
5656
pub(crate) const FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS: u64 = 5;
5757

58+
// The timeout after which we abort a RGS sync operation.
59+
pub(crate) const RGS_SYNC_TIMEOUT_SECS: u64 = 5;
60+
5861
// The length in bytes of our wallets' keys seed.
5962
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
6063

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub enum Error {
4949
TxSyncTimeout,
5050
/// A gossip updating operation failed.
5151
GossipUpdateFailed,
52+
/// A gossip updating operation timed out.
53+
GossipUpdateTimeout,
5254
/// A liquidity request operation failed.
5355
LiquidityRequestFailed,
5456
/// The given address is invalid.
@@ -127,6 +129,7 @@ impl fmt::Display for Error {
127129
Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
128130
Self::TxSyncTimeout => write!(f, "Syncing transactions timed out."),
129131
Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
132+
Self::GossipUpdateTimeout => write!(f, "Updating gossip data timed out."),
130133
Self::LiquidityRequestFailed => write!(f, "Failed to request inbound liquidity."),
131134
Self::InvalidAddress => write!(f, "The given address is invalid."),
132135
Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),

src/gossip.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::config::RGS_SYNC_TIMEOUT_SECS;
12
use crate::logger::{log_trace, FilesystemLogger, Logger};
23
use crate::types::{GossipSync, Graph, P2PGossipSync, RapidGossipSync};
34
use crate::Error;
@@ -6,6 +7,7 @@ use lightning::routing::utxo::UtxoLookup;
67

78
use std::sync::atomic::{AtomicU32, Ordering};
89
use std::sync::Arc;
10+
use std::time::Duration;
911

1012
pub(crate) enum GossipSource {
1113
P2PNetwork {
@@ -55,7 +57,17 @@ impl GossipSource {
5557
Self::RapidGossipSync { gossip_sync, server_url, latest_sync_timestamp, logger } => {
5658
let query_timestamp = latest_sync_timestamp.load(Ordering::Acquire);
5759
let query_url = format!("{}/{}", server_url, query_timestamp);
58-
let response = reqwest::get(query_url).await.map_err(|e| {
60+
61+
let response = tokio::time::timeout(
62+
Duration::from_secs(RGS_SYNC_TIMEOUT_SECS),
63+
reqwest::get(query_url),
64+
)
65+
.await
66+
.map_err(|e| {
67+
log_trace!(logger, "Retrieving RGS gossip update timed out: {}", e);
68+
Error::GossipUpdateTimeout
69+
})?
70+
.map_err(|e| {
5971
log_trace!(logger, "Failed to retrieve RGS gossip update: {}", e);
6072
Error::GossipUpdateFailed
6173
})?;

0 commit comments

Comments
 (0)