contactor 0.1.0-alpha.1

A distributed, eventual persisted, websockets framework.
Documentation
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Module responsible for managing broadcasting to multiple subscribers within rooms.

use anyhow::Result;
use dashmap::DashMap;
use futures::{SinkExt, StreamExt};
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::{watch, Mutex};
use yrs_warp::{
    broadcast::{BroadcastGroup, Subscription},
    AwarenessRef,
};

/// The default capacity for the broadcast buffer.
const DEFAULT_BUFFER_CAPACITY: usize = 16;

/// Represents a chat room with a broadcast group and listener count.
struct Room {
    /// The broadcast group for the room.
    bcast: Arc<BroadcastGroup>,
    /// The number of listeners connected to the room.
    listeners: AtomicUsize,
}

/// Errors that can occur when managing broadcasts.
#[derive(Debug, Error)]
pub enum BroadcastManagerError {
    /// The specified room was not found.
    #[error("Room '{room_name}' not found")]
    RoomNotFound { room_name: String },

    /// The specified room already exists.
    #[error("Room '{room_name}' already exists")]
    RoomAlreadyExists { room_name: String },

    /// The room still has participants connected.
    #[error("Room '{room_name}' still has participants")]
    StillParticipants { room_name: String },

    /// Underflow occurred when decrementing listener count.
    #[error("Listener count underflow in room '{room_name}'")]
    ListenerCountUnderflow { room_name: String },
}

/// Manages broadcasting messages to subscribers across multiple rooms.
pub struct BroadcastManager {
    /// A thread-safe map of room names to their corresponding `Room` instances.
    rooms: DashMap<String, Room>,
    /// A mutex-protected map for room shutdown signals.
    room_shutdown_signals: Mutex<HashMap<String, watch::Sender<()>>>,
}

impl Debug for BroadcastManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BroadcastManager")
            // .field("rooms", &self.rooms)
            // .field("room_shutdown_signals", &self.room_shutdown_signals)
            .finish()
    }
}

impl BroadcastManager {
    /// Creates a new `BroadcastManager`.
    pub fn new() -> Self {
        Self {
            rooms: DashMap::new(),
            room_shutdown_signals: Mutex::new(HashMap::new()),
        }
    }

    /// Returns the number of active rooms.
    pub fn num_rooms(&self) -> usize {
        self.rooms.len()
    }

    /// Returns the total number of listeners across all rooms.
    pub fn total_listeners(&self) -> usize {
        self.rooms
            .iter()
            .map(|entry| entry.value().listeners.load(Ordering::SeqCst))
            .sum()
    }

    /// Returns the number of listeners in a specific room.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room.
    pub fn listeners(&self, room_name: &str) -> Option<usize> {
        self.rooms
            .get(room_name)
            .map(|room| room.listeners.load(Ordering::Relaxed))
    }

    /// Subscribes a client to a room's broadcast group.
    ///
    /// # Type Parameters
    ///
    /// * `Sink` - The sink type for sending messages.
    /// * `Stream` - The stream type for receiving messages.
    /// * `E` - The error type for the stream.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room to subscribe to.
    /// * `sink` - An `Arc<Mutex<Sink>>` for sending messages to the client.
    /// * `stream` - A stream of incoming messages from the client.
    ///
    /// # Errors
    ///
    /// Returns `BroadcastManagerError::RoomNotFound` if the room does not exist.
    pub fn subscribe<Sink, Stream, E>(
        &self,
        room_name: &str,
        sink: Arc<Mutex<Sink>>,
        stream: Stream,
    ) -> Result<Subscription, BroadcastManagerError>
    where
        Sink: SinkExt<Vec<u8>> + Send + Sync + Unpin + 'static,
        Stream: StreamExt<Item = Result<Vec<u8>, E>> + Send + Sync + Unpin + 'static,
        Sink::Error: std::error::Error + Send + Sync,
        E: std::error::Error + Send + Sync + 'static,
    {
        let room = self
            .rooms
            .get(room_name)
            .ok_or(BroadcastManagerError::RoomNotFound {
                room_name: room_name.to_string(),
            })?;
        room.listeners.fetch_add(1, Ordering::SeqCst);
        Ok(room.bcast.subscribe(sink, stream))
    }

    /// Attempts to remove a room if it has no participants.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room to drop.
    ///
    /// # Errors
    ///
    /// Returns an error if the room still has participants.
    pub fn drop_room(&self, room_name: &str) -> Result<(), BroadcastManagerError> {
        let removed = self.rooms.remove_if(room_name, |_, room| {
            room.listeners.load(Ordering::SeqCst) == 0
        });

        match removed {
            Some(_) => Ok(()), // Room was successfully removed
            None => {
                if self.rooms.contains_key(room_name) {
                    Err(BroadcastManagerError::StillParticipants {
                        room_name: room_name.to_string(),
                    })
                } else {
                    Ok(())
                }
            }
        }
    }

    /// Unsubscribes a client from a room.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room.
    ///
    /// # Errors
    ///
    /// Returns an error if the room is not found or underflow occurs.
    pub fn unsubscribe(&self, room_name: &str) -> Result<(), BroadcastManagerError> {
        if let Some(room_ref) = self.rooms.get(room_name) {
            let prev_count = room_ref.listeners.fetch_sub(1, Ordering::SeqCst);
            if prev_count == 0 {
                room_ref.listeners.fetch_add(1, Ordering::SeqCst); // Revert decrement
                return Err(BroadcastManagerError::ListenerCountUnderflow {
                    room_name: room_name.to_string(),
                });
            }
            Ok(())
        } else {
            Err(BroadcastManagerError::RoomNotFound {
                room_name: room_name.to_string(),
            })
        }
    }

    /// Creates a new room with the specified name and awareness.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room to create.
    /// * `awareness` - An `AwarenessRef` for the room.
    ///
    /// # Errors
    ///
    /// Returns an error if the room already exists.
    pub async fn create_room(
        &self,
        room_name: &str,
        awareness: AwarenessRef,
    ) -> Result<(), BroadcastManagerError> {
        if self.rooms.contains_key(room_name) {
            return Err(BroadcastManagerError::RoomAlreadyExists {
                room_name: room_name.to_string(),
            });
        }
        let room = Room {
            bcast: Arc::new(BroadcastGroup::new(awareness, DEFAULT_BUFFER_CAPACITY).await),
            listeners: AtomicUsize::new(0),
        };
        self.rooms.insert(room_name.to_string(), room);
        Ok(())
    }

    /// Stores a shutdown signal sender for a room.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room.
    /// * `shutdown_tx` - A watch channel sender for shutdown signals.
    pub async fn store_room_shutdown_signal(
        &self,
        room_name: &str,
        shutdown_tx: watch::Sender<()>,
    ) {
        let mut signals = self.room_shutdown_signals.lock().await;
        signals.insert(room_name.to_string(), shutdown_tx);
    }

    /// Removes the shutdown signal sender for a room.
    ///
    /// # Arguments
    ///
    /// * `room_name` - The name of the room.
    ///
    /// # Returns
    ///
    /// An optional `watch::Sender<()>` if it exists.
    pub async fn remove_room_shutdown_signal(&self, room_name: &str) -> Option<watch::Sender<()>> {
        let mut signals = self.room_shutdown_signals.lock().await;
        signals.remove(room_name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::channel::mpsc;
    use futures::future::join_all;
    use rand::rngs::StdRng;
    use rand::{Rng, SeedableRng};
    use std::io;
    use std::sync::Arc;
    use std::time::Duration;
    use tokio::sync::{Mutex, RwLock};
    use yrs::sync::Awareness;
    use yrs::Doc;

    // Helper function to create a mock AwarenessRef
    fn create_mock_awareness() -> AwarenessRef {
        Arc::new(RwLock::new(Awareness::new(Doc::new())))
    }

    // Helper function to create a mock stream
    fn create_mock_stream() -> impl StreamExt<Item = Result<Vec<u8>, io::Error>> {
        futures::stream::empty()
    }

    #[tokio::test]
    async fn test_create_room() {
        let manager = BroadcastManager::new();
        let awareness = create_mock_awareness();

        // Test creating a new room
        assert!(manager
            .create_room("room1", awareness.clone())
            .await
            .is_ok());

        // Test creating a room that already exists
        let result = manager.create_room("room1", awareness).await;
        assert!(matches!(
            result,
            Err(BroadcastManagerError::RoomAlreadyExists { .. })
        ));
    }

    #[tokio::test]
    async fn test_subscribe_and_unsubscribe() {
        let manager = BroadcastManager::new();
        let awareness = create_mock_awareness();

        // Create a room
        manager.create_room("room1", awareness).await.unwrap();

        // Create a mock sink and stream
        let (tx, _rx) = mpsc::channel(10);
        let sink = Arc::new(Mutex::new(tx));
        let stream = create_mock_stream();

        // Test subscribing
        let subscription = manager.subscribe("room1", sink.clone(), stream);
        assert!(subscription.is_ok());

        // Test unsubscribing
        assert!(manager.unsubscribe("room1").is_ok());

        // Test unsubscribing from a non-existent room
        let result = manager.unsubscribe("non_existent_room");
        assert!(matches!(
            result,
            Err(BroadcastManagerError::RoomNotFound { .. })
        ));
    }

    #[tokio::test]
    async fn test_drop_room() {
        let manager = BroadcastManager::new();
        let awareness = create_mock_awareness();

        // Create a room
        manager.create_room("room1", awareness).await.unwrap();

        // Test dropping an empty room
        assert!(manager.drop_room("room1").is_ok());

        // Test dropping a non-existent room
        assert!(manager.drop_room("non_existent_room").is_ok());

        // Create a new room and subscribe to it
        manager
            .create_room("room2", create_mock_awareness())
            .await
            .unwrap();
        let (tx, _rx) = mpsc::channel(10);
        let sink = Arc::new(Mutex::new(tx));
        let stream = create_mock_stream();
        manager.subscribe("room2", sink, stream).unwrap();

        // Test dropping a room with active listeners
        let result = manager.drop_room("room2");
        assert!(matches!(
            result,
            Err(BroadcastManagerError::StillParticipants { .. })
        ));
    }

    #[tokio::test]
    async fn test_multiple_subscriptions() {
        let manager = BroadcastManager::new();
        let awareness = create_mock_awareness();

        // Create a room
        manager.create_room("room1", awareness).await.unwrap();

        // Create multiple subscriptions
        for _ in 0..3 {
            let (tx, _rx) = mpsc::channel(10);
            let sink = Arc::new(Mutex::new(tx));
            let stream = create_mock_stream();
            manager.subscribe("room1", sink, stream).unwrap();
        }

        // Try to drop the room (should fail due to active listeners)
        let result = manager.drop_room("room1");
        assert!(matches!(
            result,
            Err(BroadcastManagerError::StillParticipants { .. })
        ));

        // Unsubscribe all
        for _ in 0..3 {
            manager.unsubscribe("room1").unwrap();
        }

        // Now dropping should succeed
        assert!(manager.drop_room("room1").is_ok());
    }

    #[tokio::test]
    async fn test_complex_concurrent_operations() {
        let manager = Arc::new(BroadcastManager::new());
        let num_rooms = 30;
        let num_operations_per_room = 100;

        // Create rooms
        for i in 0..num_rooms {
            let awareness = create_mock_awareness();
            manager
                .create_room(&format!("room-{}", i), awareness)
                .await
                .unwrap();
        }

        // Spawn tasks for each room
        let mut tasks = vec![];
        for room_id in 0..num_rooms {
            let manager_clone = manager.clone();
            let task = tokio::spawn(async move {
                // Create a thread-safe RNG
                let mut rng = StdRng::from_entropy();
                let room_name = format!("room-{}", room_id);

                for _ in 0..num_operations_per_room {
                    let operation = rng.gen_range(0..4);
                    match operation {
                        0 => {
                            // Subscribe
                            let (tx, _rx) = mpsc::channel(10);
                            let sink = Arc::new(Mutex::new(tx));
                            let stream = create_mock_stream();
                            let _ = manager_clone.subscribe(&room_name, sink, stream);
                        }
                        1 => {
                            // Unsubscribe
                            let _ = manager_clone.unsubscribe(&room_name);
                        }
                        2 => {
                            // Attempt to drop
                            let _ = manager_clone.drop_room(&room_name);
                        }
                        3 => {
                            // Simulate some processing time
                            tokio::time::sleep(Duration::from_millis(rng.gen_range(1..10))).await;
                        }
                        _ => unreachable!(),
                    }
                }
            });
            tasks.push(task);
        }

        // Wait for all tasks to complete
        let results = join_all(tasks).await;

        // Check if any task panicked
        for result in results {
            assert!(result.is_ok());
        }

        // Verify final state
        for i in 0..num_rooms {
            let room_name = format!("room-{}", i);
            let result = manager.drop_room(&room_name);
            match result {
                Ok(()) => {
                    // Room was successfully dropped, which means it had no listeners
                    println!("Room {} was successfully dropped", room_name);
                }
                Err(BroadcastManagerError::StillParticipants { .. }) => {
                    // Room still has listeners
                    println!("Room {} still has listeners", room_name);
                }
                Err(BroadcastManagerError::RoomNotFound { .. }) => {
                    // Room was already dropped during the test
                    println!("Room {} was already dropped", room_name);
                }
                _ => panic!("Unexpected error for room {}", room_name),
            }
        }
    }
}