Skip to content

Commit

Permalink
Improve rooms registry logic
Browse files Browse the repository at this point in the history
This change removes the redundant logic when
getting or creating a room.
  • Loading branch information
Jurshsmith committed Aug 29, 2023
1 parent c2d6328 commit d27e4e2
Showing 1 changed file with 7 additions and 42 deletions.
49 changes: 7 additions & 42 deletions rust/examples/videoroom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ mod rooms_registry {
use crate::room::{Room, RoomId, WeakRoom};
use async_lock::Mutex;
use mediasoup::prelude::*;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;

Expand All @@ -250,57 +249,23 @@ mod rooms_registry {
worker_manager: &WorkerManager,
room_id: RoomId,
) -> Result<Room, String> {
let mut rooms = self.rooms.lock().await;
match rooms.entry(room_id) {
Entry::Occupied(mut entry) => match entry.get().upgrade() {
if let Some(weak_room) = self.rooms.lock().await.get(&room_id) {
match weak_room.upgrade() {
Some(room) => Ok(room),
None => {
let room = Room::new_with_id(worker_manager, room_id).await?;
entry.insert(room.downgrade());
room.on_close({
let room_id = room.id();
let rooms = Arc::clone(&self.rooms);

move || {
std::thread::spawn(move || {
futures_lite::future::block_on(async move {
rooms.lock().await.remove(&room_id);
});
});
}
})
.detach();
Ok(room)
}
},
Entry::Vacant(entry) => {
let room = Room::new_with_id(worker_manager, room_id).await?;
entry.insert(room.downgrade());
room.on_close({
let room_id = room.id();
let rooms = Arc::clone(&self.rooms);

move || {
std::thread::spawn(move || {
futures_lite::future::block_on(async move {
rooms.lock().await.remove(&room_id);
});
});
}
})
.detach();
Ok(room)
None => self.create_room(worker_manager).await,
}
} else {
self.create_room(worker_manager).await
}
}

/// Create new room with random `RoomId`
pub async fn create_room(&self, worker_manager: &WorkerManager) -> Result<Room, String> {
let mut rooms = self.rooms.lock().await;
let room = Room::new(worker_manager).await?;
rooms.insert(room.id(), room.downgrade());
let room_id = room.id();
rooms.insert(room_id, room.downgrade());
room.on_close({
let room_id = room.id();
let rooms = Arc::clone(&self.rooms);

move || {
Expand Down

0 comments on commit d27e4e2

Please sign in to comment.