From 0920c86de3523785b5f4ac67e2090f0736f9fcb2 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 28 Feb 2024 19:05:49 +0100 Subject: Closes #18. Added pingthreshold. Cargo update --- src/config.rs | 3 ++- src/services/ping.rs | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 4319ffc..58043c2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ pub struct Config { pub apikey: String, pub serveraddr: String, pub pingtimeout: i64, + pub pingthreshold: i64, } impl Config { @@ -14,6 +15,7 @@ impl Config { let config = config::Config::builder() .set_default("serveraddr", "0.0.0.0:7229")? .set_default("pingtimeout", 10)? + .set_default("pingthreshold", 1)? .add_source(File::with_name("config.toml").required(false)) .add_source(File::with_name("config.dev.toml").required(false)) .add_source(config::Environment::with_prefix("WEBOL").prefix_separator("_")) @@ -22,4 +24,3 @@ impl Config { config.try_deserialize() } } - diff --git a/src/services/ping.rs b/src/services/ping.rs index 9191f86..8cf6072 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -49,22 +49,27 @@ pub async fn spawn( }; } + trace!(?msg); + let msg = msg.expect("fatal error"); let _ = tx.send(msg.clone()); - if let BroadcastCommands::Success = msg.command { - sqlx::query!( - r#" - UPDATE devices - SET times = array_append(times, $1) - WHERE id = $2; - "#, - timer.elapsed().whole_seconds(), - device.id - ) - .execute(db) - .await - .unwrap(); + if msg.command == BroadcastCommands::Success { + if timer.elapsed().whole_seconds() > config.pingthreshold { + sqlx::query!( + r#" + UPDATE devices + SET times = array_append(times, $1) + WHERE id = $2; + "#, + timer.elapsed().whole_seconds(), + device.id + ) + .execute(db) + .await + .unwrap(); + } + ping_map.insert( uuid.clone(), Value { -- cgit v1.2.3