|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | use std::net::{IpAddr, Shutdown, ToSocketAddrs}; |
22 | | -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; |
| 22 | +use std::sync::atomic::{AtomicBool, Ordering}; |
23 | 23 | use std::sync::{Arc, Mutex}; |
24 | 24 | use std::sync::mpsc::channel; |
25 | 25 | use std::thread; |
@@ -47,9 +47,10 @@ type BoxResult<T> = Result<T,Box<dyn Error>>; |
47 | 47 |
|
48 | 48 | /// when false, the system is shutting down |
49 | 49 | static ALIVE:AtomicBool = AtomicBool::new(true); |
| 50 | + |
50 | 51 | /// a deferred kill-switch to handle shutdowns a bit more gracefully in the event of a probable disconnect |
51 | | -static KILL_TIMER:AtomicU64 = AtomicU64::new(0); |
52 | | -const KILL_TIMEOUT:u64 = 5; //once testing finishes, allow a few seconds for the server to respond |
| 52 | +static mut KILL_TIMER_RELATIVE_START_TIME:f64 = 0.0; //the time at which the kill-timer was started |
| 53 | +const KILL_TIMEOUT:f64 = 5.0; //once testing finishes, allow a few seconds for the server to respond |
53 | 54 |
|
54 | 55 | const CONNECT_TIMEOUT:Duration = Duration::from_secs(2); |
55 | 56 |
|
@@ -166,7 +167,7 @@ pub fn execute(args:ArgMatches) -> BoxResult<()> { |
166 | 167 |
|
167 | 168 | if tr.count_in_progress_streams_server() > 0 { |
168 | 169 | log::info!("giving the server a few seconds to report results..."); |
169 | | - start_kill_timer(KILL_TIMEOUT); |
| 170 | + start_kill_timer(); |
170 | 171 | } else { //all data gathered from both sides |
171 | 172 | kill(); |
172 | 173 | } |
@@ -483,14 +484,17 @@ pub fn execute(args:ArgMatches) -> BoxResult<()> { |
483 | 484 | pub fn kill() -> bool { |
484 | 485 | ALIVE.swap(false, Ordering::Relaxed) |
485 | 486 | } |
486 | | -fn start_kill_timer(timeout:u64) { |
487 | | - KILL_TIMER.swap(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + timeout, Ordering::Relaxed); |
| 487 | +fn start_kill_timer() { |
| 488 | + unsafe { |
| 489 | + KILL_TIMER_RELATIVE_START_TIME = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs_f64(); |
| 490 | + } |
488 | 491 | } |
489 | 492 | fn is_alive() -> bool { |
490 | | - let kill_timer = KILL_TIMER.load(Ordering::Relaxed); |
491 | | - if kill_timer != 0 { //initialised |
492 | | - if SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() >= kill_timer { |
493 | | - return false; |
| 493 | + unsafe { |
| 494 | + if KILL_TIMER_RELATIVE_START_TIME != 0.0 { //initialised |
| 495 | + if SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs_f64() - KILL_TIMER_RELATIVE_START_TIME >= KILL_TIMEOUT { |
| 496 | + return false; |
| 497 | + } |
494 | 498 | } |
495 | 499 | } |
496 | 500 | ALIVE.load(Ordering::Relaxed) |
|
0 commit comments