mockforge_chaos/
config.rs

1//! Chaos engineering configuration
2
3use serde::{Deserialize, Serialize};
4
5/// Main chaos engineering configuration
6#[derive(Debug, Clone, Serialize, Deserialize, Default)]
7pub struct ChaosConfig {
8    /// Enable chaos engineering
9    pub enabled: bool,
10    /// Latency injection configuration
11    pub latency: Option<LatencyConfig>,
12    /// Fault injection configuration
13    pub fault_injection: Option<FaultInjectionConfig>,
14    /// Rate limiting configuration
15    pub rate_limit: Option<RateLimitConfig>,
16    /// Traffic shaping configuration
17    pub traffic_shaping: Option<TrafficShapingConfig>,
18    /// Circuit breaker configuration
19    pub circuit_breaker: Option<CircuitBreakerConfig>,
20    /// Bulkhead configuration
21    pub bulkhead: Option<BulkheadConfig>,
22}
23
24/// Latency injection configuration
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct LatencyConfig {
27    /// Enable latency injection
28    pub enabled: bool,
29    /// Fixed delay in milliseconds
30    pub fixed_delay_ms: Option<u64>,
31    /// Random delay range (min, max) in milliseconds
32    pub random_delay_range_ms: Option<(u64, u64)>,
33    /// Jitter percentage (0-100)
34    pub jitter_percent: f64,
35    /// Probability of applying latency (0.0-1.0)
36    pub probability: f64,
37}
38
39impl Default for LatencyConfig {
40    fn default() -> Self {
41        Self {
42            enabled: false,
43            fixed_delay_ms: None,
44            random_delay_range_ms: None,
45            jitter_percent: 0.0,
46            probability: 1.0,
47        }
48    }
49}
50
51/// Fault injection configuration
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct FaultInjectionConfig {
54    /// Enable fault injection
55    pub enabled: bool,
56    /// HTTP error codes to inject
57    pub http_errors: Vec<u16>,
58    /// Probability of HTTP errors (0.0-1.0)
59    pub http_error_probability: f64,
60    /// Inject connection errors
61    pub connection_errors: bool,
62    /// Probability of connection errors (0.0-1.0)
63    pub connection_error_probability: f64,
64    /// Inject timeout errors
65    pub timeout_errors: bool,
66    /// Timeout duration in milliseconds
67    pub timeout_ms: u64,
68    /// Probability of timeout errors (0.0-1.0)
69    pub timeout_probability: f64,
70    /// Inject partial responses (incomplete data)
71    pub partial_responses: bool,
72    /// Probability of partial responses (0.0-1.0)
73    pub partial_response_probability: f64,
74}
75
76impl Default for FaultInjectionConfig {
77    fn default() -> Self {
78        Self {
79            enabled: false,
80            http_errors: vec![500, 502, 503, 504],
81            http_error_probability: 0.1,
82            connection_errors: false,
83            connection_error_probability: 0.05,
84            timeout_errors: false,
85            timeout_ms: 5000,
86            timeout_probability: 0.05,
87            partial_responses: false,
88            partial_response_probability: 0.05,
89        }
90    }
91}
92
93/// Rate limiting configuration
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct RateLimitConfig {
96    /// Enable rate limiting
97    pub enabled: bool,
98    /// Maximum requests per second
99    pub requests_per_second: u32,
100    /// Burst size (number of requests allowed in burst)
101    pub burst_size: u32,
102    /// Per-IP rate limiting
103    pub per_ip: bool,
104    /// Per-endpoint rate limiting
105    pub per_endpoint: bool,
106}
107
108impl Default for RateLimitConfig {
109    fn default() -> Self {
110        Self {
111            enabled: false,
112            requests_per_second: 100,
113            burst_size: 10,
114            per_ip: false,
115            per_endpoint: false,
116        }
117    }
118}
119
120/// Traffic shaping configuration
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct TrafficShapingConfig {
123    /// Enable traffic shaping
124    pub enabled: bool,
125    /// Bandwidth limit in bytes per second (0 = unlimited)
126    pub bandwidth_limit_bps: u64,
127    /// Packet loss percentage (0-100)
128    pub packet_loss_percent: f64,
129    /// Maximum concurrent connections (0 = unlimited)
130    pub max_connections: u32,
131    /// Connection timeout in milliseconds
132    pub connection_timeout_ms: u64,
133}
134
135impl Default for TrafficShapingConfig {
136    fn default() -> Self {
137        Self {
138            enabled: false,
139            bandwidth_limit_bps: 0,
140            packet_loss_percent: 0.0,
141            max_connections: 0,
142            connection_timeout_ms: 30000,
143        }
144    }
145}
146
147/// Circuit breaker configuration
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct CircuitBreakerConfig {
150    /// Enable circuit breaker
151    pub enabled: bool,
152    /// Failure threshold before opening circuit
153    pub failure_threshold: u64,
154    /// Success threshold before closing circuit from half-open
155    pub success_threshold: u64,
156    /// Timeout before attempting to close circuit (in milliseconds)
157    pub timeout_ms: u64,
158    /// Half-open request limit
159    pub half_open_max_requests: u32,
160    /// Failure rate threshold (percentage, 0-100)
161    pub failure_rate_threshold: f64,
162    /// Minimum number of requests before calculating failure rate
163    pub min_requests_for_rate: u64,
164    /// Rolling window duration for failure rate calculation (in milliseconds)
165    pub rolling_window_ms: u64,
166}
167
168impl Default for CircuitBreakerConfig {
169    fn default() -> Self {
170        Self {
171            enabled: false,
172            failure_threshold: 5,
173            success_threshold: 2,
174            timeout_ms: 60000,
175            half_open_max_requests: 3,
176            failure_rate_threshold: 50.0,
177            min_requests_for_rate: 10,
178            rolling_window_ms: 10000,
179        }
180    }
181}
182
183/// Bulkhead configuration
184#[derive(Debug, Clone, Serialize, Deserialize)]
185pub struct BulkheadConfig {
186    /// Enable bulkhead
187    pub enabled: bool,
188    /// Maximum concurrent requests
189    pub max_concurrent_requests: u32,
190    /// Maximum queue size (0 = no queue)
191    pub max_queue_size: u32,
192    /// Queue timeout in milliseconds
193    pub queue_timeout_ms: u64,
194}
195
196impl Default for BulkheadConfig {
197    fn default() -> Self {
198        Self {
199            enabled: false,
200            max_concurrent_requests: 100,
201            max_queue_size: 10,
202            queue_timeout_ms: 5000,
203        }
204    }
205}