From 84c32953ae5f52be44af4b48381747f55cb04f4a Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 29 Oct 2023 20:30:01 +0100 Subject: impl dashmap --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 1 + src/main.rs | 7 +++---- src/routes/start.rs | 2 +- src/services/ping.rs | 11 +++++------ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2650edc..a60c07d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -308,6 +308,19 @@ dependencies = [ "typenum", ] +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.2", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "data-encoding" version = "2.4.0" @@ -2189,6 +2202,7 @@ dependencies = [ "axum", "axum-macros", "config", + "dashmap", "once_cell", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d29a1b3..cf38752 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ sqlx = { version = "0.7.1", features = ["postgres", "runtime-tokio"]} surge-ping = "0.8.0" axum-macros = "0.3.8" uuid = { version = "1.5.0", features = ["v4", "fast-rng"] } +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 @@ -use std::collections::HashMap; use std::env; use std::sync::Arc; use axum::{Router, routing::post}; use axum::routing::{get, put}; +use dashmap::DashMap; use sqlx::PgPool; use time::util::local_offset; use tokio::sync::broadcast::{channel, Sender}; -use tokio::sync::Mutex; use tracing::{info, level_filters::LevelFilter}; use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; use crate::config::SETTINGS; @@ -51,9 +50,9 @@ async fn main() { let (tx, _) = channel(32); - let ping_map: HashMap = HashMap::new(); + let ping_map: DashMap = DashMap::new(); - let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(Mutex::new(ping_map)) }); + let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(ping_map) }); let app = Router::new() .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>, headers: HeaderMap let uuid_gencc = uuid_gen.clone(); tokio::spawn(async move{ debug!("Init ping service"); - state.ping_map.lock().await.insert(uuid_gen, ("192.168.178.94".to_string(), false)); + state.ping_map.insert(uuid_gen, ("192.168.178.94".to_string(), false)); warn!("{:?}", state.ping_map); 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 @@ use std::borrow::Cow; -use std::collections::HashMap; use std::sync::Arc; use axum::extract::{ws::WebSocket}; use axum::extract::ws::{CloseFrame, Message}; +use dashmap::DashMap; use tokio::sync::broadcast::{Sender}; -use tokio::sync::Mutex; use tracing::{debug, trace, warn}; use crate::error::WebolError; -pub type PingMap = Arc>>; +pub type PingMap = Arc>; pub async fn spawn(tx: Sender, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { let payload = [0; 8]; @@ -41,11 +40,11 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping async fn handle_broadcast_send(tx: &Sender, ip: String, ping_map: PingMap, uuid: String) { debug!("sending pingsuccess message"); - ping_map.lock().await.insert(uuid.clone(), (ip.clone(), true)); + ping_map.insert(uuid.clone(), (ip.clone(), true)); let _ = tx.send(BroadcastCommands::PingSuccess(ip)); tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; trace!("remove {} from ping_map", uuid); - ping_map.lock().await.remove(&uuid); + ping_map.remove(&uuid); } #[derive(Clone, Debug)] @@ -63,7 +62,7 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender