commy 0.4.1

A hierarchical, multi-tenant shared memory coordination system for Windows enabling secure, efficient data sharing between multiple processes via WebSocket and direct memory-mapping
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
/// Client Failover Detection - Detects connection loss and triggers reconnection
use std::sync::Arc;
use tokio::sync::RwLock;

/// Represents a server that a client is connected to
#[derive(Clone, Debug)]
pub struct ServerConnection {
    pub server_id: String,
    pub address: String,
    pub port: u16,
    pub is_connected: bool,
    pub last_heartbeat: u64,
    pub consecutive_failures: u32,
}

/// Failover detection state for a client session
#[derive(Clone, Debug, PartialEq)]
pub enum ClientFailoverState {
    Connected,
    Reconnecting,
    Failed,
    Recovered,
}

/// Represents a candidate server for reconnection
#[derive(Clone, Debug)]
pub struct ServerCandidate {
    pub server_id: String,
    pub address: String,
    pub port: u16,
    pub latency_ms: u64,
    pub priority: u32,
}

/// Configuration for client-side failover behavior
#[derive(Clone, Debug)]
pub struct ClientFailoverConfig {
    /// Maximum consecutive connection failures before triggering failover
    pub max_consecutive_failures: u32,
    /// Time to wait between reconnection attempts (ms)
    pub reconnection_delay_ms: u64,
    /// Maximum time to attempt reconnection (ms)
    pub reconnection_timeout_ms: u64,
    /// Enable automatic reconnection to healthy peer
    pub auto_reconnect: bool,
    /// List of backup servers to use for failover
    pub backup_servers: Vec<ServerConnection>,
}

impl Default for ClientFailoverConfig {
    fn default() -> Self {
        Self {
            max_consecutive_failures: 3,
            reconnection_delay_ms: 1000,
            reconnection_timeout_ms: 30000,
            auto_reconnect: true,
            backup_servers: Vec::new(),
        }
    }
}

/// Manages client-side failure detection and reconnection
pub struct ClientFailoverDetector {
    current_server: Arc<RwLock<Option<ServerConnection>>>,
    state: Arc<RwLock<ClientFailoverState>>,
    config: ClientFailoverConfig,
    available_servers: Arc<RwLock<Vec<ServerCandidate>>>,
    reconnection_attempts: Arc<RwLock<u32>>,
}

impl ClientFailoverDetector {
    /// Create new client failover detector
    pub fn new(config: ClientFailoverConfig) -> Self {
        Self {
            current_server: Arc::new(RwLock::new(None)),
            state: Arc::new(RwLock::new(ClientFailoverState::Failed)),
            config,
            available_servers: Arc::new(RwLock::new(Vec::new())),
            reconnection_attempts: Arc::new(RwLock::new(0)),
        }
    }

    /// Set the current server connection
    pub async fn set_current_server(&self, server: ServerConnection) {
        *self.current_server.write().await = Some(server);
        *self.state.write().await = ClientFailoverState::Connected;
        *self.reconnection_attempts.write().await = 0;
    }

    /// Record a connection failure
    pub async fn record_failure(&self) -> bool {
        let mut current = self.current_server.write().await;

        if let Some(ref mut server) = *current {
            server.consecutive_failures += 1;

            // Check if we should trigger failover
            if server.consecutive_failures >= self.config.max_consecutive_failures {
                *self.state.write().await = ClientFailoverState::Reconnecting;
                return true; // Failover triggered
            }
        }

        false
    }

    /// Record successful connection
    pub async fn record_success(&self) {
        if let Some(ref mut server) = *self.current_server.write().await {
            server.consecutive_failures = 0;
            server.is_connected = true;
        }
        *self.state.write().await = ClientFailoverState::Connected;
        *self.reconnection_attempts.write().await = 0;
    }

    /// Get current server
    pub async fn get_current_server(&self) -> Option<ServerConnection> {
        self.current_server.read().await.clone()
    }

    /// Get current failover state
    pub async fn get_state(&self) -> ClientFailoverState {
        self.state.read().await.clone()
    }

    /// Update available servers (from server discovery)
    pub async fn update_available_servers(&self, servers: Vec<ServerCandidate>) {
        *self.available_servers.write().await = servers;
    }

    /// Get list of available servers for failover
    pub async fn get_available_servers(&self) -> Vec<ServerCandidate> {
        self.available_servers.read().await.clone()
    }

    /// Find best server for reconnection
    pub async fn find_reconnection_target(&self) -> Option<ServerCandidate> {
        let available = self.available_servers.read().await;

        if available.is_empty() {
            return None;
        }

        // Sort by priority (higher first), then by latency (lower first)
        let mut candidates = available.clone();
        candidates.sort_by(|a, b| {
            match b.priority.cmp(&a.priority) {
                std::cmp::Ordering::Equal => a.latency_ms.cmp(&b.latency_ms),
                other => other,
            }
        });

        candidates.first().cloned()
    }

    /// Attempt reconnection to a new server
    pub async fn attempt_reconnection(&self, new_server: ServerConnection) -> bool {
        let mut attempts = self.reconnection_attempts.write().await;
        *attempts += 1;

        if *attempts > 10 {
            // Too many attempts
            *self.state.write().await = ClientFailoverState::Failed;
            return false;
        }

        // In real implementation, would establish connection here
        // For now, just update state
        *self.current_server.write().await = Some(new_server);
        *self.state.write().await = ClientFailoverState::Reconnecting;

        true
    }

    /// Mark reconnection as successful
    pub async fn reconnection_successful(&self) {
        *self.state.write().await = ClientFailoverState::Recovered;
        *self.reconnection_attempts.write().await = 0;
    }

    /// Mark reconnection as failed
    pub async fn reconnection_failed(&self) {
        let attempts = self.reconnection_attempts.read().await;
        if *attempts >= 10 {
            *self.state.write().await = ClientFailoverState::Failed;
        }
    }

    /// Get reconnection attempt count
    pub async fn get_reconnection_attempts(&self) -> u32 {
        *self.reconnection_attempts.read().await
    }

    /// Reset failover state (e.g., after successful recovery)
    pub async fn reset(&self) {
        *self.state.write().await = ClientFailoverState::Failed;
        *self.reconnection_attempts.write().await = 0;
        
        if let Some(ref mut server) = *self.current_server.write().await {
            server.consecutive_failures = 0;
        }
    }
}

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

    #[tokio::test]
    async fn test_client_failover_detector_creation() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        assert_eq!(detector.get_state().await, ClientFailoverState::Failed);
    }

    #[tokio::test]
    async fn test_set_current_server() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let server = ServerConnection {
            server_id: "server1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5000,
            is_connected: true,
            last_heartbeat: 1000,
            consecutive_failures: 0,
        };

        detector.set_current_server(server.clone()).await;

        assert_eq!(detector.get_state().await, ClientFailoverState::Connected);
        assert_eq!(
            detector.get_current_server().await.unwrap().server_id,
            "server1"
        );
    }

    #[tokio::test]
    async fn test_record_failure_no_trigger() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let server = ServerConnection {
            server_id: "server1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5000,
            is_connected: true,
            last_heartbeat: 1000,
            consecutive_failures: 0,
        };

        detector.set_current_server(server).await;
        let triggered = detector.record_failure().await;

        assert!(!triggered);
        let current = detector.get_current_server().await.unwrap();
        assert_eq!(current.consecutive_failures, 1);
    }

    #[tokio::test]
    async fn test_record_failure_trigger_failover() {
        let mut config = ClientFailoverConfig::default();
        config.max_consecutive_failures = 2;

        let detector = ClientFailoverDetector::new(config);
        let server = ServerConnection {
            server_id: "server1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5000,
            is_connected: true,
            last_heartbeat: 1000,
            consecutive_failures: 0,
        };

        detector.set_current_server(server).await;
        detector.record_failure().await;
        let triggered = detector.record_failure().await;

        assert!(triggered);
        assert_eq!(detector.get_state().await, ClientFailoverState::Reconnecting);
    }

    #[tokio::test]
    async fn test_record_success() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let server = ServerConnection {
            server_id: "server1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5000,
            is_connected: true,
            last_heartbeat: 1000,
            consecutive_failures: 2,
        };

        detector.set_current_server(server).await;
        detector.record_success().await;

        let current = detector.get_current_server().await.unwrap();
        assert_eq!(current.consecutive_failures, 0);
        assert_eq!(detector.get_state().await, ClientFailoverState::Connected);
    }

    #[tokio::test]
    async fn test_find_reconnection_target() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());

        let candidates = vec![
            ServerCandidate {
                server_id: "server1".to_string(),
                address: "127.0.0.1".to_string(),
                port: 5001,
                latency_ms: 10,
                priority: 1,
            },
            ServerCandidate {
                server_id: "server2".to_string(),
                address: "127.0.0.1".to_string(),
                port: 5002,
                latency_ms: 5,
                priority: 2,
            },
        ];

        detector.update_available_servers(candidates).await;
        let target = detector.find_reconnection_target().await;

        assert!(target.is_some());
        assert_eq!(target.unwrap().server_id, "server2"); // Higher priority
    }

    #[tokio::test]
    async fn test_find_reconnection_target_empty() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        detector.update_available_servers(vec![]).await;

        let target = detector.find_reconnection_target().await;
        assert!(target.is_none());
    }

    #[tokio::test]
    async fn test_attempt_reconnection() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let new_server = ServerConnection {
            server_id: "server2".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5001,
            is_connected: true,
            last_heartbeat: 2000,
            consecutive_failures: 0,
        };

        let result = detector.attempt_reconnection(new_server.clone()).await;

        assert!(result);
        assert_eq!(detector.get_current_server().await.unwrap().server_id, "server2");
        assert_eq!(detector.get_state().await, ClientFailoverState::Reconnecting);
    }

    #[tokio::test]
    async fn test_reconnection_successful() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let new_server = ServerConnection {
            server_id: "server2".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5001,
            is_connected: true,
            last_heartbeat: 2000,
            consecutive_failures: 0,
        };

        detector.attempt_reconnection(new_server).await;
        detector.reconnection_successful().await;

        assert_eq!(detector.get_state().await, ClientFailoverState::Recovered);
        assert_eq!(detector.get_reconnection_attempts().await, 0);
    }

    #[tokio::test]
    async fn test_reset() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());
        let server = ServerConnection {
            server_id: "server1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 5000,
            is_connected: true,
            last_heartbeat: 1000,
            consecutive_failures: 5,
        };

        detector.set_current_server(server).await;
        detector.reset().await;

        let current = detector.get_current_server().await.unwrap();
        assert_eq!(current.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn test_latency_based_selection() {
        let detector = ClientFailoverDetector::new(ClientFailoverConfig::default());

        let candidates = vec![
            ServerCandidate {
                server_id: "server1".to_string(),
                address: "127.0.0.1".to_string(),
                port: 5001,
                latency_ms: 50,
                priority: 1,
            },
            ServerCandidate {
                server_id: "server2".to_string(),
                address: "127.0.0.1".to_string(),
                port: 5002,
                latency_ms: 10,
                priority: 1,
            },
        ];

        detector.update_available_servers(candidates).await;
        let target = detector.find_reconnection_target().await;

        assert!(target.is_some());
        assert_eq!(target.unwrap().server_id, "server2"); // Lower latency
    }

    #[tokio::test]
    async fn test_reconnection_failed_sets_failed_state() {
        let config = ClientFailoverConfig::default();
        let detector = ClientFailoverDetector::new(config);

        let server = ServerConnection {
            server_id: "s1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 9000,
            is_connected: true,
            last_heartbeat: 0,
            consecutive_failures: 0,
        };

        // Exhaust reconnection attempts to trigger Failed state
        for _ in 0..10 {
            detector.attempt_reconnection(server.clone()).await;
        }

        detector.reconnection_failed().await;
        assert_eq!(detector.get_state().await, ClientFailoverState::Failed);
    }

    #[tokio::test]
    async fn test_get_reconnection_attempts() {
        let config = ClientFailoverConfig::default();
        let detector = ClientFailoverDetector::new(config);

        assert_eq!(detector.get_reconnection_attempts().await, 0);

        let server = ServerConnection {
            server_id: "s1".to_string(),
            address: "127.0.0.1".to_string(),
            port: 9000,
            is_connected: true,
            last_heartbeat: 0,
            consecutive_failures: 0,
        };

        detector.attempt_reconnection(server).await;
        assert_eq!(detector.get_reconnection_attempts().await, 1);
    }
}