Skip to content

Commit 7c7b686

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 de800e1 commit 7c7b686

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
@@ -163,6 +163,7 @@ enum NodeError {
163163
"MessageSigningFailed",
164164
"TxSyncFailed",
165165
"GossipUpdateFailed",
166+
"GossipUpdateTimeout",
166167
"LiquidityRequestFailed",
167168
"InvalidAddress",
168169
"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
@@ -47,6 +47,8 @@ pub enum Error {
4747
TxSyncFailed,
4848
/// A gossip updating operation failed.
4949
GossipUpdateFailed,
50+
/// A gossip updating operation timed out.
51+
GossipUpdateTimeout,
5052
/// A liquidity request operation failed.
5153
LiquidityRequestFailed,
5254
/// The given address is invalid.
@@ -122,6 +124,7 @@ impl fmt::Display for Error {
122124
Self::MessageSigningFailed => write!(f, "Failed to sign given message."),
123125
Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
124126
Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
127+
Self::GossipUpdateTimeout => write!(f, "Updating gossip data timed out."),
125128
Self::LiquidityRequestFailed => write!(f, "Failed to request inbound liquidity."),
126129
Self::InvalidAddress => write!(f, "The given address is invalid."),
127130
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)