more reduction in long write locks

main
Kyle Johnsen 2022-02-08 10:20:26 -05:00
parent d5cfc471e2
commit 38eb7f2c0e
1 changed files with 62 additions and 53 deletions

View File

@ -264,20 +264,21 @@ fn send_client_left_message(to: &Arc<Client>, from: u32, room: &str){
fn client_leave_room(client: &Arc<Client>, send_to_client: bool){ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
//first remove the client from the room they are in //first remove the client from the room they are in
let mut room = client.room.write().unwrap(); //I need to get the room, because I'll be modifying the clients in it {
let room = client.room.read().unwrap(); //I need to get the room, because I'll be modifying the clients in it
if room.is_some(){ if room.is_some(){
{ {
let mut change_master = false;
let mut new_master_id = 0;
println!("Client in room, leaving"); println!("Client in room, leaving");
let room = room.as_ref().unwrap(); let room = room.as_ref().unwrap();
//may have to choose a new master //may have to choose a new master
let mut master_client = room.master_client.write().unwrap();
let mut change_master = false;
let mut clients = room.clients.write().unwrap();
let mut new_master_id = 0; {
let clients = room.clients.read().unwrap();
let master_client = room.master_client.read().unwrap();
if master_client.id == client.id { if master_client.id == client.id {
println!("Will change master"); println!("Will change master");
//change the master //change the master
@ -290,11 +291,8 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
} }
} }
println!("Client leaving current room {}",&room.name); println!("Client leaving current room {}",&room.name);
for (_k,v) in clients.iter() { for (_k,v) in clients.iter() {
if !send_to_client && v.id == client.id{ if !send_to_client && v.id == client.id{
continue; continue;
@ -304,7 +302,14 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
send_client_left_message(v, client.id, &room.name); send_client_left_message(v, client.id, &room.name);
} }
} }
}
{
let mut clients = room.clients.write().unwrap();
clients.remove(&client.id); //remove the client from that list in the room clients.remove(&client.id); //remove the client from that list in the room
}
let clients = room.clients.read().unwrap();
//if the room is empty, destroy it as well //if the room is empty, destroy it as well
@ -317,17 +322,21 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
for (_k,v) in clients.iter() { for (_k,v) in clients.iter() {
send_client_master_message(&v, new_master_id); send_client_master_message(&v, new_master_id);
} }
{
let mut master_client = room.master_client.write().unwrap();
*master_client = clients.get(&new_master_id).unwrap().clone(); *master_client = clients.get(&new_master_id).unwrap().clone();
}
} }
} }
} else{ } else{
println!("Client not in room"); println!("Client not in room");
} }
}
{
let mut room = client.room.write().unwrap();
*room = Option::None; *room = Option::None;
}
} }