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 --- ...248493f4458a532eee554d57438ef7849758238185.json | 15 +++++++++ Cargo.lock | 38 +++++++++++----------- src/config.rs | 3 +- src/services/ping.rs | 31 ++++++++++-------- 4 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 .sqlx/query-df462bb016e2d258de146e248493f4458a532eee554d57438ef7849758238185.json diff --git a/.sqlx/query-df462bb016e2d258de146e248493f4458a532eee554d57438ef7849758238185.json b/.sqlx/query-df462bb016e2d258de146e248493f4458a532eee554d57438ef7849758238185.json new file mode 100644 index 0000000..e448b24 --- /dev/null +++ b/.sqlx/query-df462bb016e2d258de146e248493f4458a532eee554d57438ef7849758238185.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE devices\n SET times = array_append(times, $1)\n WHERE id = $2;\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Text" + ] + }, + "nullable": [] + }, + "hash": "df462bb016e2d258de146e248493f4458a532eee554d57438ef7849758238185" +} diff --git a/Cargo.lock b/Cargo.lock index fcbf58a..4faf4b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "ahash" -version = "0.8.9" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d713b3834d76b85304d4d525563c1276e2e30dc97cc67bfb4585a4a29fc2c89f" +checksum = "8b79b82693f705137f8fb9b37871d99e4f9a7df12b917eed79c3d3954830a60b" dependencies = [ "cfg-if", "getrandom", @@ -53,7 +53,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -148,7 +148,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -334,9 +334,9 @@ checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" [[package]] name = "crossbeam-channel" -version = "0.5.11" +version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b" +checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" dependencies = [ "crossbeam-utils", ] @@ -1251,7 +1251,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -1282,7 +1282,7 @@ checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -1575,7 +1575,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -1969,9 +1969,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.50" +version = "2.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb" +checksum = "6ab617d94515e94ae53b8406c628598680aa0c9587474ecbe58188f7b345d66c" dependencies = [ "proc-macro2", "quote", @@ -1986,9 +1986,9 @@ checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" [[package]] name = "tempfile" -version = "3.10.0" +version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", "fastrand", @@ -2013,7 +2013,7 @@ checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -2108,7 +2108,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -2242,7 +2242,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] @@ -2602,9 +2602,9 @@ checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6" [[package]] name = "winnow" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a4191c47f15cc3ec71fcb4913cb83d58def65dd3787610213c649283b5ce178" +checksum = "44e19b97e00a4d3db3cdb9b53c8c5f87151b5689b82cc86c2848cbdcccb2689b" dependencies = [ "memchr", ] @@ -2635,7 +2635,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.51", ] [[package]] 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