acton-service 0.23.0

Production-ready Rust backend framework with type-enforced API versioning
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
//! Room/channel management using acton-reactive actors
//!
//! This module provides actor-based room management for WebSocket connections.
//! Rooms allow connections to be grouped for targeted message broadcasting.

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;

use acton_reactive::prelude::*;
use axum::extract::ws::Message;
use chrono::{DateTime, Utc};
use tokio::sync::mpsc;

use super::config::RoomConfig;
use super::handler::ConnectionId;
use super::messages::{
    BroadcastToRoom, ConnectionDisconnected, GetRoomInfo, JoinRoomRequest, LeaveRoomRequest,
    RoomInfoResponse,
};

/// Unique identifier for a room
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RoomId(String);

impl RoomId {
    /// Create a new room ID
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Get the room ID as a string slice
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for RoomId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for RoomId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for RoomId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

/// A member of a room
#[derive(Debug, Clone)]
pub struct RoomMember {
    /// The connection ID
    pub connection_id: ConnectionId,
    /// Channel for sending messages to this member
    pub sender: mpsc::Sender<Message>,
    /// Optional user ID if authenticated
    pub user_id: Option<String>,
    /// When the member joined
    pub joined_at: DateTime<Utc>,
}

impl RoomMember {
    /// Create a new room member
    #[must_use]
    pub fn new(connection_id: ConnectionId, sender: mpsc::Sender<Message>) -> Self {
        Self {
            connection_id,
            sender,
            user_id: None,
            joined_at: Utc::now(),
        }
    }

    /// Create an authenticated room member
    #[must_use]
    pub fn authenticated(
        connection_id: ConnectionId,
        sender: mpsc::Sender<Message>,
        user_id: String,
    ) -> Self {
        Self {
            connection_id,
            sender,
            user_id: Some(user_id),
            joined_at: Utc::now(),
        }
    }
}

/// A chat room / channel
#[derive(Debug)]
pub struct Room {
    /// Room identifier
    pub id: RoomId,
    /// Members currently in the room
    pub members: HashMap<ConnectionId, RoomMember>,
    /// When the room was created
    pub created_at: DateTime<Utc>,
    /// Last activity time (for idle cleanup)
    pub last_activity: DateTime<Utc>,
    /// Optional metadata
    pub metadata: HashMap<String, String>,
}

impl Room {
    /// Create a new empty room
    #[must_use]
    pub fn new(id: RoomId) -> Self {
        let now = Utc::now();
        Self {
            id,
            members: HashMap::new(),
            created_at: now,
            last_activity: now,
            metadata: HashMap::new(),
        }
    }

    /// Get the number of members in the room
    #[must_use]
    pub fn member_count(&self) -> usize {
        self.members.len()
    }

    /// Check if the room is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.members.is_empty()
    }

    /// Update the last activity timestamp
    pub fn touch(&mut self) {
        self.last_activity = Utc::now();
    }
}

/// State for the room manager actor
#[derive(Debug, Default)]
pub struct RoomManagerState {
    /// All rooms indexed by ID
    rooms: HashMap<RoomId, Room>,
    /// Rooms each connection is a member of (for cleanup on disconnect)
    connection_rooms: HashMap<ConnectionId, HashSet<RoomId>>,
    /// Maximum members per room
    max_members_per_room: usize,
    /// Maximum rooms per connection
    max_rooms_per_connection: usize,
}

/// Shared room manager storage for AppState access
pub type SharedRoomManager = Arc<ActorHandle>;

/// Actor-based room manager
///
/// Manages WebSocket rooms/channels using the acton-reactive actor system.
/// This provides thread-safe room operations without explicit locking.
pub struct RoomManager;

impl RoomManager {
    /// Spawn the room manager actor
    ///
    /// # Arguments
    ///
    /// * `runtime` - The actor runtime to spawn into
    /// * `config` - Room configuration
    ///
    /// # Returns
    ///
    /// The actor handle for sending messages to the room manager
    pub async fn spawn(
        runtime: &mut ActorRuntime,
        config: RoomConfig,
    ) -> anyhow::Result<ActorHandle> {
        let mut agent = runtime.new_actor::<RoomManagerState>();

        // Initialize configuration
        agent.model.max_members_per_room = config.max_members;
        agent.model.max_rooms_per_connection = config.max_rooms_per_connection;

        // Handle join room requests
        agent.mutate_on::<JoinRoomRequest>(|agent, envelope| {
            let request = envelope.message();
            let room_id = request.room_id.clone();
            let member = request.member.clone();
            let connection_id = member.connection_id;

            // Check connection room limit
            let connection_rooms = agent
                .model
                .connection_rooms
                .entry(connection_id)
                .or_default();

            if connection_rooms.len() >= agent.model.max_rooms_per_connection {
                tracing::warn!(
                    connection_id = %connection_id,
                    limit = agent.model.max_rooms_per_connection,
                    "Connection at max room limit"
                );
                return Reply::ready();
            }

            // Get or create room
            let room = agent
                .model
                .rooms
                .entry(room_id.clone())
                .or_insert_with(|| Room::new(room_id.clone()));

            // Check room member limit
            if room.members.len() >= agent.model.max_members_per_room {
                tracing::warn!(
                    room_id = %room_id,
                    limit = agent.model.max_members_per_room,
                    "Room at max capacity"
                );
                return Reply::ready();
            }

            // Add member to room
            room.members.insert(connection_id, member);
            room.touch();
            connection_rooms.insert(room_id.clone());

            tracing::info!(
                room_id = %room_id,
                connection_id = %connection_id,
                member_count = room.members.len(),
                "Member joined room"
            );

            Reply::ready()
        });

        // Handle leave room requests
        agent.mutate_on::<LeaveRoomRequest>(|agent, envelope| {
            let request = envelope.message();
            let room_id = &request.room_id;
            let connection_id = request.connection_id;

            // Remove from room
            if let Some(room) = agent.model.rooms.get_mut(room_id) {
                room.members.remove(&connection_id);
                room.touch();

                tracing::info!(
                    room_id = %room_id,
                    connection_id = %connection_id,
                    member_count = room.members.len(),
                    "Member left room"
                );

                // Clean up empty rooms
                if room.is_empty() {
                    agent.model.rooms.remove(room_id);
                    tracing::debug!(room_id = %room_id, "Empty room removed");
                }
            }

            // Update connection tracking
            if let Some(rooms) = agent.model.connection_rooms.get_mut(&connection_id) {
                rooms.remove(room_id);
            }

            Reply::ready()
        });

        // Handle broadcast to room
        agent.act_on::<BroadcastToRoom>(|agent, envelope| {
            let request = envelope.message();
            let room_id = &request.room_id;
            let message = request.message.clone();
            let exclude_sender = request.exclude_sender;

            if let Some(room) = agent.model.rooms.get(room_id) {
                // Collect senders (filtering out excluded connection)
                let senders: Vec<_> = room
                    .members
                    .values()
                    .filter(|m| {
                        exclude_sender
                            .map(|id| m.connection_id != id)
                            .unwrap_or(true)
                    })
                    .map(|m| m.sender.clone())
                    .collect();

                let member_count = senders.len();
                let room_id_log = room_id.clone();

                Reply::pending(async move {
                    let mut sent = 0;
                    for sender in senders {
                        if sender.send(message.clone()).await.is_ok() {
                            sent += 1;
                        }
                    }
                    tracing::debug!(
                        room_id = %room_id_log,
                        sent = sent,
                        total = member_count,
                        "Broadcast completed"
                    );
                })
            } else {
                Reply::ready()
            }
        });

        // Handle connection disconnect (leave all rooms)
        agent.mutate_on::<ConnectionDisconnected>(|agent, envelope| {
            let connection_id = envelope.message().connection_id;

            // Get all rooms this connection was in
            if let Some(room_ids) = agent.model.connection_rooms.remove(&connection_id) {
                for room_id in room_ids {
                    if let Some(room) = agent.model.rooms.get_mut(&room_id) {
                        room.members.remove(&connection_id);

                        // Clean up empty rooms
                        if room.is_empty() {
                            agent.model.rooms.remove(&room_id);
                            tracing::debug!(room_id = %room_id, "Empty room removed after disconnect");
                        }
                    }
                }
            }

            tracing::debug!(
                connection_id = %connection_id,
                "Connection removed from all rooms"
            );

            Reply::ready()
        });

        // Handle room info requests
        agent.act_on::<GetRoomInfo>(|agent, envelope| {
            let room_id = envelope.message().room_id.clone();
            let reply = envelope.reply_envelope();

            let response = if let Some(room) = agent.model.rooms.get(&room_id) {
                RoomInfoResponse {
                    room_id,
                    member_count: room.member_count(),
                    exists: true,
                }
            } else {
                RoomInfoResponse {
                    room_id,
                    member_count: 0,
                    exists: false,
                }
            };

            Reply::pending(async move {
                reply.send(response).await;
            })
        });

        // Log startup
        agent.after_start(|_agent| {
            tracing::info!("WebSocket room manager started");
            Reply::ready()
        });

        // Log shutdown
        agent.before_stop(|agent| {
            let room_count = agent.model.rooms.len();
            let connection_count = agent.model.connection_rooms.len();

            tracing::info!(
                rooms = room_count,
                connections = connection_count,
                "WebSocket room manager shutting down"
            );

            Reply::ready()
        });

        let handle = agent.start().await;
        Ok(handle)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_room_id_from_string() {
        let id: RoomId = "test-room".into();
        assert_eq!(id.as_str(), "test-room");
    }

    #[test]
    fn test_room_creation() {
        let room = Room::new("test".into());
        assert!(room.is_empty());
        assert_eq!(room.member_count(), 0);
    }

    #[tokio::test]
    async fn test_room_member_creation() {
        let (tx, _rx) = mpsc::channel(32);
        let member = RoomMember::new(ConnectionId::new(), tx);
        assert!(member.user_id.is_none());
    }

    #[tokio::test]
    async fn test_authenticated_member() {
        let (tx, _rx) = mpsc::channel(32);
        let member = RoomMember::authenticated(ConnectionId::new(), tx, "user123".to_string());
        assert_eq!(member.user_id, Some("user123".to_string()));
    }
}