Skip to content

Commit b999bd5

Browse files
committed
Removed use of atomic u64 for kill-timer, instead using a simpler global
1 parent c1a384c commit b999bd5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/client.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
use std::net::{IpAddr, Shutdown, ToSocketAddrs};
22-
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
22+
use std::sync::atomic::{AtomicBool, Ordering};
2323
use std::sync::{Arc, Mutex};
2424
use std::sync::mpsc::channel;
2525
use std::thread;
@@ -47,9 +47,10 @@ type BoxResult<T> = Result<T,Box<dyn Error>>;
4747

4848
/// when false, the system is shutting down
4949
static ALIVE:AtomicBool = AtomicBool::new(true);
50+
5051
/// 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
5354

5455
const CONNECT_TIMEOUT:Duration = Duration::from_secs(2);
5556

@@ -166,7 +167,7 @@ pub fn execute(args:ArgMatches) -> BoxResult<()> {
166167

167168
if tr.count_in_progress_streams_server() > 0 {
168169
log::info!("giving the server a few seconds to report results...");
169-
start_kill_timer(KILL_TIMEOUT);
170+
start_kill_timer();
170171
} else { //all data gathered from both sides
171172
kill();
172173
}
@@ -483,14 +484,17 @@ pub fn execute(args:ArgMatches) -> BoxResult<()> {
483484
pub fn kill() -> bool {
484485
ALIVE.swap(false, Ordering::Relaxed)
485486
}
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+
}
488491
}
489492
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+
}
494498
}
495499
}
496500
ALIVE.load(Ordering::Relaxed)

0 commit comments

Comments
 (0)