Skip to content

Commit

Permalink
Add keep_scanning atomic bool to interrupt scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnet3 committed Oct 4, 2024
1 parent 09a70e4 commit 7712bfe
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/scanner/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
sync::atomic::AtomicBool,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -48,6 +49,7 @@ impl SpScanner {
start: Height,
end: Height,
dust_limit: Amount,
keep_scanning: &AtomicBool,
) -> Result<()> {
if start > end {
bail!("bigger start than end: {} > {}", start, end);
Expand All @@ -61,7 +63,8 @@ impl SpScanner {
let block_data_stream = self.backend.get_block_data_for_range(range, dust_limit);

// process blocks using block data stream
self.process_blocks(block_data_stream).await?;
self.process_blocks(block_data_stream, keep_scanning)
.await?;

// after processing, always send update
self.updater.update_last_scan(end);
Expand All @@ -78,12 +81,17 @@ impl SpScanner {
async fn process_blocks(
&mut self,
block_data_stream: impl Stream<Item = Result<BlockData>>,
keep_scanning: &AtomicBool,
) -> Result<()> {
pin_mut!(block_data_stream);

let mut update_time: Instant = Instant::now();

while let Some(blockdata) = block_data_stream.next().await {
if !keep_scanning.load(std::sync::atomic::Ordering::Relaxed) {
return Ok(());
}

let blockdata = blockdata?;
let blkheight = blockdata.blkheight;
let blkhash = blockdata.blkhash;
Expand Down

0 comments on commit 7712bfe

Please sign in to comment.