diff options
author | FxQnLr <[email protected]> | 2023-10-29 20:30:01 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-10-29 20:30:01 +0100 |
commit | 84c32953ae5f52be44af4b48381747f55cb04f4a (patch) | |
tree | 6b7ffd4d7ac7b89bb9bf169be7ba6d266f79d650 /src | |
parent | 0cca10290d089aabac8f2e4356cfaf80f06ae194 (diff) | |
download | webol-84c32953ae5f52be44af4b48381747f55cb04f4a.tar webol-84c32953ae5f52be44af4b48381747f55cb04f4a.tar.gz webol-84c32953ae5f52be44af4b48381747f55cb04f4a.zip |
impl dashmap
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | src/routes/start.rs | 2 | ||||
-rw-r--r-- | src/services/ping.rs | 11 |
3 files changed, 9 insertions, 11 deletions
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 | ||