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>,
) -> 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<EventStream![Event + 'a]> {
if tok == Some(admin_key.to_string()) {
let full_info: Vec<AdminTrackedInfo> = tracking
.read()
.unwrap()
.iter()
.map(|(_, tracked)| admin_view(&tracked.read().unwrap()))
.collect();

View file

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

View file

@ -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<TrackedInfo> = 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);

View file

@ -17,7 +17,7 @@ fn tracked_view(
dbg: Option<bool>,
tracking: &State<Tracking>,
) -> 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(
match tracked.read().unwrap().state {
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")]
fn tracked_events<'a>(
id: &'a str,
@ -49,14 +42,14 @@ fn tracked_events<'a>(
config: &State<Config>,
mut shutdown: Shutdown,
) -> 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);
Some(EventStream! {
let mut interval = time::interval(timeout);
loop {
select!{
_ = 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);
yield evt;
}
@ -79,7 +72,7 @@ fn store_pos(
evt_queues: &State<TrackingEventQueue>,
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);
state_update(&tracked.read().unwrap(), &evt_queues, &admin_queue);
}
@ -94,8 +87,7 @@ fn set_state(
evt_queues: &State<TrackingEventQueue>,
admin_queue: &State<AdminEventQueue>,
) -> Option<()> {
let tracking_lock = tracking.read().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
if let Vieux {
ref mut invisible,
ref mut color,
@ -118,8 +110,7 @@ pub async fn activate_invisibility(
admin_queue: &State<AdminEventQueue>,
config: &State<Config>,
) -> Option<()> {
let tracking_lock = tracking.read().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
if let Conscrit {
ref mut invisible,
ref mut invisibility_codes,
@ -139,25 +130,12 @@ pub async fn activate_invisibility(
sleep(timeout).await;
if let Conscrit {
ref mut invisible, ..
} = track_clone
.read()
.unwrap()
.get(&id_str)
.unwrap()
.write()
.unwrap()
.state
} = track_clone.get(&id_str).unwrap().write().unwrap().state
{
*invisible = false;
}
state_update(
&track_clone
.read()
.unwrap()
.get(&id_str)
.unwrap()
.read()
.unwrap(),
&track_clone.get(&id_str).unwrap().read().unwrap(),
&queue_clone,
&admin_clone,
);
@ -179,8 +157,7 @@ pub async fn activate_blur(
admin_queue: &State<AdminEventQueue>,
config: &State<Config>,
) -> Option<()> {
let tracking_lock = tracking.read().unwrap();
let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap();
let tracked = &mut tracking.get(&id.to_string()).unwrap().write().unwrap();
if let Conscrit {
ref mut blurred,
ref mut blur_codes,
@ -200,25 +177,12 @@ pub async fn activate_blur(
sleep(timeout).await;
if let Conscrit {
ref mut blurred, ..
} = track_clone
.read()
.unwrap()
.get(&id_str)
.unwrap()
.write()
.unwrap()
.state
} = track_clone.get(&id_str).unwrap().write().unwrap().state
{
*blurred = false;
}
state_update(
&track_clone
.read()
.unwrap()
.get(&id_str)
.unwrap()
.read()
.unwrap(),
&track_clone.get(&id_str).unwrap().read().unwrap(),
&queue_clone,
&admin_clone,
);