Skip to content

Commit d52dedb

Browse files
jmecomjm
andauthored
Scope relay runtime state by community (#1658)
Co-authored-by: jm <[email protected]>
1 parent 3dd236e commit d52dedb

7 files changed

Lines changed: 241 additions & 41 deletions

File tree

‎crates/buzz-relay/src/audio/handler.rs‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ async fn handle_active_audio_connection(
341341
}
342342
}
343343

344-
let room = state.audio_rooms.get_or_create(channel_id);
344+
let room = state
345+
.audio_rooms
346+
.get_or_create(tenant.community(), channel_id);
345347

346348
// Re-check archived status after obtaining the room. This closes the
347349
// cross-boundary race: a joiner that passed ensure_membership before
@@ -359,12 +361,16 @@ async fn handle_active_audio_connection(
359361
.into(),
360362
))
361363
.await;
362-
state.audio_rooms.cleanup_if_empty(channel_id);
364+
state
365+
.audio_rooms
366+
.cleanup_if_empty(tenant.community(), channel_id);
363367
return;
364368
}
365369
Err(e) => {
366370
warn!(channel_id = %channel_id, "pre-join channel check failed (fail-closed): {e}");
367-
state.audio_rooms.cleanup_if_empty(channel_id);
371+
state
372+
.audio_rooms
373+
.cleanup_if_empty(tenant.community(), channel_id);
368374
return;
369375
}
370376
Ok(_) => {} // Channel exists and is not archived — proceed.
@@ -438,7 +444,9 @@ async fn handle_active_audio_connection(
438444
remote_rejection_ws_error(&reason).to_string().into(),
439445
))
440446
.await;
441-
state.audio_rooms.cleanup_if_empty(channel_id);
447+
state
448+
.audio_rooms
449+
.cleanup_if_empty(tenant.community(), channel_id);
442450
return;
443451
}
444452
Err(crate::audio::join::DialError::Mesh(e)) => {
@@ -453,7 +461,9 @@ async fn handle_active_audio_connection(
453461
.into(),
454462
))
455463
.await;
456-
state.audio_rooms.cleanup_if_empty(channel_id);
464+
state
465+
.audio_rooms
466+
.cleanup_if_empty(tenant.community(), channel_id);
457467
return;
458468
}
459469
}
@@ -587,7 +597,9 @@ async fn handle_active_audio_connection(
587597
.is_err()
588598
{
589599
room.remove_peer(peer_id);
590-
state.audio_rooms.cleanup_if_empty(channel_id);
600+
state
601+
.audio_rooms
602+
.cleanup_if_empty(tenant.community(), channel_id);
591603
return;
592604
}
593605
} else {
@@ -797,7 +809,9 @@ async fn handle_active_audio_connection(
797809
room_emptied = false;
798810
}
799811
Ok(()) => {
800-
room_emptied = state.audio_rooms.cleanup_if_empty(channel_id);
812+
room_emptied = state
813+
.audio_rooms
814+
.cleanup_if_empty(tenant.community(), channel_id);
801815

802816
emit_participant_event(
803817
&state,
@@ -811,7 +825,9 @@ async fn handle_active_audio_connection(
811825
}
812826
}
813827
} else {
814-
room_emptied = state.audio_rooms.cleanup_if_empty(channel_id);
828+
room_emptied = state
829+
.audio_rooms
830+
.cleanup_if_empty(tenant.community(), channel_id);
815831
}
816832

817833
// Owner path: release this room's lease when the room empties, so a new

‎crates/buzz-relay/src/audio/join.rs‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,13 @@ impl<D: HuddleDirectory + ?Sized> HuddleControlAcceptor<D> {
11711171
let msg = match event {
11721172
Ok(delta) => roster_delta_msg(delta),
11731173
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
1174-
let Some(room) = self.rooms.get(session_id) else {
1174+
let Some(community_id) = stream_community else {
1175+
break Ok(());
1176+
};
1177+
let Some(room) = self.rooms.get(
1178+
CommunityId::from_uuid(community_id),
1179+
session_id,
1180+
) else {
11751181
break Ok(());
11761182
};
11771183
roster_snapshot_msg(&room)
@@ -1245,7 +1251,7 @@ impl<D: HuddleDirectory + ?Sized> HuddleControlAcceptor<D> {
12451251
teardown_reason = Some(GoodbyeReason::Draining);
12461252
break Ok(());
12471253
}
1248-
let room = self.rooms.get_or_create(session_id);
1254+
let room = self.rooms.get_or_create(community, session_id);
12491255
// Subscribe before admission; PeerRegistered carries a
12501256
// snapshot after admission, and queued deltas at or below
12511257
// that revision are ignored by the receiver.
@@ -1282,7 +1288,10 @@ impl<D: HuddleDirectory + ?Sized> HuddleControlAcceptor<D> {
12821288
}
12831289
HuddleControlMsg::UnregisterPeer { pubkey } => {
12841290
if let Some(peer_id) = registered.remove(&pubkey) {
1285-
if let Some(room) = self.rooms.get(session_id) {
1291+
if let Some(room) = stream_community.and_then(|community_id| {
1292+
self.rooms
1293+
.get(CommunityId::from_uuid(community_id), session_id)
1294+
}) {
12861295
let peer_index = room.peers.get(&peer_id).map(|peer| peer.peer_index);
12871296
room.remove_peer(peer_id);
12881297
if let Some(peer_index) = peer_index {
@@ -1299,7 +1308,10 @@ impl<D: HuddleDirectory + ?Sized> HuddleControlAcceptor<D> {
12991308
}
13001309
}
13011310
HuddleControlMsg::RosterResync => {
1302-
let Some(room) = self.rooms.get(session_id) else {
1311+
let Some(room) = stream_community.and_then(|community_id| {
1312+
self.rooms
1313+
.get(CommunityId::from_uuid(community_id), session_id)
1314+
}) else {
13031315
break Ok(());
13041316
};
13051317
stream
@@ -1335,7 +1347,10 @@ impl<D: HuddleDirectory + ?Sized> HuddleControlAcceptor<D> {
13351347
// Teardown: drop every peer this stream registered, regardless of how
13361348
// the loop ended. Dropping the peer drops its `audio_tx`, which ends the
13371349
// matching `spawn_remote_peer_sink` task.
1338-
if let Some(room) = self.rooms.get(session_id) {
1350+
if let Some(room) = stream_community.and_then(|community_id| {
1351+
self.rooms
1352+
.get(CommunityId::from_uuid(community_id), session_id)
1353+
}) {
13391354
for (pubkey, peer_id) in registered {
13401355
let peer_index = room.peers.get(&peer_id).map(|peer| peer.peer_index);
13411356
room.remove_peer(peer_id);
@@ -2261,7 +2276,7 @@ mod tests {
22612276
let session_id = Uuid::new_v4();
22622277
let fenced = fenced_owned_by(owner_rt, session_id);
22632278
let rooms = Arc::new(AudioRoomManager::new());
2264-
let room = rooms.get_or_create(session_id);
2279+
let room = rooms.get_or_create(community(), session_id);
22652280
let (_local_id, _local_index, _audio_rx, mut local_ctrl_rx) =
22662281
room.add_peer("owner-local".into(), 2).unwrap();
22672282
// Discard the local peer's own roster delta; this assertion targets the

‎crates/buzz-relay/src/audio/mesh.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ impl MeshAudioRouter {
223223
return verdict;
224224
}
225225

226-
let Some(room) = self.rooms.get(session_id) else {
226+
let Some(room) = self.rooms.get_unambiguous_by_channel(session_id) else {
227227
// No local room for this session: nothing to deliver to. Not an
228-
// error — membership can race a datagram in flight.
228+
// error — membership can race a datagram in flight. An ambiguous
229+
// same-UUID room collision is also dropped because the current
230+
// media envelope has no community label.
229231
return verdict;
230232
};
231233

‎crates/buzz-relay/src/audio/room.rs‎

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! Frames are opaque Opus bytes — the relay never decodes audio.
99
//! `try_send` is used throughout: real-time audio tolerates drops, never queues.
1010
11+
use buzz_core::CommunityId;
1112
use bytes::Bytes;
1213
use dashmap::DashMap;
1314
use std::sync::Arc;
@@ -117,8 +118,8 @@ struct AdmissionGuard {
117118
///
118119
/// Pin is per-`Room`-instance and clears when the manager evicts the
119120
/// Room via [`AudioRoomManager::cleanup_if_empty`] — the next
120-
/// `get_or_create` for the same channel id then constructs a fresh
121-
/// `Room` with a fresh `AdmissionGuard` (and therefore `None` pin),
121+
/// `get_or_create` for the same community-local channel then constructs a
122+
/// fresh `Room` with a fresh `AdmissionGuard` (and therefore `None` pin),
122123
/// so a new generation of joiners can negotiate a new version.
123124
/// A momentarily-empty-but-not-yet-cleaned-up Room keeps its pin so
124125
/// reconnecting peers don't accidentally renegotiate mid-call. See
@@ -158,6 +159,8 @@ impl AdmissionGuard {
158159

159160
/// A single audio room for one channel.
160161
pub struct Room {
162+
/// Community this room belongs to.
163+
pub community_id: CommunityId,
161164
/// Channel UUID this room belongs to.
162165
pub channel_id: Uuid,
163166
/// Connected peers keyed by peer UUID.
@@ -170,10 +173,11 @@ pub struct Room {
170173
}
171174

172175
impl Room {
173-
/// Create an empty room for the given channel.
174-
pub fn new(channel_id: Uuid) -> Self {
176+
/// Create an empty room for the given community-local channel.
177+
pub fn new(community_id: CommunityId, channel_id: Uuid) -> Self {
175178
let (roster_tx, _) = broadcast::channel(64);
176179
Self {
180+
community_id,
177181
channel_id,
178182
peers: DashMap::new(),
179183
guard: std::sync::Mutex::new(AdmissionGuard::new()),
@@ -487,7 +491,7 @@ impl Room {
487491

488492
/// Global registry of active audio rooms.
489493
pub struct AudioRoomManager {
490-
rooms: DashMap<Uuid, Arc<Room>>,
494+
rooms: DashMap<(CommunityId, Uuid), Arc<Room>>,
491495
}
492496

493497
impl AudioRoomManager {
@@ -499,25 +503,47 @@ impl AudioRoomManager {
499503
}
500504

501505
/// Get an existing room or create a new one.
502-
pub fn get_or_create(&self, channel_id: Uuid) -> Arc<Room> {
506+
///
507+
/// Channel UUIDs are only unique inside a community. The room key must
508+
/// carry both labels so two tenants that legitimately reuse the same UUID
509+
/// never share peer lists, protocol pins, or audio frames.
510+
pub fn get_or_create(&self, community_id: CommunityId, channel_id: Uuid) -> Arc<Room> {
503511
self.rooms
504-
.entry(channel_id)
505-
.or_insert_with(|| Arc::new(Room::new(channel_id)))
512+
.entry((community_id, channel_id))
513+
.or_insert_with(|| Arc::new(Room::new(community_id, channel_id)))
506514
.clone()
507515
}
508516

509-
/// Look up an existing room without creating one. Returns `None` when this
510-
/// pod hosts no room for the channel — the cross-pod media path uses this
511-
/// so an inbound datagram for a session we don't participate in is dropped,
512-
/// never materializing an empty room.
513-
pub fn get(&self, channel_id: Uuid) -> Option<Arc<Room>> {
514-
self.rooms.get(&channel_id).map(|r| r.clone())
517+
/// Look up an existing community-local room without creating one.
518+
pub fn get(&self, community_id: CommunityId, channel_id: Uuid) -> Option<Arc<Room>> {
519+
self.rooms
520+
.get(&(community_id, channel_id))
521+
.map(|room| room.clone())
522+
}
523+
524+
/// Look up a room for a mesh datagram that carries only a channel UUID.
525+
///
526+
/// The current mesh media envelope does not carry a community identifier.
527+
/// If two active communities use the same channel UUID, routing would be
528+
/// ambiguous, so fail closed instead of delivering one community's audio
529+
/// to the other. Control-path lookups always use [`Self::get`].
530+
pub fn get_unambiguous_by_channel(&self, channel_id: Uuid) -> Option<Arc<Room>> {
531+
let mut matches = self
532+
.rooms
533+
.iter()
534+
.filter(|entry| entry.key().1 == channel_id)
535+
.map(|entry| Arc::clone(entry.value()));
536+
let room = matches.next()?;
537+
if matches.next().is_some() {
538+
return None;
539+
}
540+
Some(room)
515541
}
516542

517543
/// Remove the room if it has no peers. Returns `true` if the room was removed.
518-
pub fn cleanup_if_empty(&self, channel_id: Uuid) -> bool {
544+
pub fn cleanup_if_empty(&self, community_id: CommunityId, channel_id: Uuid) -> bool {
519545
self.rooms
520-
.remove_if(&channel_id, |_, room| room.is_empty())
546+
.remove_if(&(community_id, channel_id), |_, room| room.is_empty())
521547
.is_some()
522548
}
523549
}
@@ -533,7 +559,7 @@ mod tests {
533559
use super::*;
534560

535561
fn fresh_room() -> Room {
536-
Room::new(Uuid::new_v4())
562+
Room::new(CommunityId::from_uuid(Uuid::new_v4()), Uuid::new_v4())
537563
}
538564

539565
#[test]
@@ -644,9 +670,10 @@ mod tests {
644670
#[test]
645671
fn manager_cleanup_resets_version_pin() {
646672
let manager = AudioRoomManager::new();
673+
let community_id = CommunityId::from_uuid(Uuid::new_v4());
647674
let channel_id = Uuid::new_v4();
648675

649-
let room1 = manager.get_or_create(channel_id);
676+
let room1 = manager.get_or_create(community_id, channel_id);
650677
let (peer_id, _, _, _) = room1
651678
.add_peer("alice".to_string(), 2)
652679
.expect("first peer admits");
@@ -655,16 +682,53 @@ mod tests {
655682
.remove_peer_and_check_ended(peer_id)
656683
.expect("peer existed");
657684
assert!(ended, "single-peer room should end on its last departure");
658-
assert!(manager.cleanup_if_empty(channel_id));
685+
assert!(manager.cleanup_if_empty(community_id, channel_id));
659686

660687
// Next joiner with a different version on the same channel id gets a
661688
// brand-new room (no v=2 pin carried over from the prior generation).
662-
let room2 = manager.get_or_create(channel_id);
689+
let room2 = manager.get_or_create(community_id, channel_id);
663690
let _ = room2
664691
.add_peer("bob".to_string(), 1)
665692
.expect("fresh room must accept any version");
666693
}
667694

695+
#[test]
696+
fn manager_isolates_same_channel_uuid_across_communities() {
697+
let manager = AudioRoomManager::new();
698+
let channel_id = Uuid::new_v4();
699+
let community_a = CommunityId::from_uuid(Uuid::new_v4());
700+
let community_b = CommunityId::from_uuid(Uuid::new_v4());
701+
702+
let room_a = manager.get_or_create(community_a, channel_id);
703+
assert!(Arc::ptr_eq(
704+
&manager
705+
.get_unambiguous_by_channel(channel_id)
706+
.expect("one matching room is unambiguous"),
707+
&room_a
708+
));
709+
let room_b = manager.get_or_create(community_b, channel_id);
710+
711+
assert!(
712+
!Arc::ptr_eq(&room_a, &room_b),
713+
"same channel UUID in two communities must create distinct rooms"
714+
);
715+
assert_eq!(room_a.community_id, community_a);
716+
assert_eq!(room_b.community_id, community_b);
717+
assert!(
718+
manager.get_unambiguous_by_channel(channel_id).is_none(),
719+
"community-free mesh lookup must fail closed on a UUID collision"
720+
);
721+
722+
room_a
723+
.add_peer("alice".to_string(), 1)
724+
.expect("A peer admits");
725+
assert_eq!(room_a.peer_pubkeys(), vec![("alice".to_string(), 0)]);
726+
assert!(
727+
room_b.peer_pubkeys().is_empty(),
728+
"A room peers must not appear in B's same-UUID room"
729+
);
730+
}
731+
668732
/// Peer-index reuse: after a peer leaves, their index is released; a new
669733
/// peer joining the same (still-pinned) room reuses the freed index.
670734
/// Version pin must persist across this reuse — the room generation

‎crates/buzz-relay/src/handlers/auth.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ pub async fn handle_auth(event: nostr::Event, conn: Arc<ConnectionState>, state:
304304
// Successfully materialized — this owner is authoritative.
305305
auth_ctx.agent_owner_pubkey = Some(owner);
306306
// Pre-warm the observer cache to avoid stale negatives.
307-
let cache_key = (pubkey.to_bytes().to_vec(), owner.to_bytes().to_vec());
307+
let cache_key = (
308+
conn.tenant.community(),
309+
pubkey.to_bytes().to_vec(),
310+
owner.to_bytes().to_vec(),
311+
);
308312
state.observer_owner_cache.insert(cache_key, true);
309313
}
310314
Ok(false) => {

0 commit comments

Comments
 (0)