use RwLock instead of Mutex for Users

This commit is contained in:
Adam 2024-08-02 21:01:52 -04:00
parent 37fbb8dfba
commit 8ea7650de5
5 changed files with 26 additions and 26 deletions

View file

@ -1,7 +1,7 @@
use anyhow::Result;
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use crate::models::*;
@ -28,7 +28,7 @@ impl Game {
tracing::debug!(
"Creating game {} with {} as host",
&request.name,
request.host.lock().unwrap().name
request.host.read().unwrap().name
);
game.name = request.name;
game.host = request.host.clone();
@ -91,14 +91,14 @@ impl Game {
}
/// Create a new player and add them to the game.
pub fn create_player(&mut self, user: Arc<Mutex<User>>) -> Result<()> {
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
let mut new_player = Player {
user: user.clone(),
white: vec![],
black: vec![],
};
let new_player_name = user.lock().unwrap().name.clone();
let new_player_name = user.read().unwrap().name.clone();
tracing::debug!("Creating player for {}", &new_player_name);
let mut hand_buf = vec![];

View file

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
/// Games update
#[derive(Serialize, Deserialize, Debug)]
@ -64,7 +64,7 @@ pub struct NewGameManifest {
/// Game name
pub name: String,
/// Game host
pub host: Arc<Mutex<User>>,
pub host: Arc<RwLock<User>>,
}
/// Game join request structure
@ -127,7 +127,7 @@ pub enum PlayerRole {
#[derive(Debug)]
pub struct Player {
/// Player's username
pub user: Arc<Mutex<User>>,
pub user: Arc<RwLock<User>>,
/// The player's hand
pub white: Vec<CardWhite>,
/// The player's wins
@ -140,7 +140,7 @@ pub struct Game {
/// The name of the game
pub name: String,
/// The host user of the game
pub host: Arc<Mutex<User>>,
pub host: Arc<RwLock<User>>,
/// White draw pile
pub white: Vec<CardWhite>,
/// Black draw pile

View file

@ -12,7 +12,7 @@ use futures::{SinkExt, StreamExt};
use lib::models::*;
use rand::seq::SliceRandom;
use serde_json::to_string;
use std::sync::Mutex;
use std::sync::RwLock;
use std::{net::SocketAddr, sync::Arc};
pub mod message_handler;
use crate::message_handler::*;
@ -73,7 +73,7 @@ async fn handle_new_user(
addr: &SocketAddr,
) -> Result<()> {
// Create
let new_user = Arc::new(Mutex::new(generate_new_user(state)));
let new_user = Arc::new(RwLock::new(generate_new_user(state)));
// Notify client of new username
sender
@ -113,9 +113,9 @@ fn generate_new_user(state: &Arc<AppState>) -> User {
}
/// Generate message to notify client of user changes
fn client_self_user_update(new_user: &Arc<Mutex<User>>) -> String {
fn client_self_user_update(new_user: &Arc<RwLock<User>>) -> String {
to_string::<UserUpdate>(&UserUpdate {
username: new_user.lock().unwrap().name.clone(),
username: new_user.read().unwrap().name.clone(),
})
.unwrap()
}
@ -126,7 +126,7 @@ fn chat_meta_update(state: &Arc<AppState>) -> String {
let mut names = vec![];
for user in state.online_users.lock().unwrap().iter() {
names.push(user.1.lock().unwrap().name.clone());
names.push(user.1.read().unwrap().name.clone());
}
to_string::<ChatUpdate>(&ChatUpdate {
@ -164,7 +164,7 @@ fn games_update(state: &Arc<AppState>) -> String {
names.push(format!(
"Name: {} Host: {}",
game.name,
game.host.lock().unwrap().name
game.host.read().unwrap().name
));
}
@ -181,7 +181,7 @@ fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
.unwrap()
.get(addr)
.unwrap()
.lock()
.read()
.unwrap()
.name
);

View file

@ -82,7 +82,7 @@ fn handle_chat_message(
tx: &Sender<String>,
addr: SocketAddr,
) -> Result<()> {
let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().lock().unwrap().name, chat_message.text};
let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().read().unwrap().name, chat_message.text};
tracing::debug!("{msg}");
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
@ -102,7 +102,7 @@ fn handle_user_log_in(
.unwrap()
.get(&addr)
.unwrap()
.lock()
.read()
.unwrap()
.name
.clone();
@ -138,7 +138,7 @@ fn handle_user_log_in(
.unwrap()
.get_mut(&addr)
.unwrap()
.lock()
.write()
.unwrap()
.change_name(user_log_in.username);
@ -178,7 +178,7 @@ fn handle_close(
.unwrap()
.get(&addr)
.unwrap()
.lock()
.read()
.unwrap()
.name,
cf.code,
@ -197,7 +197,7 @@ fn handle_close(
.unwrap()
.get(&addr)
.unwrap()
.lock()
.read()
.unwrap()
.name
),
@ -211,7 +211,7 @@ fn handle_close(
.unwrap()
.get(&addr)
.unwrap()
.lock()
.read()
.unwrap()
.name
.clone();

View file

@ -8,7 +8,7 @@ use std::{
fs::{read_to_string, File},
io::{BufRead, BufReader},
net::SocketAddr,
sync::{Arc, Mutex},
sync::{Arc, Mutex, RwLock},
};
use tokio::sync::broadcast;
use tower_http::services::ServeDir;
@ -43,8 +43,8 @@ fn load_names(path: &str) -> Result<Vec<String>> {
// Our shared state
pub struct AppState {
// We require unique usernames. This tracks which usernames have been taken.
online_users: Mutex<HashMap<SocketAddr, Arc<Mutex<User>>>>,
offline_users: Mutex<HashMap<String, Arc<Mutex<User>>>>,
online_users: Mutex<HashMap<SocketAddr, Arc<RwLock<User>>>>,
offline_users: Mutex<HashMap<String, Arc<RwLock<User>>>>,
// Channel used to send messages to all connected clients.
tx: broadcast::Sender<String>,
// Master card decks
@ -70,8 +70,8 @@ async fn main() -> Result<()> {
// Set up application state for use with with_state().
// Main Broadcast Channel
let (tx, _rx) = broadcast::channel(100);
let online_users = Mutex::new(HashMap::<SocketAddr, Arc<Mutex<User>>>::new());
let offline_users = Mutex::new(HashMap::<String, Arc<Mutex<User>>>::new());
let online_users = Mutex::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
let offline_users = Mutex::new(HashMap::<String, Arc<RwLock<User>>>::new());
let all_cards = load_cards_from_json("data/cah-cards-full.json")?;
let games = Mutex::new(vec![]);
let first_names = load_names("data/first.txt")?;