Skip to content

Commit

Permalink
Stop busy-waiting in a Future for 45 seconds every minute
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 29, 2023
1 parent a6731d1 commit 04c49f5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions zebrad/src/components/sync/progress.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Progress tracking for blockchain syncing.
use std::{cmp::min, ops::Add, time::Duration};
use std::{
cmp::min,
ops::Add,
time::{Duration, Instant},
};

use chrono::{TimeZone, Utc};
use chrono::Utc;
use num_integer::div_ceil;

use zebra_chain::{
Expand Down Expand Up @@ -118,17 +122,15 @@ pub async fn show_block_chain_progress(
let mut last_state_change_height = Height(0);

// The last time we logged an update.
// Initialised with the unix epoch, to simplify the code while still staying in the std range.
let mut last_log_time = Utc
.timestamp_opt(0, 0)
.single()
.expect("in-range number of seconds and valid nanosecond");
let mut last_log_time = Instant::now();

#[cfg(feature = "progress-bar")]
let block_bar = howudoin::new().label("Blocks");

loop {
let now = Utc::now();
let instant_now = Instant::now();

let is_syncer_stopped = sync_status.is_close_to_tip();

if let Some(estimated_height) =
Expand All @@ -153,13 +155,12 @@ pub async fn show_block_chain_progress(
}

// Skip logging if it isn't time for it yet
let elapsed_since_log = (now - last_log_time)
.to_std()
.expect("elapsed times are in range");
let elapsed_since_log = last_log_time.saturating_duration_since(instant_now);
if elapsed_since_log < LOG_INTERVAL {
tokio::time::sleep(PROGRESS_BAR_INTERVAL).await;
continue;
} else {
last_log_time = now;
last_log_time = instant_now;
}

// Work out the sync progress towards the estimated tip.
Expand Down

0 comments on commit 04c49f5

Please sign in to comment.