infernum-server 0.2.0-rc.2

HTTP API server for local LLM inference
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Circuit breaker for inference backend resilience.
//!
//! The circuit breaker pattern prevents cascading failures by temporarily
//! blocking requests to a failing service, allowing it time to recover.
//!
//! # States
//!
//! | State | Behavior |
//! |-------|----------|
//! | Closed | Normal operation, requests pass through |
//! | Open | All requests rejected immediately |
//! | HalfOpen | Limited requests allowed to test recovery |
//!
//! # Example
//!
//! ```ignore
//! use infernum_server::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};
//! use std::time::Duration;
//!
//! let config = CircuitBreakerConfig {
//!     failure_threshold: 5,
//!     reset_timeout: Duration::from_secs(30),
//!     half_open_requests: 3,
//!     ..Default::default()
//! };
//!
//! let breaker = CircuitBreaker::new(config);
//!
//! // Check if request is allowed
//! if breaker.allow_request() {
//!     match do_inference().await {
//!         Ok(_) => breaker.record_success(),
//!         Err(_) => breaker.record_failure(),
//!     }
//! }
//! ```

use std::sync::atomic::{AtomicU32, AtomicU64, AtomicU8, Ordering};
use std::time::{Duration, Instant};

/// Circuit breaker states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CircuitState {
    /// Normal operation - requests pass through.
    Closed = 0,
    /// Circuit is open - requests are rejected.
    Open = 1,
    /// Testing recovery - limited requests allowed.
    HalfOpen = 2,
}

impl CircuitState {
    /// Returns the string name of the state.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Closed => "closed",
            Self::Open => "open",
            Self::HalfOpen => "half_open",
        }
    }
}

impl From<u8> for CircuitState {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Closed,
            1 => Self::Open,
            2 => Self::HalfOpen,
            _ => Self::Closed,
        }
    }
}

/// Configuration for the circuit breaker.
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of consecutive failures before opening the circuit.
    pub failure_threshold: u32,

    /// Time to wait before attempting recovery (transition to half-open).
    pub reset_timeout: Duration,

    /// Number of successful requests required in half-open state to close circuit.
    pub half_open_requests: u32,

    /// Optional name for metrics and logging.
    pub name: String,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 5,
            reset_timeout: Duration::from_secs(30),
            half_open_requests: 3,
            name: "inference".to_string(),
        }
    }
}

impl CircuitBreakerConfig {
    /// Creates a new configuration with the given failure threshold.
    #[must_use]
    pub fn new(failure_threshold: u32, reset_timeout: Duration) -> Self {
        Self {
            failure_threshold,
            reset_timeout,
            ..Default::default()
        }
    }

    /// Creates a strict configuration (opens quickly, recovers slowly).
    #[must_use]
    pub fn strict() -> Self {
        Self {
            failure_threshold: 3,
            reset_timeout: Duration::from_secs(60),
            half_open_requests: 5,
            name: "inference".to_string(),
        }
    }

    /// Creates a lenient configuration (tolerates more failures).
    #[must_use]
    pub fn lenient() -> Self {
        Self {
            failure_threshold: 10,
            reset_timeout: Duration::from_secs(15),
            half_open_requests: 2,
            name: "inference".to_string(),
        }
    }

    /// Sets the name for this circuit breaker.
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }
}

/// Thread-safe circuit breaker for protecting inference calls.
///
/// Uses atomic operations for lock-free state management, suitable for
/// high-throughput scenarios.
#[derive(Debug)]
pub struct CircuitBreaker {
    /// Current state (0=Closed, 1=Open, 2=HalfOpen).
    state: AtomicU8,

    /// Consecutive failure count.
    failure_count: AtomicU32,

    /// Consecutive success count in half-open state.
    half_open_successes: AtomicU32,

    /// Timestamp when circuit opened (milliseconds since UNIX epoch).
    opened_at: AtomicU64,

    /// Reference instant for relative time calculations.
    start_instant: Instant,

    /// Configuration.
    config: CircuitBreakerConfig,

    // Metrics counters
    total_requests: AtomicU64,
    rejected_requests: AtomicU64,
    successful_requests: AtomicU64,
    failed_requests: AtomicU64,
    state_transitions: AtomicU64,
}

impl CircuitBreaker {
    /// Creates a new circuit breaker with the given configuration.
    #[must_use]
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            state: AtomicU8::new(CircuitState::Closed as u8),
            failure_count: AtomicU32::new(0),
            half_open_successes: AtomicU32::new(0),
            opened_at: AtomicU64::new(0),
            start_instant: Instant::now(),
            config,
            total_requests: AtomicU64::new(0),
            rejected_requests: AtomicU64::new(0),
            successful_requests: AtomicU64::new(0),
            failed_requests: AtomicU64::new(0),
            state_transitions: AtomicU64::new(0),
        }
    }

    /// Creates a circuit breaker with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(CircuitBreakerConfig::default())
    }

    /// Returns the current state of the circuit breaker.
    #[must_use]
    pub fn state(&self) -> CircuitState {
        CircuitState::from(self.state.load(Ordering::Acquire))
    }

    /// Returns the name of this circuit breaker.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.config.name
    }

    /// Returns the current failure count.
    #[must_use]
    pub fn failure_count(&self) -> u32 {
        self.failure_count.load(Ordering::Relaxed)
    }

    /// Checks if a request should be allowed.
    ///
    /// Returns `true` if the request can proceed, `false` if it should be rejected.
    #[must_use]
    pub fn allow_request(&self) -> bool {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let current_state = self.state();

        match current_state {
            CircuitState::Closed => true,
            CircuitState::Open => {
                // Check if reset timeout has elapsed
                if self.should_attempt_reset() {
                    // Transition to half-open
                    self.transition_to(CircuitState::HalfOpen);
                    true
                } else {
                    self.rejected_requests.fetch_add(1, Ordering::Relaxed);
                    false
                }
            },
            CircuitState::HalfOpen => {
                // Allow limited requests in half-open state
                true
            },
        }
    }

    /// Records a successful request.
    ///
    /// In half-open state, may transition to closed after enough successes.
    pub fn record_success(&self) {
        self.successful_requests.fetch_add(1, Ordering::Relaxed);

        let current_state = self.state();

        match current_state {
            CircuitState::Closed => {
                // Reset failure count on success
                self.failure_count.store(0, Ordering::Relaxed);
            },
            CircuitState::HalfOpen => {
                let successes = self.half_open_successes.fetch_add(1, Ordering::Relaxed) + 1;

                if successes >= self.config.half_open_requests {
                    // Enough successes, close the circuit
                    self.transition_to(CircuitState::Closed);
                }
            },
            CircuitState::Open => {
                // Should not happen, but handle gracefully
            },
        }
    }

    /// Records a failed request.
    ///
    /// May transition to open state after reaching failure threshold.
    pub fn record_failure(&self) {
        self.failed_requests.fetch_add(1, Ordering::Relaxed);

        let current_state = self.state();

        match current_state {
            CircuitState::Closed => {
                let failures = self.failure_count.fetch_add(1, Ordering::Relaxed) + 1;

                if failures >= self.config.failure_threshold {
                    self.transition_to(CircuitState::Open);
                }
            },
            CircuitState::HalfOpen => {
                // Any failure in half-open immediately reopens the circuit
                self.transition_to(CircuitState::Open);
            },
            CircuitState::Open => {
                // Already open, nothing to do
            },
        }
    }

    /// Manually resets the circuit breaker to closed state.
    pub fn reset(&self) {
        self.transition_to(CircuitState::Closed);
        self.failure_count.store(0, Ordering::Relaxed);
        self.half_open_successes.store(0, Ordering::Relaxed);
    }

    /// Returns metrics for this circuit breaker.
    #[must_use]
    pub fn metrics(&self) -> CircuitBreakerMetrics {
        CircuitBreakerMetrics {
            name: self.config.name.clone(),
            state: self.state(),
            failure_count: self.failure_count.load(Ordering::Relaxed),
            total_requests: self.total_requests.load(Ordering::Relaxed),
            rejected_requests: self.rejected_requests.load(Ordering::Relaxed),
            successful_requests: self.successful_requests.load(Ordering::Relaxed),
            failed_requests: self.failed_requests.load(Ordering::Relaxed),
            state_transitions: self.state_transitions.load(Ordering::Relaxed),
        }
    }

    /// Renders Prometheus-format metrics.
    #[must_use]
    pub fn render_prometheus_metrics(&self) -> String {
        let metrics = self.metrics();
        let name = &metrics.name;

        format!(
            r#"# HELP infernum_circuit_breaker_state Current circuit breaker state (0=closed, 1=open, 2=half_open)
# TYPE infernum_circuit_breaker_state gauge
infernum_circuit_breaker_state{{name="{name}"}} {}

# HELP infernum_circuit_breaker_failures Current consecutive failure count
# TYPE infernum_circuit_breaker_failures gauge
infernum_circuit_breaker_failures{{name="{name}"}} {}

# HELP infernum_circuit_breaker_requests_total Total requests through circuit breaker
# TYPE infernum_circuit_breaker_requests_total counter
infernum_circuit_breaker_requests_total{{name="{name}"}} {}

# HELP infernum_circuit_breaker_rejected_total Requests rejected due to open circuit
# TYPE infernum_circuit_breaker_rejected_total counter
infernum_circuit_breaker_rejected_total{{name="{name}"}} {}

# HELP infernum_circuit_breaker_transitions_total State transitions
# TYPE infernum_circuit_breaker_transitions_total counter
infernum_circuit_breaker_transitions_total{{name="{name}"}} {}
"#,
            metrics.state as u8,
            metrics.failure_count,
            metrics.total_requests,
            metrics.rejected_requests,
            metrics.state_transitions,
        )
    }

    // Internal helper methods

    fn should_attempt_reset(&self) -> bool {
        let opened_at = self.opened_at.load(Ordering::Acquire);
        if opened_at == 0 {
            return false;
        }

        let now = self.start_instant.elapsed().as_millis() as u64;
        let elapsed = now.saturating_sub(opened_at);
        elapsed >= self.config.reset_timeout.as_millis() as u64
    }

    fn transition_to(&self, new_state: CircuitState) {
        let old_state = self.state.swap(new_state as u8, Ordering::AcqRel);

        if old_state != new_state as u8 {
            self.state_transitions.fetch_add(1, Ordering::Relaxed);

            match new_state {
                CircuitState::Open => {
                    // Record when we opened (use max(1, ...) to ensure non-zero)
                    let now = self.start_instant.elapsed().as_millis() as u64;
                    self.opened_at.store(now.max(1), Ordering::Release);
                },
                CircuitState::Closed => {
                    // Reset counters
                    self.failure_count.store(0, Ordering::Relaxed);
                    self.half_open_successes.store(0, Ordering::Relaxed);
                    self.opened_at.store(0, Ordering::Relaxed);
                },
                CircuitState::HalfOpen => {
                    // Reset half-open success counter
                    self.half_open_successes.store(0, Ordering::Relaxed);
                },
            }

            tracing::info!(
                circuit_breaker = %self.config.name,
                old_state = CircuitState::from(old_state).as_str(),
                new_state = new_state.as_str(),
                "Circuit breaker state transition"
            );
        }
    }
}

/// Metrics snapshot for a circuit breaker.
#[derive(Debug, Clone)]
pub struct CircuitBreakerMetrics {
    /// Name of the circuit breaker.
    pub name: String,
    /// Current state.
    pub state: CircuitState,
    /// Current consecutive failure count.
    pub failure_count: u32,
    /// Total requests processed.
    pub total_requests: u64,
    /// Requests rejected due to open circuit.
    pub rejected_requests: u64,
    /// Successful requests.
    pub successful_requests: u64,
    /// Failed requests.
    pub failed_requests: u64,
    /// Number of state transitions.
    pub state_transitions: u64,
}

/// Error returned when circuit is open.
#[derive(Debug, Clone)]
pub struct CircuitOpenError {
    /// Name of the circuit breaker.
    pub circuit_name: String,
    /// How long until reset will be attempted.
    pub retry_after: Option<Duration>,
}

impl std::fmt::Display for CircuitOpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "circuit breaker '{}' is open", self.circuit_name)?;
        if let Some(retry_after) = self.retry_after {
            write!(f, ", retry after {:?}", retry_after)?;
        }
        Ok(())
    }
}

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

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

    #[test]
    fn test_circuit_state_default() {
        let breaker = CircuitBreaker::with_defaults();
        assert_eq!(breaker.state(), CircuitState::Closed);
    }

    #[test]
    fn test_circuit_allows_requests_when_closed() {
        let breaker = CircuitBreaker::with_defaults();
        assert!(breaker.allow_request());
        assert!(breaker.allow_request());
        assert!(breaker.allow_request());
    }

    #[test]
    fn test_circuit_opens_after_failures() {
        let config = CircuitBreakerConfig {
            failure_threshold: 3,
            reset_timeout: Duration::from_secs(30),
            half_open_requests: 2,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

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

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

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

    #[test]
    fn test_circuit_rejects_when_open() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            reset_timeout: Duration::from_secs(60), // Long timeout
            half_open_requests: 1,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);

        // Requests should be rejected
        assert!(!breaker.allow_request());
        assert!(!breaker.allow_request());
    }

    #[test]
    fn test_success_resets_failure_count() {
        let config = CircuitBreakerConfig {
            failure_threshold: 3,
            reset_timeout: Duration::from_secs(30),
            half_open_requests: 2,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure();
        breaker.record_failure();
        assert_eq!(breaker.failure_count(), 2);

        // Success resets the counter
        breaker.record_success();
        assert_eq!(breaker.failure_count(), 0);
        assert_eq!(breaker.state(), CircuitState::Closed);
    }

    #[test]
    fn test_half_open_transitions_to_closed_on_success() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            reset_timeout: Duration::from_millis(50), // Short timeout
            half_open_requests: 2,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);

        // Wait for reset timeout (generous margin)
        thread::sleep(Duration::from_millis(100));

        // Next request should transition to half-open
        assert!(breaker.allow_request());
        assert_eq!(breaker.state(), CircuitState::HalfOpen);

        // Successes in half-open
        breaker.record_success();
        assert_eq!(breaker.state(), CircuitState::HalfOpen);

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

    #[test]
    fn test_half_open_returns_to_open_on_failure() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            reset_timeout: Duration::from_millis(50),
            half_open_requests: 3,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);

        // Wait for reset timeout (generous margin)
        thread::sleep(Duration::from_millis(100));

        // Transition to half-open
        assert!(breaker.allow_request());
        assert_eq!(breaker.state(), CircuitState::HalfOpen);

        // Any failure in half-open reopens
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);
    }

    #[test]
    fn test_manual_reset() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            reset_timeout: Duration::from_secs(60),
            half_open_requests: 1,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);

        // Manual reset
        breaker.reset();
        assert_eq!(breaker.state(), CircuitState::Closed);
        assert_eq!(breaker.failure_count(), 0);
    }

    #[test]
    fn test_metrics() {
        let breaker = CircuitBreaker::with_defaults();

        let _ = breaker.allow_request();
        breaker.record_success();
        let _ = breaker.allow_request();
        breaker.record_failure();

        let metrics = breaker.metrics();
        assert_eq!(metrics.total_requests, 2);
        assert_eq!(metrics.successful_requests, 1);
        assert_eq!(metrics.failed_requests, 1);
    }

    #[test]
    fn test_prometheus_metrics_output() {
        let breaker = CircuitBreaker::with_defaults();
        let output = breaker.render_prometheus_metrics();

        assert!(output.contains("infernum_circuit_breaker_state"));
        assert!(output.contains("infernum_circuit_breaker_failures"));
        assert!(output.contains("infernum_circuit_breaker_requests_total"));
    }

    #[test]
    fn test_config_presets() {
        let strict = CircuitBreakerConfig::strict();
        assert_eq!(strict.failure_threshold, 3);
        assert_eq!(strict.reset_timeout, Duration::from_secs(60));

        let lenient = CircuitBreakerConfig::lenient();
        assert_eq!(lenient.failure_threshold, 10);
        assert_eq!(lenient.reset_timeout, Duration::from_secs(15));
    }

    #[test]
    fn test_config_with_name() {
        let config = CircuitBreakerConfig::default().with_name("gpu-inference");
        assert_eq!(config.name, "gpu-inference");
    }

    #[test]
    fn test_circuit_state_as_str() {
        assert_eq!(CircuitState::Closed.as_str(), "closed");
        assert_eq!(CircuitState::Open.as_str(), "open");
        assert_eq!(CircuitState::HalfOpen.as_str(), "half_open");
    }

    #[test]
    fn test_state_transitions_counted() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            reset_timeout: Duration::from_millis(50),
            half_open_requests: 1,
            name: "test".to_string(),
        };
        let breaker = CircuitBreaker::new(config);

        // Closed -> Open
        breaker.record_failure();
        assert_eq!(breaker.metrics().state_transitions, 1);

        // Wait for reset (generous margin)
        thread::sleep(Duration::from_millis(100));

        // Open -> HalfOpen
        let _ = breaker.allow_request();
        assert_eq!(breaker.metrics().state_transitions, 2);

        // HalfOpen -> Closed
        breaker.record_success();
        assert_eq!(breaker.metrics().state_transitions, 3);
    }

    #[test]
    fn test_concurrent_access() {
        use std::sync::Arc;

        let breaker = Arc::new(CircuitBreaker::with_defaults());
        let mut handles = vec![];

        // Spawn multiple threads doing requests
        for _ in 0..10 {
            let b = Arc::clone(&breaker);
            handles.push(thread::spawn(move || {
                for _ in 0..100 {
                    if b.allow_request() {
                        if rand_bool() {
                            b.record_success();
                        } else {
                            b.record_failure();
                        }
                    }
                }
            }));
        }

        for handle in handles {
            handle.join().expect("Thread panicked");
        }

        // Should not panic and metrics should be consistent
        let metrics = breaker.metrics();
        assert!(metrics.total_requests > 0);
    }

    // Simple pseudo-random for testing
    fn rand_bool() -> bool {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        use std::time::SystemTime;

        let mut hasher = DefaultHasher::new();
        SystemTime::now().hash(&mut hasher);
        thread::current().id().hash(&mut hasher);
        hasher.finish() % 2 == 0
    }
}