Skip to content

Commit 3e79263

Browse files
committed
feat(sync): implement exponential backoff for transient sync errors with retry delay multiplier
1 parent 01c396a commit 3e79263

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/chain/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use lightning_transaction_sync::EsploraSyncClient;
3636
use lightning_block_sync::gossip::UtxoSource;
3737
use lightning_block_sync::init::{synchronize_listeners, validate_best_block_header};
3838
use lightning_block_sync::poll::{ChainPoller, ChainTip, ValidatedBlockHeader};
39-
use lightning_block_sync::SpvClient;
39+
use lightning_block_sync::{BlockSourceErrorKind, SpvClient};
4040

4141
use bdk_esplora::EsploraAsyncExt;
4242
use bdk_wallet::Update as BdkUpdate;
@@ -425,6 +425,8 @@ impl ChainSource {
425425
"Starting initial synchronization of chain listeners. This might take a while..",
426426
);
427427

428+
let mut backoff_delay_multiplier = 0;
429+
428430
loop {
429431
let channel_manager_best_block_hash =
430432
channel_manager.current_best_block().block_hash;
@@ -504,8 +506,23 @@ impl ChainSource {
504506

505507
Err(e) => {
506508
log_error!(logger, "Failed to synchronize chain listeners: {:?}", e);
507-
tokio::time::sleep(Duration::from_secs(CHAIN_POLLING_INTERVAL_SECS))
508-
.await;
509+
backoff_delay_multiplier += 1;
510+
let backoff_delay =
511+
CHAIN_POLLING_INTERVAL_SECS * backoff_delay_multiplier;
512+
if e.kind() == BlockSourceErrorKind::Transient {
513+
log_info!(
514+
logger,
515+
"Transient error. Retrying sync in {} seconds (backoff multiplier: {}).",
516+
backoff_delay,
517+
backoff_delay_multiplier
518+
);
519+
tokio::time::sleep(Duration::from_secs(backoff_delay)).await;
520+
} else {
521+
log_error!(
522+
logger,
523+
"Failed to synchronize chain listeners: {:?}, e"
524+
);
525+
}
509526
},
510527
}
511528
}

0 commit comments

Comments
 (0)