Skip to content

Commit

Permalink
Retry broadcasts after 500ms if they failed due to an HTTP error
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tnull committed Nov 29, 2023
1 parent e51bed9 commit 366a970
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/tx_broadcaster.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::logger::{log_error, log_trace, Logger};
use crate::logger::{log_debug, log_error, log_trace, Logger};

use lightning::chain::chaininterface::BroadcasterInterface;

Expand All @@ -10,6 +10,7 @@ use tokio::sync::mpsc;
use tokio::sync::Mutex;

use std::ops::Deref;
use std::time::Duration;

const BCAST_PACKAGE_QUEUE_SIZE: usize = 50;

Expand Down Expand Up @@ -40,14 +41,43 @@ where
Ok(()) => {
log_trace!(self.logger, "Successfully broadcast transaction {}", tx.txid());
}
Err(e) => {
log_error!(
self.logger,
"Failed to broadcast transaction {}: {}",
tx.txid(),
e
);
}
Err(e) => match e {
esplora_client::Error::Reqwest(_) => {
// Wait 500 ms and retry in case we get a `Reqwest` error (typically
// 429)
tokio::time::sleep(Duration::from_millis(500)).await;
log_error!(
self.logger,
"Sync failed due to HTTP connection error, retrying: {}",
e
);
match self.esplora_client.broadcast(tx).await {
Ok(()) => {
log_debug!(
self.logger,
"Successfully broadcast transaction {}",
tx.txid()
);
}
Err(e) => {
log_error!(
self.logger,
"Failed to broadcast transaction {}: {}",
tx.txid(),
e
);
}
}
}
_ => {
log_error!(
self.logger,
"Failed to broadcast transaction {}: {}",
tx.txid(),
e
);
}
},
}
}
}
Expand Down

0 comments on commit 366a970

Please sign in to comment.