diff options
-rw-r--r-- | Cargo.lock | 14 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | src/routes/start.rs | 2 | ||||
-rw-r--r-- | src/services/ping.rs | 11 |
5 files changed, 24 insertions, 11 deletions
@@ -309,6 +309,19 @@ dependencies = [ | |||
309 | ] | 309 | ] |
310 | 310 | ||
311 | [[package]] | 311 | [[package]] |
312 | name = "dashmap" | ||
313 | version = "5.5.3" | ||
314 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
315 | checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" | ||
316 | dependencies = [ | ||
317 | "cfg-if", | ||
318 | "hashbrown 0.14.2", | ||
319 | "lock_api", | ||
320 | "once_cell", | ||
321 | "parking_lot_core", | ||
322 | ] | ||
323 | |||
324 | [[package]] | ||
312 | name = "data-encoding" | 325 | name = "data-encoding" |
313 | version = "2.4.0" | 326 | version = "2.4.0" |
314 | source = "registry+https://github.com/rust-lang/crates.io-index" | 327 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -2189,6 +2202,7 @@ dependencies = [ | |||
2189 | "axum", | 2202 | "axum", |
2190 | "axum-macros", | 2203 | "axum-macros", |
2191 | "config", | 2204 | "config", |
2205 | "dashmap", | ||
2192 | "once_cell", | 2206 | "once_cell", |
2193 | "serde", | 2207 | "serde", |
2194 | "serde_json", | 2208 | "serde_json", |
@@ -19,3 +19,4 @@ sqlx = { version = "0.7.1", features = ["postgres", "runtime-tokio"]} | |||
19 | surge-ping = "0.8.0" | 19 | surge-ping = "0.8.0" |
20 | axum-macros = "0.3.8" | 20 | axum-macros = "0.3.8" |
21 | uuid = { version = "1.5.0", features = ["v4", "fast-rng"] } | 21 | uuid = { version = "1.5.0", features = ["v4", "fast-rng"] } |
22 | dashmap = "5.5.3" | ||
diff --git a/src/main.rs b/src/main.rs index 545d8fe..762a817 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,12 +1,11 @@ | |||
1 | use std::collections::HashMap; | ||
2 | use std::env; | 1 | use std::env; |
3 | use std::sync::Arc; | 2 | use std::sync::Arc; |
4 | use axum::{Router, routing::post}; | 3 | use axum::{Router, routing::post}; |
5 | use axum::routing::{get, put}; | 4 | use axum::routing::{get, put}; |
5 | use dashmap::DashMap; | ||
6 | use sqlx::PgPool; | 6 | use sqlx::PgPool; |
7 | use time::util::local_offset; | 7 | use time::util::local_offset; |
8 | use tokio::sync::broadcast::{channel, Sender}; | 8 | use tokio::sync::broadcast::{channel, Sender}; |
9 | use tokio::sync::Mutex; | ||
10 | use tracing::{info, level_filters::LevelFilter}; | 9 | use tracing::{info, level_filters::LevelFilter}; |
11 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 10 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
12 | use crate::config::SETTINGS; | 11 | use crate::config::SETTINGS; |
@@ -51,9 +50,9 @@ async fn main() { | |||
51 | 50 | ||
52 | let (tx, _) = channel(32); | 51 | let (tx, _) = channel(32); |
53 | 52 | ||
54 | let ping_map: HashMap<String, (String, bool)> = HashMap::new(); | 53 | let ping_map: DashMap<String, (String, bool)> = DashMap::new(); |
55 | 54 | ||
56 | let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(Mutex::new(ping_map)) }); | 55 | let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(ping_map) }); |
57 | 56 | ||
58 | let app = Router::new() | 57 | let app = Router::new() |
59 | .route("/start", post(start)) | 58 | .route("/start", post(start)) |
diff --git a/src/routes/start.rs b/src/routes/start.rs index b1c8a73..5b73281 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -47,7 +47,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
47 | let uuid_gencc = uuid_gen.clone(); | 47 | let uuid_gencc = uuid_gen.clone(); |
48 | tokio::spawn(async move{ | 48 | tokio::spawn(async move{ |
49 | debug!("Init ping service"); | 49 | debug!("Init ping service"); |
50 | state.ping_map.lock().await.insert(uuid_gen, ("192.168.178.94".to_string(), false)); | 50 | state.ping_map.insert(uuid_gen, ("192.168.178.94".to_string(), false)); |
51 | 51 | ||
52 | warn!("{:?}", state.ping_map); | 52 | warn!("{:?}", state.ping_map); |
53 | 53 | ||
diff --git a/src/services/ping.rs b/src/services/ping.rs index 6835fc0..ed848fc 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -1,16 +1,15 @@ | |||
1 | use std::borrow::Cow; | 1 | use std::borrow::Cow; |
2 | use std::collections::HashMap; | ||
3 | use std::sync::Arc; | 2 | use std::sync::Arc; |
4 | 3 | ||
5 | use axum::extract::{ws::WebSocket}; | 4 | use axum::extract::{ws::WebSocket}; |
6 | use axum::extract::ws::{CloseFrame, Message}; | 5 | use axum::extract::ws::{CloseFrame, Message}; |
6 | use dashmap::DashMap; | ||
7 | use tokio::sync::broadcast::{Sender}; | 7 | use tokio::sync::broadcast::{Sender}; |
8 | use tokio::sync::Mutex; | ||
9 | use tracing::{debug, trace, warn}; | 8 | use tracing::{debug, trace, warn}; |
10 | 9 | ||
11 | use crate::error::WebolError; | 10 | use crate::error::WebolError; |
12 | 11 | ||
13 | pub type PingMap = Arc<Mutex<HashMap<String, (String, bool)>>>; | 12 | pub type PingMap = Arc<DashMap<String, (String, bool)>>; |
14 | 13 | ||
15 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { | 14 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { |
16 | let payload = [0; 8]; | 15 | let payload = [0; 8]; |
@@ -41,11 +40,11 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
41 | 40 | ||
42 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) { | 41 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) { |
43 | debug!("sending pingsuccess message"); | 42 | debug!("sending pingsuccess message"); |
44 | ping_map.lock().await.insert(uuid.clone(), (ip.clone(), true)); | 43 | ping_map.insert(uuid.clone(), (ip.clone(), true)); |
45 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); | 44 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); |
46 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | 45 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; |
47 | trace!("remove {} from ping_map", uuid); | 46 | trace!("remove {} from ping_map", uuid); |
48 | ping_map.lock().await.remove(&uuid); | 47 | ping_map.remove(&uuid); |
49 | } | 48 | } |
50 | 49 | ||
51 | #[derive(Clone, Debug)] | 50 | #[derive(Clone, Debug)] |
@@ -63,7 +62,7 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommand | |||
63 | trace!("Search for uuid: {:?}", uuid); | 62 | trace!("Search for uuid: {:?}", uuid); |
64 | 63 | ||
65 | // TODO: Handle Error | 64 | // TODO: Handle Error |
66 | let device = ping_map.lock().await.get(&uuid).unwrap().to_owned(); | 65 | let device = ping_map.get(&uuid).unwrap().to_owned(); |
67 | 66 | ||
68 | trace!("got device: {:?}", device); | 67 | trace!("got device: {:?}", device); |
69 | 68 | ||