Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust examples: Improve rooms registry logic #1146

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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