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
//! 弹性模式模块:提供熔断器和限流器等可靠性保障机制。
//!
//! # Resilience Primitives Module
//!
//! This module provides opt-in resilience patterns for building robust AI applications
//! that gracefully handle failures and protect against overload.
//!
//! ## Overview
//!
//! Resilience patterns are essential for production AI systems to:
//! - Prevent cascade failures when downstream services are unavailable
//! - Protect against API rate limit violations
//! - Provide graceful degradation under high load
//! - Enable fast failure detection and recovery
//!
//! ## Key Components
//!
//! | Component | Description |
//! |-----------|-------------|
//! | [`circuit_breaker`] | Circuit breaker pattern for failure isolation |
//! | [`rate_limiter`] | Token bucket rate limiter for throughput control |
//!
//! ## Circuit Breaker
//!
//! The circuit breaker prevents repeated calls to a failing service:
//! - **Closed**: Normal operation, requests pass through
//! - **Open**: Failures exceeded threshold, requests fail fast
//! - **Half-Open**: Testing if service has recovered
//!
//! ```rust
//! use ai_lib_contact::resilience::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};
//! use std::time::Duration;
//!
//! let config = CircuitBreakerConfig::new()
//! .with_failure_threshold(5)
//! .with_reset_timeout(Duration::from_secs(30));
//! let breaker = CircuitBreaker::new(config);
//!
//! // Check if request is allowed
//! if breaker.allow_request() {
//! // Make API call...
//! breaker.record_success();
//! }
//! ```
//!
//! ## Rate Limiter
//!
//! The rate limiter controls request throughput using the token bucket algorithm:
//!
//! ```rust,no_run
//! use ai_lib_contact::resilience::rate_limiter::{RateLimiter, RateLimiterConfig};
//!
//! #[tokio::main]
//! async fn main() {
//! let config = RateLimiterConfig::new()
//! .with_max_tokens(100)
//! .with_refill_rate(10.0); // 10 tokens per second
//! let limiter = RateLimiter::new(config);
//!
//! // Try to acquire a permit
//! if limiter.try_acquire().await {
//! // Proceed with request...
//! }
//! }
//! ```