code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Synchronization primitives for Code Mesh Core
//!
//! This module provides cross-platform synchronization primitives that work
//! consistently across native and WASM environments.

use crate::{Error, Result};
use std::sync::Arc;
use std::time::{Duration, Instant};

#[cfg(feature = "native")]
use tokio::sync::{Mutex as TokioMutex, RwLock as TokioRwLock};

#[cfg(feature = "wasm")]
use parking_lot::{Mutex as ParkingMutex, RwLock as ParkingRwLock};

/// Cross-platform async mutex
#[cfg(feature = "native")]
pub type AsyncMutex<T> = TokioMutex<T>;

#[cfg(feature = "wasm")]
pub type AsyncMutex<T> = ParkingMutex<T>;

/// Cross-platform async read-write lock
#[cfg(feature = "native")]
pub type AsyncRwLock<T> = TokioRwLock<T>;

#[cfg(feature = "wasm")]
pub type AsyncRwLock<T> = ParkingRwLock<T>;

/// Debouncer for rate-limiting operations
pub struct Debouncer {
    last_execution: Arc<AsyncMutex<Option<Instant>>>,
    delay: Duration,
}

impl Debouncer {
    /// Create a new debouncer with the specified delay
    pub fn new(delay: Duration) -> Self {
        Self {
            last_execution: Arc::new(AsyncMutex::new(None)),
            delay,
        }
    }

    /// Execute a function with debouncing
    #[cfg(feature = "native")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<Option<T>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        let now = Instant::now();
        let mut last_exec = self.last_execution.lock().await;
        
        if let Some(last) = *last_exec {
            if now.duration_since(last) < self.delay {
                return Ok(None);
            }
        }
        
        *last_exec = Some(now);
        drop(last_exec);
        
        f().await.map(Some)
    }

    /// Execute a function with debouncing (WASM version)
    #[cfg(feature = "wasm")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<Option<T>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        let now = Instant::now();
        let mut last_exec = self.last_execution.lock();
        
        if let Some(last) = *last_exec {
            if now.duration_since(last) < self.delay {
                return Ok(None);
            }
        }
        
        *last_exec = Some(now);
        drop(last_exec);
        
        f().await.map(Some)
    }

    /// Check if enough time has passed since the last execution
    #[cfg(feature = "native")]
    pub async fn should_execute(&self) -> bool {
        let now = Instant::now();
        let last_exec = self.last_execution.lock().await;
        
        if let Some(last) = *last_exec {
            now.duration_since(last) >= self.delay
        } else {
            true
        }
    }

    /// Check if enough time has passed since the last execution (WASM version)
    #[cfg(feature = "wasm")]
    pub async fn should_execute(&self) -> bool {
        let now = Instant::now();
        let last_exec = self.last_execution.lock();
        
        if let Some(last) = *last_exec {
            now.duration_since(last) >= self.delay
        } else {
            true
        }
    }
}

/// Rate limiter for controlling operation frequency
pub struct RateLimiter {
    tokens: Arc<AsyncMutex<f64>>,
    max_tokens: f64,
    refill_rate: f64, // tokens per second
    last_refill: Arc<AsyncMutex<Instant>>,
}

impl RateLimiter {
    /// Create a new rate limiter
    pub fn new(max_tokens: f64, refill_rate: f64) -> Self {
        Self {
            tokens: Arc::new(AsyncMutex::new(max_tokens)),
            max_tokens,
            refill_rate,
            last_refill: Arc::new(AsyncMutex::new(Instant::now())),
        }
    }

    /// Try to acquire a token (non-blocking)
    #[cfg(feature = "native")]
    pub async fn try_acquire(&self, tokens: f64) -> bool {
        self.refill_tokens().await;
        
        let mut current_tokens = self.tokens.lock().await;
        if *current_tokens >= tokens {
            *current_tokens -= tokens;
            true
        } else {
            false
        }
    }

    /// Try to acquire a token (non-blocking, WASM version)
    #[cfg(feature = "wasm")]
    pub async fn try_acquire(&self, tokens: f64) -> bool {
        self.refill_tokens().await;
        
        let mut current_tokens = self.tokens.lock();
        if *current_tokens >= tokens {
            *current_tokens -= tokens;
            true
        } else {
            false
        }
    }

    /// Acquire a token (blocking until available)
    #[cfg(feature = "native")]
    pub async fn acquire(&self, tokens: f64) -> Result<()> {
        loop {
            if self.try_acquire(tokens).await {
                return Ok(());
            }
            
            // Calculate wait time
            let wait_time = Duration::from_secs_f64(tokens / self.refill_rate);
            tokio::time::sleep(wait_time).await;
        }
    }

    /// Acquire a token (blocking until available, WASM version)
    #[cfg(feature = "wasm")]
    pub async fn acquire(&self, tokens: f64) -> Result<()> {
        loop {
            if self.try_acquire(tokens).await {
                return Ok(());
            }
            
            // In WASM, we can't really sleep, so we yield
            wasm_bindgen_futures::JsFuture::from(
                js_sys::Promise::resolve(&wasm_bindgen::JsValue::UNDEFINED)
            ).await.map_err(|_| Error::Other(anyhow::anyhow!("JS Promise failed")))?;
        }
    }

    /// Refill tokens based on elapsed time
    #[cfg(feature = "native")]
    async fn refill_tokens(&self) {
        let now = Instant::now();
        let mut last_refill = self.last_refill.lock().await;
        let elapsed = now.duration_since(*last_refill).as_secs_f64();
        
        if elapsed > 0.0 {
            let mut tokens = self.tokens.lock().await;
            let new_tokens = (*tokens + elapsed * self.refill_rate).min(self.max_tokens);
            *tokens = new_tokens;
            *last_refill = now;
        }
    }

    /// Refill tokens based on elapsed time (WASM version)
    #[cfg(feature = "wasm")]
    async fn refill_tokens(&self) {
        let now = Instant::now();
        let mut last_refill = self.last_refill.lock();
        let elapsed = now.duration_since(*last_refill).as_secs_f64();
        
        if elapsed > 0.0 {
            let mut tokens = self.tokens.lock();
            let new_tokens = (*tokens + elapsed * self.refill_rate).min(self.max_tokens);
            *tokens = new_tokens;
            *last_refill = now;
        }
    }

    /// Get current token count
    #[cfg(feature = "native")]
    pub async fn tokens(&self) -> f64 {
        self.refill_tokens().await;
        *self.tokens.lock().await
    }

    /// Get current token count (WASM version)
    #[cfg(feature = "wasm")]
    pub async fn tokens(&self) -> f64 {
        self.refill_tokens().await;
        *self.tokens.lock()
    }
}

/// Circuit breaker for handling failures gracefully
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

pub struct CircuitBreaker {
    state: Arc<AsyncMutex<CircuitState>>,
    failure_count: Arc<AsyncMutex<u32>>,
    success_count: Arc<AsyncMutex<u32>>,
    failure_threshold: u32,
    success_threshold: u32,
    timeout: Duration,
    last_failure: Arc<AsyncMutex<Option<Instant>>>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker
    pub fn new(failure_threshold: u32, success_threshold: u32, timeout: Duration) -> Self {
        Self {
            state: Arc::new(AsyncMutex::new(CircuitState::Closed)),
            failure_count: Arc::new(AsyncMutex::new(0)),
            success_count: Arc::new(AsyncMutex::new(0)),
            failure_threshold,
            success_threshold,
            timeout,
            last_failure: Arc::new(AsyncMutex::new(None)),
        }
    }

    /// Execute a function with circuit breaker protection
    #[cfg(feature = "native")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        // Check if circuit is open
        let state = *self.state.lock().await;
        match state {
            CircuitState::Open => {
                let last_failure = *self.last_failure.lock().await;
                if let Some(failure_time) = last_failure {
                    if Instant::now().duration_since(failure_time) >= self.timeout {
                        // Transition to half-open
                        *self.state.lock().await = CircuitState::HalfOpen;
                        *self.success_count.lock().await = 0;
                    } else {
                        return Err(Error::Other(anyhow::anyhow!("Circuit breaker is open")));
                    }
                }
            }
            CircuitState::HalfOpen => {
                // Allow limited requests
            }
            CircuitState::Closed => {
                // Normal operation
            }
        }

        // Execute the function
        match f().await {
            Ok(result) => {
                self.on_success().await;
                Ok(result)
            }
            Err(error) => {
                self.on_failure().await;
                Err(error)
            }
        }
    }

    /// Execute a function with circuit breaker protection (WASM version)
    #[cfg(feature = "wasm")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        // Check if circuit is open
        let state = *self.state.lock();
        match state {
            CircuitState::Open => {
                let last_failure = *self.last_failure.lock();
                if let Some(failure_time) = last_failure {
                    if Instant::now().duration_since(failure_time) >= self.timeout {
                        // Transition to half-open
                        *self.state.lock() = CircuitState::HalfOpen;
                        *self.success_count.lock() = 0;
                    } else {
                        return Err(Error::Other(anyhow::anyhow!("Circuit breaker is open")));
                    }
                }
            }
            CircuitState::HalfOpen => {
                // Allow limited requests
            }
            CircuitState::Closed => {
                // Normal operation
            }
        }

        // Execute the function
        match f().await {
            Ok(result) => {
                self.on_success().await;
                Ok(result)
            }
            Err(error) => {
                self.on_failure().await;
                Err(error)
            }
        }
    }

    /// Handle successful execution
    #[cfg(feature = "native")]
    async fn on_success(&self) {
        let state = *self.state.lock().await;
        match state {
            CircuitState::HalfOpen => {
                let mut success_count = self.success_count.lock().await;
                *success_count += 1;
                if *success_count >= self.success_threshold {
                    *self.state.lock().await = CircuitState::Closed;
                    *self.failure_count.lock().await = 0;
                }
            }
            CircuitState::Closed => {
                *self.failure_count.lock().await = 0;
            }
            _ => {}
        }
    }

    /// Handle successful execution (WASM version)
    #[cfg(feature = "wasm")]
    async fn on_success(&self) {
        let state = *self.state.lock();
        match state {
            CircuitState::HalfOpen => {
                let mut success_count = self.success_count.lock();
                *success_count += 1;
                if *success_count >= self.success_threshold {
                    *self.state.lock() = CircuitState::Closed;
                    *self.failure_count.lock() = 0;
                }
            }
            CircuitState::Closed => {
                *self.failure_count.lock() = 0;
            }
            _ => {}
        }
    }

    /// Handle failed execution
    #[cfg(feature = "native")]
    async fn on_failure(&self) {
        let mut failure_count = self.failure_count.lock().await;
        *failure_count += 1;
        *self.last_failure.lock().await = Some(Instant::now());

        if *failure_count >= self.failure_threshold {
            *self.state.lock().await = CircuitState::Open;
        }
    }

    /// Handle failed execution (WASM version)
    #[cfg(feature = "wasm")]
    async fn on_failure(&self) {
        let mut failure_count = self.failure_count.lock();
        *failure_count += 1;
        *self.last_failure.lock() = Some(Instant::now());

        if *failure_count >= self.failure_threshold {
            *self.state.lock() = CircuitState::Open;
        }
    }

    /// Get current circuit state
    #[cfg(feature = "native")]
    pub async fn state(&self) -> CircuitState {
        *self.state.lock().await
    }

    /// Get current circuit state (WASM version)
    #[cfg(feature = "wasm")]
    pub async fn state(&self) -> CircuitState {
        *self.state.lock()
    }
}

/// Timeout wrapper for operations
pub struct TimeoutWrapper {
    timeout: Duration,
}

impl TimeoutWrapper {
    /// Create a new timeout wrapper
    pub fn new(timeout: Duration) -> Self {
        Self { timeout }
    }

    /// Execute a function with timeout
    #[cfg(feature = "native")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        match tokio::time::timeout(self.timeout, f()).await {
            Ok(result) => result,
            Err(_) => Err(Error::Other(anyhow::anyhow!("Operation timed out"))),
        }
    }

    /// Execute a function with timeout (WASM version - simplified)
    #[cfg(feature = "wasm")]
    pub async fn execute<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        // WASM doesn't have real timeouts, so we just execute the function
        f().await
    }
}

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

    #[cfg(feature = "native")]
    #[tokio::test]
    async fn test_debouncer() {
        let debouncer = Debouncer::new(Duration::from_millis(100));
        let mut counter = 0;

        // First call should execute
        let result = debouncer.execute(|| async {
            counter += 1;
            Ok(counter)
        }).await.unwrap();
        assert_eq!(result, Some(1));

        // Immediate second call should be debounced
        let result = debouncer.execute(|| async {
            counter += 1;
            Ok(counter)
        }).await.unwrap();
        assert_eq!(result, None);

        // Wait for debounce period
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Now it should execute again
        let result = debouncer.execute(|| async {
            counter += 1;
            Ok(counter)
        }).await.unwrap();
        assert_eq!(result, Some(2));
    }

    #[cfg(feature = "native")]
    #[tokio::test]
    async fn test_rate_limiter() {
        let limiter = RateLimiter::new(2.0, 1.0); // 2 tokens, refill 1 per second

        // Should be able to acquire 2 tokens immediately
        assert!(limiter.try_acquire(1.0).await);
        assert!(limiter.try_acquire(1.0).await);
        
        // Third token should fail
        assert!(!limiter.try_acquire(1.0).await);

        // Wait for refill
        tokio::time::sleep(Duration::from_secs(1)).await;
        
        // Should be able to acquire one more token
        assert!(limiter.try_acquire(1.0).await);
    }

    #[cfg(feature = "native")]
    #[tokio::test]
    async fn test_circuit_breaker() {
        let breaker = CircuitBreaker::new(2, 1, Duration::from_millis(100));
        
        // Should start closed
        assert_eq!(breaker.state().await, CircuitState::Closed);

        // Fail twice to open the circuit
        let _result = breaker.execute(|| async { 
            Err::<(), _>(Error::Other(anyhow::anyhow!("Test error")))
        }).await;
        let _result = breaker.execute(|| async { 
            Err::<(), _>(Error::Other(anyhow::anyhow!("Test error")))
        }).await;

        // Circuit should now be open
        assert_eq!(breaker.state().await, CircuitState::Open);

        // Wait for timeout
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Should transition to half-open on next call
        let _result = breaker.execute(|| async { Ok(()) }).await;
        assert_eq!(breaker.state().await, CircuitState::Closed);
    }
}