Merge #625: bitcoin: looper: avoid large sleeps when bitcoind is syncing

0d5041ca4a972a946244643db738e6d35b0d114e bitcoin: looper: avoid large sleeps when bitcoind is syncing (Antoine Poinsot)

Pull request description:

  Sleeping for 30 whole seconds impedes the shutdown check. Sleep only .5s but still only do poll bitcoind one every 30s when it's syncing.

  It was reported to make the GUI hang when closing it while syncing.

ACKs for top commit:
  edouardparis:
    utACK 0d5041ca4a972a946244643db738e6d35b0d114e

Tree-SHA512: 48c9121a02abaf8311d7b646ea64cbde4e14fda737c6f78521739bc185e6081642d05b3ca8088d3ce6782ffa00d6e5879e6a6e09da7f05c85f8476ac83b6860b
This commit is contained in:
Antoine Poinsot 2023-08-22 16:16:16 +02:00
commit 05c9b580db
No known key found for this signature in database
GPG Key ID: E13FC145CD3F4304

View File

@ -306,6 +306,16 @@ fn maybe_initialize_tip(bit: &impl BitcoinInterface, db: &impl DatabaseInterface
}
}
fn sync_poll_interval() -> time::Duration {
// TODO: be smarter, like in revaultd, but more generic too.
#[cfg(not(test))]
{
time::Duration::from_secs(30)
}
#[cfg(test)]
time::Duration::from_secs(0)
}
/// Main event loop. Repeatedly polls the Bitcoin interface until told to stop through the
/// `shutdown` atomic.
pub fn looper(
@ -329,7 +339,15 @@ pub fn looper(
let now = time::Instant::now();
if let Some(last_poll) = last_poll {
if now.duration_since(last_poll) < poll_interval {
let time_since_poll = now.duration_since(last_poll);
let poll_interval = if synced {
poll_interval
} else {
// Until we are synced we poll less often to avoid harassing bitcoind and impeding
// the sync. As a function since it's mocked for the tests.
sync_poll_interval()
};
if time_since_poll < poll_interval {
thread::sleep(time::Duration::from_millis(500));
continue;
}
@ -345,10 +363,6 @@ pub fn looper(
);
synced = sync_progress == 1.0;
if !synced {
// Avoid harassing bitcoind..
// TODO: be smarter, like in revaultd, but more generic too.
#[cfg(not(test))]
thread::sleep(time::Duration::from_secs(30));
continue;
}
}