mockforge-registry-server 0.3.130

Plugin registry server for MockForge
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Circuit breaker pattern for external service resilience
//!
//! Prevents cascade failures when external dependencies (Redis, S3, email) are unhealthy.
//! The circuit breaker transitions between states:
//! - CLOSED: Normal operation, requests go through
//! - OPEN: Too many failures, requests are immediately rejected
//! - HALF-OPEN: After recovery timeout, allow test requests to check if service recovered

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, warn};

/// Circuit breaker state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Normal operation - requests pass through
    Closed,
    /// Circuit is open - requests are rejected immediately
    Open,
    /// Testing if service has recovered
    HalfOpen,
}

impl std::fmt::Display for CircuitState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CircuitState::Closed => write!(f, "CLOSED"),
            CircuitState::Open => write!(f, "OPEN"),
            CircuitState::HalfOpen => write!(f, "HALF-OPEN"),
        }
    }
}

/// Configuration for circuit breaker behavior
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of failures before opening the circuit
    pub failure_threshold: u32,
    /// Duration to keep circuit open before testing recovery
    pub recovery_timeout: Duration,
    /// Number of successful requests in half-open state to close circuit
    pub success_threshold: u32,
    /// Window for tracking failures (failures outside window are forgotten)
    pub failure_window: Duration,
    /// Name for logging purposes
    pub name: String,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 5,
            recovery_timeout: Duration::from_secs(30),
            success_threshold: 2,
            failure_window: Duration::from_secs(60),
            name: "default".to_string(),
        }
    }
}

impl CircuitBreakerConfig {
    /// Create a new config with the given name
    pub fn with_name(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Set failure threshold
    pub fn failure_threshold(mut self, threshold: u32) -> Self {
        self.failure_threshold = threshold;
        self
    }

    /// Set recovery timeout
    pub fn recovery_timeout(mut self, timeout: Duration) -> Self {
        self.recovery_timeout = timeout;
        self
    }

    /// Set success threshold for half-open state
    pub fn success_threshold(mut self, threshold: u32) -> Self {
        self.success_threshold = threshold;
        self
    }

    /// Set failure tracking window
    pub fn failure_window(mut self, window: Duration) -> Self {
        self.failure_window = window;
        self
    }
}

/// Error returned when circuit is open
#[derive(Debug, Clone)]
pub struct CircuitOpenError {
    pub service_name: String,
    pub time_until_retry: Duration,
}

impl std::fmt::Display for CircuitOpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Circuit breaker open for '{}': service unavailable, retry in {:?}",
            self.service_name, self.time_until_retry
        )
    }
}

impl std::error::Error for CircuitOpenError {}

/// Internal state tracking
struct CircuitBreakerState {
    state: CircuitState,
    failure_count: u32,
    success_count: u32,
    last_failure_time: Option<Instant>,
    last_state_change: Instant,
}

/// Circuit breaker implementation
#[derive(Clone)]
pub struct CircuitBreaker {
    config: Arc<CircuitBreakerConfig>,
    state: Arc<RwLock<CircuitBreakerState>>,
    // Atomic counters for metrics
    total_calls: Arc<AtomicU64>,
    total_failures: Arc<AtomicU64>,
    total_rejections: Arc<AtomicU64>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker with the given configuration
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config: Arc::new(config),
            state: Arc::new(RwLock::new(CircuitBreakerState {
                state: CircuitState::Closed,
                failure_count: 0,
                success_count: 0,
                last_failure_time: None,
                last_state_change: Instant::now(),
            })),
            total_calls: Arc::new(AtomicU64::new(0)),
            total_failures: Arc::new(AtomicU64::new(0)),
            total_rejections: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Get the current circuit state
    pub async fn state(&self) -> CircuitState {
        let state = self.state.read().await;
        self.effective_state(&state)
    }

    /// Get metrics for this circuit breaker
    pub fn metrics(&self) -> CircuitBreakerMetrics {
        CircuitBreakerMetrics {
            name: self.config.name.clone(),
            total_calls: self.total_calls.load(Ordering::Relaxed),
            total_failures: self.total_failures.load(Ordering::Relaxed),
            total_rejections: self.total_rejections.load(Ordering::Relaxed),
        }
    }

    /// Check if a request should be allowed through
    /// Returns Ok(()) if allowed, Err with time until retry if circuit is open
    pub async fn check(&self) -> Result<(), CircuitOpenError> {
        let state = self.state.read().await;
        let effective_state = self.effective_state(&state);

        match effective_state {
            CircuitState::Closed => Ok(()),
            CircuitState::HalfOpen => Ok(()), // Allow test requests
            CircuitState::Open => {
                self.total_rejections.fetch_add(1, Ordering::Relaxed);
                let elapsed = state.last_state_change.elapsed();
                let time_until_retry = self.config.recovery_timeout.saturating_sub(elapsed);
                Err(CircuitOpenError {
                    service_name: self.config.name.clone(),
                    time_until_retry,
                })
            }
        }
    }

    /// Execute a fallible operation with circuit breaker protection
    pub async fn call<F, T, E>(&self, operation: F) -> Result<T, CircuitBreakerError<E>>
    where
        F: std::future::Future<Output = Result<T, E>>,
        E: std::error::Error,
    {
        self.total_calls.fetch_add(1, Ordering::Relaxed);

        // Check if circuit allows the request
        self.check().await.map_err(CircuitBreakerError::Open)?;

        // Execute the operation
        match operation.await {
            Ok(result) => {
                self.record_success().await;
                Ok(result)
            }
            Err(e) => {
                self.record_failure().await;
                Err(CircuitBreakerError::ServiceError(e))
            }
        }
    }

    /// Record a successful operation
    pub async fn record_success(&self) {
        let mut state = self.state.write().await;
        let effective_state = self.effective_state(&state);

        match effective_state {
            CircuitState::Closed => {
                // Reset failure count on success in closed state
                if state.failure_count > 0 {
                    debug!(
                        "Circuit breaker '{}': success in closed state, resetting failure count",
                        self.config.name
                    );
                }
                state.failure_count = 0;
            }
            CircuitState::HalfOpen => {
                state.success_count += 1;
                debug!(
                    "Circuit breaker '{}': success in half-open state ({}/{})",
                    self.config.name, state.success_count, self.config.success_threshold
                );

                if state.success_count >= self.config.success_threshold {
                    debug!(
                        "Circuit breaker '{}': closing circuit after {} successful requests",
                        self.config.name, state.success_count
                    );
                    state.state = CircuitState::Closed;
                    state.failure_count = 0;
                    state.success_count = 0;
                    state.last_state_change = Instant::now();
                }
            }
            CircuitState::Open => {
                // Should not happen since we check before calling
            }
        }
    }

    /// Record a failed operation
    pub async fn record_failure(&self) {
        self.total_failures.fetch_add(1, Ordering::Relaxed);
        let mut state = self.state.write().await;
        let effective_state = self.effective_state(&state);

        match effective_state {
            CircuitState::Closed => {
                // Check if previous failures are outside the window
                if let Some(last_failure) = state.last_failure_time {
                    if last_failure.elapsed() > self.config.failure_window {
                        state.failure_count = 0;
                    }
                }

                state.failure_count += 1;
                state.last_failure_time = Some(Instant::now());

                debug!(
                    "Circuit breaker '{}': failure in closed state ({}/{})",
                    self.config.name, state.failure_count, self.config.failure_threshold
                );

                if state.failure_count >= self.config.failure_threshold {
                    warn!(
                        "Circuit breaker '{}': opening circuit after {} failures",
                        self.config.name, state.failure_count
                    );
                    state.state = CircuitState::Open;
                    state.last_state_change = Instant::now();
                }
            }
            CircuitState::HalfOpen => {
                // Any failure in half-open immediately opens the circuit
                warn!(
                    "Circuit breaker '{}': failure in half-open state, re-opening circuit",
                    self.config.name
                );
                state.state = CircuitState::Open;
                state.success_count = 0;
                state.last_state_change = Instant::now();
            }
            CircuitState::Open => {
                // Already open, no action needed
            }
        }
    }

    /// Calculate the effective state, considering recovery timeout
    fn effective_state(&self, state: &CircuitBreakerState) -> CircuitState {
        match state.state {
            CircuitState::Open => {
                // Check if recovery timeout has elapsed
                if state.last_state_change.elapsed() >= self.config.recovery_timeout {
                    CircuitState::HalfOpen
                } else {
                    CircuitState::Open
                }
            }
            other => other,
        }
    }

    /// Force the circuit to close (for testing or manual intervention)
    pub async fn force_close(&self) {
        let mut state = self.state.write().await;
        warn!("Circuit breaker '{}': manually closing circuit", self.config.name);
        state.state = CircuitState::Closed;
        state.failure_count = 0;
        state.success_count = 0;
        state.last_state_change = Instant::now();
    }

    /// Force the circuit to open (for testing or manual intervention)
    pub async fn force_open(&self) {
        let mut state = self.state.write().await;
        warn!("Circuit breaker '{}': manually opening circuit", self.config.name);
        state.state = CircuitState::Open;
        state.last_state_change = Instant::now();
    }
}

/// Error type that wraps either a circuit open error or the underlying service error
#[derive(Debug)]
pub enum CircuitBreakerError<E> {
    /// Circuit is open, request was not attempted
    Open(CircuitOpenError),
    /// The underlying service returned an error
    ServiceError(E),
}

impl<E: std::fmt::Display> std::fmt::Display for CircuitBreakerError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CircuitBreakerError::Open(e) => write!(f, "{}", e),
            CircuitBreakerError::ServiceError(e) => write!(f, "{}", e),
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for CircuitBreakerError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CircuitBreakerError::Open(e) => Some(e),
            CircuitBreakerError::ServiceError(e) => Some(e),
        }
    }
}

/// Metrics for circuit breaker monitoring
#[derive(Debug, Clone)]
pub struct CircuitBreakerMetrics {
    pub name: String,
    pub total_calls: u64,
    pub total_failures: u64,
    pub total_rejections: u64,
}

/// Registry of circuit breakers for different services
#[derive(Clone, Default)]
pub struct CircuitBreakerRegistry {
    breakers: Arc<RwLock<std::collections::HashMap<String, CircuitBreaker>>>,
}

impl CircuitBreakerRegistry {
    /// Create a new registry
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a circuit breaker
    pub async fn register(&self, name: impl Into<String>, breaker: CircuitBreaker) {
        let name = name.into();
        let mut breakers = self.breakers.write().await;
        breakers.insert(name, breaker);
    }

    /// Get a circuit breaker by name
    pub async fn get(&self, name: &str) -> Option<CircuitBreaker> {
        let breakers = self.breakers.read().await;
        breakers.get(name).cloned()
    }

    /// Get or create a circuit breaker with default config
    pub async fn get_or_create(&self, name: impl Into<String>) -> CircuitBreaker {
        let name = name.into();
        let breakers = self.breakers.read().await;
        if let Some(breaker) = breakers.get(&name) {
            return breaker.clone();
        }
        drop(breakers);

        let breaker = CircuitBreaker::new(CircuitBreakerConfig::with_name(&name));
        let mut breakers = self.breakers.write().await;
        breakers.entry(name).or_insert(breaker).clone()
    }

    /// Get all circuit breaker metrics
    pub async fn all_metrics(&self) -> Vec<CircuitBreakerMetrics> {
        let breakers = self.breakers.read().await;
        breakers.values().map(|b| b.metrics()).collect()
    }

    /// Get all circuit breaker states
    pub async fn all_states(&self) -> Vec<(String, CircuitState)> {
        let breakers = self.breakers.read().await;
        let mut states = Vec::new();
        for (name, breaker) in breakers.iter() {
            let state = breaker.state().await;
            states.push((name.clone(), state));
        }
        states
    }
}

/// Predefined circuit breaker configurations for common services
pub mod presets {
    use super::*;

    /// Circuit breaker config for Redis (fast recovery, low threshold)
    pub fn redis() -> CircuitBreakerConfig {
        CircuitBreakerConfig::with_name("redis")
            .failure_threshold(3)
            .recovery_timeout(Duration::from_secs(10))
            .success_threshold(2)
            .failure_window(Duration::from_secs(30))
    }

    /// Circuit breaker config for S3 (slower operations, higher threshold)
    pub fn s3() -> CircuitBreakerConfig {
        CircuitBreakerConfig::with_name("s3")
            .failure_threshold(5)
            .recovery_timeout(Duration::from_secs(30))
            .success_threshold(2)
            .failure_window(Duration::from_secs(60))
    }

    /// Circuit breaker config for email service (external API, longer recovery)
    pub fn email() -> CircuitBreakerConfig {
        CircuitBreakerConfig::with_name("email")
            .failure_threshold(3)
            .recovery_timeout(Duration::from_secs(60))
            .success_threshold(1)
            .failure_window(Duration::from_secs(120))
    }

    /// Circuit breaker config for database (critical service, conservative)
    pub fn database() -> CircuitBreakerConfig {
        CircuitBreakerConfig::with_name("database")
            .failure_threshold(10)
            .recovery_timeout(Duration::from_secs(15))
            .success_threshold(3)
            .failure_window(Duration::from_secs(60))
    }
}

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

    #[tokio::test]
    async fn test_circuit_starts_closed() {
        let breaker = CircuitBreaker::new(CircuitBreakerConfig::with_name("test"));
        assert_eq!(breaker.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_opens_after_failures() {
        let config = CircuitBreakerConfig::with_name("test").failure_threshold(3);
        let breaker = CircuitBreaker::new(config);

        // Record failures
        breaker.record_failure().await;
        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Closed);

        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_circuit_rejects_when_open() {
        let config = CircuitBreakerConfig::with_name("test")
            .failure_threshold(1)
            .recovery_timeout(Duration::from_secs(60));
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        assert!(breaker.check().await.is_err());
    }

    #[tokio::test]
    async fn test_circuit_transitions_to_half_open() {
        let config = CircuitBreakerConfig::with_name("test")
            .failure_threshold(1)
            .recovery_timeout(Duration::from_millis(10));
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Open);

        // Wait for recovery timeout
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert_eq!(breaker.state().await, CircuitState::HalfOpen);
    }

    #[tokio::test]
    async fn test_circuit_closes_after_successes_in_half_open() {
        let config = CircuitBreakerConfig::with_name("test")
            .failure_threshold(1)
            .recovery_timeout(Duration::from_millis(10))
            .success_threshold(2);
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        tokio::time::sleep(Duration::from_millis(20)).await;

        assert_eq!(breaker.state().await, CircuitState::HalfOpen);

        breaker.record_success().await;
        assert_eq!(breaker.state().await, CircuitState::HalfOpen);

        breaker.record_success().await;
        assert_eq!(breaker.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_failure_in_half_open_reopens_circuit() {
        let config = CircuitBreakerConfig::with_name("test")
            .failure_threshold(1)
            .recovery_timeout(Duration::from_millis(10));
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        tokio::time::sleep(Duration::from_millis(20)).await;

        assert_eq!(breaker.state().await, CircuitState::HalfOpen);

        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_success_resets_failure_count() {
        let config = CircuitBreakerConfig::with_name("test").failure_threshold(3);
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        breaker.record_failure().await;
        breaker.record_success().await;

        // Failure count should be reset, need 3 more failures to open
        breaker.record_failure().await;
        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Closed);

        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_force_close() {
        let config = CircuitBreakerConfig::with_name("test").failure_threshold(1);
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        assert_eq!(breaker.state().await, CircuitState::Open);

        breaker.force_close().await;
        assert_eq!(breaker.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_force_open() {
        let breaker = CircuitBreaker::new(CircuitBreakerConfig::with_name("test"));
        assert_eq!(breaker.state().await, CircuitState::Closed);

        breaker.force_open().await;
        assert_eq!(breaker.state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_metrics() {
        let config = CircuitBreakerConfig::with_name("test").failure_threshold(5);
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure().await;
        breaker.record_success().await;

        let metrics = breaker.metrics();
        assert_eq!(metrics.name, "test");
        assert_eq!(metrics.total_failures, 1);
    }

    #[tokio::test]
    async fn test_registry() {
        let registry = CircuitBreakerRegistry::new();

        let redis_breaker = CircuitBreaker::new(presets::redis());
        registry.register("redis", redis_breaker).await;

        let s3_breaker = CircuitBreaker::new(presets::s3());
        registry.register("s3", s3_breaker).await;

        assert!(registry.get("redis").await.is_some());
        assert!(registry.get("s3").await.is_some());
        assert!(registry.get("nonexistent").await.is_none());
    }

    #[tokio::test]
    async fn test_registry_get_or_create() {
        let registry = CircuitBreakerRegistry::new();

        let breaker1 = registry.get_or_create("test").await;
        let breaker2 = registry.get_or_create("test").await;

        // Should be the same circuit breaker
        breaker1.record_failure().await;
        assert_eq!(breaker1.metrics().total_failures, 1);
        assert_eq!(breaker2.metrics().total_failures, 1);
    }
}