Skip to content

Commit

Permalink
Fix UTXO scan edge case when PC awakes from sleep
Browse files Browse the repository at this point in the history
This PR fixes an issue where bottled up tokio interval events are fired
successively for the edge case where a computer wakes up from sleep. This
was evident in the UTXO scanning service where many many UTXO scanning tasks
would be started in parallel afterwards instead of only one.
  • Loading branch information
hansieodendaal committed Aug 6, 2021
1 parent fbc35c3 commit 4d09eae
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions base_layer/wallet/src/utxo_scanner_service/utxo_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ where TBackend: WalletBackend + 'static
match self.get_next_peer() {
Some(peer) => match self.attempt_sync(peer.clone()).await {
Ok((total_scanned, final_utxo_pos, elapsed)) => {
debug!(target: LOG_TARGET, "Scanning to UTXO #{}", final_utxo_pos);
debug!(target: LOG_TARGET, "Scanned to UTXO #{}", final_utxo_pos);
self.finalize(total_scanned, final_utxo_pos, elapsed).await?;
return Ok(());
},
Expand Down Expand Up @@ -709,21 +709,34 @@ where TBackend: WalletBackend + 'static
let mut shutdown = self.shutdown_signal.clone();
let start_at = Instant::now() + Duration::from_secs(1);
let mut work_interval = time::interval_at(start_at.into(), self.scan_for_utxo_interval).fuse();
let mut previous = Instant::now();
loop {
futures::select! {
_ = work_interval.select_next_some() => {
let running_flag = self.is_running.clone();
if !running_flag.load(Ordering::SeqCst) {
let task = self.create_task();
debug!(target: LOG_TARGET, "UTXO scanning service starting scan for utxos");
task::spawn(async move {
if let Err(err) = task.run().await {
error!(target: LOG_TARGET, "Error scanning UTXOs: {}", err);
}
//we make sure the flag is set to false here
running_flag.store(false, Ordering::Relaxed);
});
// This bit of code prevents bottled up tokio interval events to be fired successively for the edge
// case where a computer wakes up from sleep.
if start_at.elapsed() > self.scan_for_utxo_interval &&
previous.elapsed() < self.scan_for_utxo_interval.mul_f32(0.9)
{
debug!(
target: LOG_TARGET,
"UTXO scanning work interval event fired too quickly, not running the task"
);
} else {
let running_flag = self.is_running.clone();
if !running_flag.load(Ordering::SeqCst) {
let task = self.create_task();
debug!(target: LOG_TARGET, "UTXO scanning service starting scan for utxos");
task::spawn(async move {
if let Err(err) = task.run().await {
error!(target: LOG_TARGET, "Error scanning UTXOs: {}", err);
}
//we make sure the flag is set to false here
running_flag.store(false, Ordering::Relaxed);
});
}
}
previous = Instant::now();
},
request_context = request_stream.select_next_some() => {
trace!(target: LOG_TARGET, "Handling Service API Request");
Expand Down

0 comments on commit 4d09eae

Please sign in to comment.