Skip to content

Commit

Permalink
intercept CTRL-C to exit gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Dec 11, 2024
1 parent 449d49b commit 1d98aa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rand = { workspace = true }
serde = { workspace = true, features = ["derive"] }
futures = { workspace = true }
async-trait = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true, features = ["signal"]}
alloy-serde = { workspace = true }
serde_json = { workspace = true }
contender_bundle_provider = { workspace = true }
14 changes: 14 additions & 0 deletions crates/core/src/spammer/spammer_trait.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Mutex;
use std::{pin::Pin, sync::Arc};

use alloy::providers::Provider;
Expand Down Expand Up @@ -39,6 +40,15 @@ where
run_id: Option<u64>,
sent_tx_callback: Arc<F>,
) -> impl std::future::Future<Output = Result<()>> {
let quit = Arc::new(Mutex::new(false));

let quit_clone = quit.clone();
tokio::task::spawn(async move {
let _ = tokio::signal::ctrl_c().await;
let mut quit = quit_clone.lock().unwrap();
*quit = true;
});

async move {
let tx_requests = scenario
.load_txs(crate::generator::PlanType::Spam(
Expand Down Expand Up @@ -82,6 +92,10 @@ where
if cache_size == 0 {
break;
}
if quit.lock().expect("lock failure").clone() {
println!("CTRL-C received, stopping spam...");
break;
}
block_counter += 1;
}
println!("done spamming. run_id={}", run_id);
Expand Down

0 comments on commit 1d98aa8

Please sign in to comment.