mahjong_service/http_server/
base.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#![allow(clippy::await_holding_lock)]
use crate::common::Storage;
use crate::socket::MahjongWebsocketServer;
use actix::prelude::*;
use actix_web::web;
use mahjong_core::GameId;
use rustc_hash::FxHashMap;
use std::sync::{Arc, Mutex};

pub type DataStorage = web::Data<Arc<Box<dyn Storage>>>;
pub type DataSocketServer = web::Data<Arc<Mutex<Addr<MahjongWebsocketServer>>>>;

#[derive(Default)]
pub struct GamesManager {
    games_locks: FxHashMap<GameId, Arc<Mutex<()>>>,
}

impl GamesManager {
    pub fn get_game_mutex(&mut self, game_id: &GameId) -> Arc<Mutex<()>> {
        let mutex_arc = self
            .games_locks
            .entry(game_id.clone())
            .or_insert(Arc::new(Mutex::new(())));

        mutex_arc.clone()
    }
}

pub type GamesManagerData = web::Data<Arc<Mutex<GamesManager>>>;

macro_rules! get_lock {
    ($manager:expr, $game_id:expr) => {
        let game_lock = { $manager.lock().unwrap().get_game_mutex(&$game_id) };
        let _game_lock = game_lock.lock().unwrap();
    };
}

pub(crate) use get_lock;