Skip to content

Commit 3af3ddf

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 b2f6c91 commit 3af3ddf

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
@@ -142,6 +142,7 @@ enum NodeError {
142142
"MessageSigningFailed",
143143
"TxSyncFailed",
144144
"GossipUpdateFailed",
145+
"GossipUpdateTimeout",
145146
"LiquidityRequestFailed",
146147
"InvalidAddress",
147148
"InvalidSocketAddress",

src/config.rs

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

54+
// The timeout after which we abort a RGS sync operation.
55+
pub(crate) const RGS_SYNC_TIMEOUT_SECS: u64 = 5;
56+
5457
// The length in bytes of our wallets' keys seed.
5558
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
5659

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub enum Error {
4141
TxSyncFailed,
4242
/// A gossip updating operation failed.
4343
GossipUpdateFailed,
44+
/// A gossip updating operation timed out.
45+
GossipUpdateTimeout,
4446
/// A liquidity request operation failed.
4547
LiquidityRequestFailed,
4648
/// The given address is invalid.
@@ -105,6 +107,7 @@ impl fmt::Display for Error {
105107
Self::MessageSigningFailed => write!(f, "Failed to sign given message."),
106108
Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
107109
Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
110+
Self::GossipUpdateTimeout => write!(f, "Updating gossip data timed out."),
108111
Self::LiquidityRequestFailed => write!(f, "Failed to request inbound liquidity."),
109112
Self::InvalidAddress => write!(f, "The given address is invalid."),
110113
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, NetworkGraph, 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)