no need for RwLock tracking

This commit is contained in:
catvayor 2024-06-16 09:57:29 +02:00
parent 5d9b730bb6
commit 7298d8d2b9
4 changed files with 22 additions and 66 deletions

View file

@ -38,8 +38,7 @@ fn admin_set_state(
admin_queue: &State<AdminEventQueue>, admin_queue: &State<AdminEventQueue>,
) -> Option<()> { ) -> Option<()> {
if tok == Some(admin_key.to_string()) { if tok == Some(admin_key.to_string()) {
let tracking_lock = tracking.read().unwrap(); let tracked = tracking.get(&id.to_string()).unwrap();
let tracked = tracking_lock.get(&id.to_string()).unwrap();
tracked.write().unwrap().state = nstate.into_inner(); tracked.write().unwrap().state = nstate.into_inner();
state_update(&tracked.read().unwrap(), &evt_queue, &admin_queue); state_update(&tracked.read().unwrap(), &evt_queue, &admin_queue);
Some(()) Some(())
@ -59,8 +58,6 @@ fn admin_events<'a>(
) -> Option<EventStream![Event + 'a]> { ) -> Option<EventStream![Event + 'a]> {
if tok == Some(admin_key.to_string()) { if tok == Some(admin_key.to_string()) {
let full_info: Vec<AdminTrackedInfo> = tracking let full_info: Vec<AdminTrackedInfo> = tracking
.read()
.unwrap()
.iter() .iter()
.map(|(_, tracked)| admin_view(&tracked.read().unwrap())) .map(|(_, tracked)| admin_view(&tracked.read().unwrap()))
.collect(); .collect();

View file

@ -168,8 +168,8 @@ impl From<QueuedEvent> for Event {
} }
} }
pub type Tracking = Arc<RwLock<HashMap<String, RwLock<Tracked>>>>; pub type Tracking = Arc<HashMap<String, RwLock<Tracked>>>;
pub type TrackingEventQueue = Arc<RwLock<HashMap<String, RwLock<VecDeque<QueuedEvent>>>>>; pub type TrackingEventQueue = Arc<HashMap<String, RwLock<VecDeque<QueuedEvent>>>>;
pub type AdminEventQueue = Arc<RwLock<VecDeque<QueuedEvent>>>; pub type AdminEventQueue = Arc<RwLock<VecDeque<QueuedEvent>>>;
pub type AdminKey = String; pub type AdminKey = String;
@ -274,8 +274,6 @@ pub fn state_update(
admin_queue: &AdminEventQueue, admin_queue: &AdminEventQueue,
) { ) {
evt_queues evt_queues
.read()
.unwrap()
.get(&tracked.id) .get(&tracked.id)
.unwrap() .unwrap()
.write() .write()

View file

@ -26,11 +26,10 @@ fn index() -> &'static str {
} }
fn send_coords(tracking: &Tracking, evt_queue: &TrackingEventQueue, config: &Config) { fn send_coords(tracking: &Tracking, evt_queue: &TrackingEventQueue, config: &Config) {
let tracking_lock = tracking.read().unwrap(); for (id, queue) in evt_queue.iter() {
for (id, queue) in evt_queue.read().unwrap().iter() { let watcher = tracking.get(id).unwrap().read().unwrap();
let watcher = tracking_lock.get(id).unwrap().read().unwrap();
let mut infos: Vec<TrackedInfo> = Vec::new(); let mut infos: Vec<TrackedInfo> = Vec::new();
for (_, tracked) in tracking_lock.iter() { for (_, tracked) in tracking.iter() {
if let Some(info) = if let Some(info) =
apparent_info(&watcher, &tracked.read().unwrap(), config.blurred_move) apparent_info(&watcher, &tracked.read().unwrap(), config.blurred_move)
{ {
@ -49,7 +48,7 @@ fn clean_expired_evt(
admin_queue: &AdminEventQueue, admin_queue: &AdminEventQueue,
config: &Config, config: &Config,
) { ) {
for (_, queue) in evt_queues.read().unwrap().iter() { for (_, queue) in evt_queues.iter() {
let queue = &mut queue.write().unwrap(); let queue = &mut queue.write().unwrap();
while let Some(queued_evt) = queue.front() { while let Some(queued_evt) = queue.front() {
if queued_evt.expired(Duration::from_millis(config.event_timeout)) { if queued_evt.expired(Duration::from_millis(config.event_timeout)) {
@ -73,7 +72,7 @@ fn clean_expired_evt(
async fn rocket() -> _ { async fn rocket() -> _ {
let rocket = rocket::build(); let rocket = rocket::build();
let config: Config = rocket.figment().extract().unwrap(); let config: Config = rocket.figment().extract().unwrap();
let tracking: Tracking = Arc::new(RwLock::new( let tracking: Tracking = Arc::new(
config config
.teams .teams
.iter() .iter()
@ -88,15 +87,13 @@ async fn rocket() -> _ {
) )
}) })
.collect(), .collect(),
)); );
let evt_queue: TrackingEventQueue = Arc::new(RwLock::new( let evt_queue: TrackingEventQueue = Arc::new(
tracking tracking
.read()
.unwrap()
.iter() .iter()
.map(|(id, _)| (id.clone(), RwLock::new(VecDeque::new()))) .map(|(id, _)| (id.clone(), RwLock::new(VecDeque::new())))
.collect(), .collect(),
)); );
let admin_evt_queue: AdminEventQueue = Arc::new(RwLock::new(VecDeque::new())); let admin_evt_queue: AdminEventQueue = Arc::new(RwLock::new(VecDeque::new()));
let key: AdminKey = config.admin_token.clone(); let key: AdminKey = config.admin_token.clone();
println!("Admin token: {}", key); println!("Admin token: {}", key);

View file

@ -17,7 +17,7 @@ fn tracked_view(
dbg: Option<bool>, dbg: Option<bool>,
tracking: &State<Tracking>, tracking: &State<Tracking>,
) -> Option<Template> { ) -> Option<Template> {
if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { if let Some(tracked) = tracking.get(&id.to_string()) {
Some(Template::render( Some(Template::render(
match tracked.read().unwrap().state { match tracked.read().unwrap().state {
Vieux { .. } => "vieux", Vieux { .. } => "vieux",
@ -35,13 +35,6 @@ fn tracked_view(
} }
} }
fn evts_for(id: &str, evt_queues: &TrackingEventQueue, timeout: Duration) -> Vec<Event> {
evts_to_send(
evt_queues.read().unwrap().get(&id.to_string()).unwrap(),
timeout,
)
}
#[get("/<id>/events")] #[get("/<id>/events")]
fn tracked_events<'a>( fn tracked_events<'a>(
id: &'a str, id: &'a str,
@ -49,14 +42,14 @@ fn tracked_events<'a>(
config: &State<Config>, config: &State<Config>,
mut shutdown: Shutdown, mut shutdown: Shutdown,
) -> Option<EventStream![Event + 'a]> { ) -> Option<EventStream![Event + 'a]> {
if evt_queue.read().unwrap().contains_key(&id.to_string()) { if evt_queue.contains_key(&id.to_string()) {
let timeout = Duration::from_millis(config.event_timeout); let timeout = Duration::from_millis(config.event_timeout);
Some(EventStream! { Some(EventStream! {
let mut interval = time::interval(timeout); let mut interval = time::interval(timeout);
loop { loop {
select!{ select!{
_ = interval.tick() =>{ _ = interval.tick() =>{
for evt in evts_for(id, evt_queue, timeout){ for evt in evts_to_send(evt_queue.get(&id.to_string()).unwrap(), timeout){
//println!("{:?}", evt); //println!("{:?}", evt);
yield evt; yield evt;
} }
@ -79,7 +72,7 @@ fn store_pos(
evt_queues: &State<TrackingEventQueue>, evt_queues: &State<TrackingEventQueue>,
admin_queue: &State<AdminEventQueue>, admin_queue: &State<AdminEventQueue>,
) { ) {
if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { if let Some(tracked) = tracking.get(&id.to_string()) {
tracked.write().unwrap().pos = (lat, long); tracked.write().unwrap().pos = (lat, long);
state_update(&tracked.read().unwrap(), &evt_queues, &admin_queue); state_update(&tracked.read().unwrap(), &evt_queues, &admin_queue);
} }
@ -94,8 +87,7 @@ fn set_state(
evt_queues: &State<TrackingEventQueue>, evt_queues: &State<TrackingEventQueue>,
admin_queue: &State<AdminEventQueue>, admin_queue: &State<AdminEventQueue>,
) -> Option<()> { ) -> Option<()> {
let tracking_lock = tracking.read().unwrap(); let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
if let Vieux { if let Vieux {
ref mut invisible, ref mut invisible,
ref mut color, ref mut color,
@ -118,8 +110,7 @@ pub async fn activate_invisibility(
admin_queue: &State<AdminEventQueue>, admin_queue: &State<AdminEventQueue>,
config: &State<Config>, config: &State<Config>,
) -> Option<()> { ) -> Option<()> {
let tracking_lock = tracking.read().unwrap(); let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
if let Conscrit { if let Conscrit {
ref mut invisible, ref mut invisible,
ref mut invisibility_codes, ref mut invisibility_codes,
@ -139,25 +130,12 @@ pub async fn activate_invisibility(
sleep(timeout).await; sleep(timeout).await;
if let Conscrit { if let Conscrit {
ref mut invisible, .. ref mut invisible, ..
} = track_clone } = track_clone.get(&id_str).unwrap().write().unwrap().state
.read()
.unwrap()
.get(&id_str)
.unwrap()
.write()
.unwrap()
.state
{ {
*invisible = false; *invisible = false;
} }
state_update( state_update(
&track_clone &track_clone.get(&id_str).unwrap().read().unwrap(),
.read()
.unwrap()
.get(&id_str)
.unwrap()
.read()
.unwrap(),
&queue_clone, &queue_clone,
&admin_clone, &admin_clone,
); );
@ -179,8 +157,7 @@ pub async fn activate_blur(
admin_queue: &State<AdminEventQueue>, admin_queue: &State<AdminEventQueue>,
config: &State<Config>, config: &State<Config>,
) -> Option<()> { ) -> Option<()> {
let tracking_lock = tracking.read().unwrap(); let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
if let Conscrit { if let Conscrit {
ref mut blurred, ref mut blurred,
ref mut blur_codes, ref mut blur_codes,
@ -200,25 +177,12 @@ pub async fn activate_blur(
sleep(timeout).await; sleep(timeout).await;
if let Conscrit { if let Conscrit {
ref mut blurred, .. ref mut blurred, ..
} = track_clone } = track_clone.get(&id_str).unwrap().write().unwrap().state
.read()
.unwrap()
.get(&id_str)
.unwrap()
.write()
.unwrap()
.state
{ {
*blurred = false; *blurred = false;
} }
state_update( state_update(
&track_clone &track_clone.get(&id_str).unwrap().read().unwrap(),
.read()
.unwrap()
.get(&id_str)
.unwrap()
.read()
.unwrap(),
&queue_clone, &queue_clone,
&admin_clone, &admin_clone,
); );