From 7298d8d2b9d14c7b1197610c12aa8b5185ad8270 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sun, 16 Jun 2024 09:57:29 +0200 Subject: [PATCH] no need for RwLock tracking --- src/admin.rs | 5 +---- src/global.rs | 6 ++---- src/main.rs | 19 +++++++---------- src/track.rs | 58 ++++++++++----------------------------------------- 4 files changed, 22 insertions(+), 66 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index e3c1fc2..ce3ce4e 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -38,8 +38,7 @@ fn admin_set_state( admin_queue: &State, ) -> Option<()> { if tok == Some(admin_key.to_string()) { - let tracking_lock = tracking.read().unwrap(); - let tracked = tracking_lock.get(&id.to_string()).unwrap(); + let tracked = tracking.get(&id.to_string()).unwrap(); tracked.write().unwrap().state = nstate.into_inner(); state_update(&tracked.read().unwrap(), &evt_queue, &admin_queue); Some(()) @@ -59,8 +58,6 @@ fn admin_events<'a>( ) -> Option { if tok == Some(admin_key.to_string()) { let full_info: Vec = tracking - .read() - .unwrap() .iter() .map(|(_, tracked)| admin_view(&tracked.read().unwrap())) .collect(); diff --git a/src/global.rs b/src/global.rs index a075300..d5b1209 100644 --- a/src/global.rs +++ b/src/global.rs @@ -168,8 +168,8 @@ impl From for Event { } } -pub type Tracking = Arc>>>; -pub type TrackingEventQueue = Arc>>>>; +pub type Tracking = Arc>>; +pub type TrackingEventQueue = Arc>>>; pub type AdminEventQueue = Arc>>; pub type AdminKey = String; @@ -274,8 +274,6 @@ pub fn state_update( admin_queue: &AdminEventQueue, ) { evt_queues - .read() - .unwrap() .get(&tracked.id) .unwrap() .write() diff --git a/src/main.rs b/src/main.rs index d942867..968a8a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,11 +26,10 @@ fn index() -> &'static str { } fn send_coords(tracking: &Tracking, evt_queue: &TrackingEventQueue, config: &Config) { - let tracking_lock = tracking.read().unwrap(); - for (id, queue) in evt_queue.read().unwrap().iter() { - let watcher = tracking_lock.get(id).unwrap().read().unwrap(); + for (id, queue) in evt_queue.iter() { + let watcher = tracking.get(id).unwrap().read().unwrap(); let mut infos: Vec = Vec::new(); - for (_, tracked) in tracking_lock.iter() { + for (_, tracked) in tracking.iter() { if let Some(info) = apparent_info(&watcher, &tracked.read().unwrap(), config.blurred_move) { @@ -49,7 +48,7 @@ fn clean_expired_evt( admin_queue: &AdminEventQueue, config: &Config, ) { - for (_, queue) in evt_queues.read().unwrap().iter() { + for (_, queue) in evt_queues.iter() { let queue = &mut queue.write().unwrap(); while let Some(queued_evt) = queue.front() { if queued_evt.expired(Duration::from_millis(config.event_timeout)) { @@ -73,7 +72,7 @@ fn clean_expired_evt( async fn rocket() -> _ { let rocket = rocket::build(); let config: Config = rocket.figment().extract().unwrap(); - let tracking: Tracking = Arc::new(RwLock::new( + let tracking: Tracking = Arc::new( config .teams .iter() @@ -88,15 +87,13 @@ async fn rocket() -> _ { ) }) .collect(), - )); - let evt_queue: TrackingEventQueue = Arc::new(RwLock::new( + ); + let evt_queue: TrackingEventQueue = Arc::new( tracking - .read() - .unwrap() .iter() .map(|(id, _)| (id.clone(), RwLock::new(VecDeque::new()))) .collect(), - )); + ); let admin_evt_queue: AdminEventQueue = Arc::new(RwLock::new(VecDeque::new())); let key: AdminKey = config.admin_token.clone(); println!("Admin token: {}", key); diff --git a/src/track.rs b/src/track.rs index f0d7283..4f4af99 100644 --- a/src/track.rs +++ b/src/track.rs @@ -17,7 +17,7 @@ fn tracked_view( dbg: Option, tracking: &State, ) -> Option