use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
pub fn start_timeout_thread(timeout_seconds: u64) -> Arc<AtomicBool> {
let should_stop = Arc::new(AtomicBool::new(false));
let should_stop_clone = should_stop.clone();
let is_test = std::env::var("RUST_TEST_THREADS").is_ok();
let sleep_interval = if is_test {
Duration::from_millis(10) } else {
Duration::from_secs(1) };
thread::spawn(move || {
let mut elapsed_time = Duration::from_secs(0);
let timeout_duration = Duration::from_secs(timeout_seconds);
while elapsed_time < timeout_duration {
if should_stop_clone.load(Ordering::SeqCst) {
return;
}
thread::sleep(sleep_interval);
elapsed_time += sleep_interval;
}
eprintln!("Search operation timed out after {timeout_seconds} seconds");
std::process::exit(1);
});
should_stop
}