Skip to content

Commit b31f451

Browse files
committed
Retry broadcasts after 500ms if they failed due to an HTTP error
Due to the rate-limiting applied by many Esplora servers we often receive HTTP 429 ('too many requests') errors during syncing. Here, we simply give broadcasting transactions a second chance after a slight delay of 500ms to ease the pain of immediate failures. Generally, rebroadcasting will then be initiated by the `OuputSweeper`.
1 parent bccc835 commit b31f451

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/tx_broadcaster.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::logger::{log_error, log_trace, Logger};
1+
use crate::logger::{log_debug, log_error, log_trace, Logger};
22

33
use lightning::chain::chaininterface::BroadcasterInterface;
44

@@ -10,6 +10,7 @@ use tokio::sync::mpsc;
1010
use tokio::sync::Mutex;
1111

1212
use std::ops::Deref;
13+
use std::time::Duration;
1314

1415
const BCAST_PACKAGE_QUEUE_SIZE: usize = 50;
1516

@@ -40,14 +41,43 @@ where
4041
Ok(()) => {
4142
log_trace!(self.logger, "Successfully broadcast transaction {}", tx.txid());
4243
}
43-
Err(e) => {
44-
log_error!(
45-
self.logger,
46-
"Failed to broadcast transaction {}: {}",
47-
tx.txid(),
48-
e
49-
);
50-
}
44+
Err(e) => match e {
45+
esplora_client::Error::Reqwest(_) => {
46+
// Wait 500 ms and retry in case we get a `Reqwest` error (typically
47+
// 429)
48+
tokio::time::sleep(Duration::from_millis(500)).await;
49+
log_error!(
50+
self.logger,
51+
"Sync failed due to HTTP connection error, retrying: {}",
52+
e
53+
);
54+
match self.esplora_client.broadcast(tx).await {
55+
Ok(()) => {
56+
log_debug!(
57+
self.logger,
58+
"Successfully broadcast transaction {}",
59+
tx.txid()
60+
);
61+
}
62+
Err(e) => {
63+
log_error!(
64+
self.logger,
65+
"Failed to broadcast transaction {}: {}",
66+
tx.txid(),
67+
e
68+
);
69+
}
70+
}
71+
}
72+
_ => {
73+
log_error!(
74+
self.logger,
75+
"Failed to broadcast transaction {}: {}",
76+
tx.txid(),
77+
e
78+
);
79+
}
80+
},
5181
}
5282
}
5383
}

0 commit comments

Comments
 (0)