eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
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
//! Circuit breaker pattern implementation for fault tolerance
//!
//! This module provides a circuit breaker to prevent cascading failures
//! by temporarily stopping requests to a failing service and allowing
//! recovery detection through periodic health checks.

use crate::error_categories::{ErrorCategorizer, ErrorCategory};
use crate::yahoo_error::YahooError;
use log::{debug, warn, info, error};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;

/// Circuit breaker states
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CircuitState {
    /// Normal operation - requests are allowed through
    Closed,
    /// Failure threshold exceeded - requests are blocked
    Open,
    /// Testing recovery - limited requests allowed to test service health
    HalfOpen,
}

/// Configuration for circuit breaker behavior
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of failures before opening the circuit
    pub failure_threshold: u32,
    /// Number of successful requests to close the circuit when half-open
    pub success_threshold: u32,
    /// Time to wait before attempting recovery (in milliseconds)
    pub recovery_timeout_ms: u64,
    /// Maximum number of requests allowed in half-open state
    pub half_open_max_requests: u32,
    /// Window size for failure rate calculation (in milliseconds)
    pub failure_rate_window_ms: u64,
    /// Minimum number of requests before failure rate is calculated
    pub minimum_request_volume: u32,
    /// Whether to consider different error categories differently
    pub categorize_failures: bool,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 5,
            success_threshold: 3,
            recovery_timeout_ms: 30000, // 30 seconds
            half_open_max_requests: 3,
            failure_rate_window_ms: 60000, // 1 minute
            minimum_request_volume: 10,
            categorize_failures: true,
        }
    }
}

impl CircuitBreakerConfig {
    /// Configuration optimized for high-availability services
    pub fn high_availability() -> Self {
        Self {
            failure_threshold: 3,
            success_threshold: 5,
            recovery_timeout_ms: 10000, // 10 seconds
            half_open_max_requests: 2,
            failure_rate_window_ms: 30000, // 30 seconds
            minimum_request_volume: 5,
            categorize_failures: true,
        }
    }

    /// Configuration for fault-tolerant but less aggressive breaking
    pub fn fault_tolerant() -> Self {
        Self {
            failure_threshold: 10,
            success_threshold: 2,
            recovery_timeout_ms: 60000, // 1 minute
            half_open_max_requests: 5,
            failure_rate_window_ms: 120000, // 2 minutes
            minimum_request_volume: 20,
            categorize_failures: true,
        }
    }

    /// Configuration for development/testing (more permissive)
    pub fn development() -> Self {
        Self {
            failure_threshold: 15,
            success_threshold: 1,
            recovery_timeout_ms: 5000, // 5 seconds
            half_open_max_requests: 10,
            failure_rate_window_ms: 30000,
            minimum_request_volume: 3,
            categorize_failures: true,
        }
    }
}

/// Statistics for circuit breaker monitoring
#[derive(Debug, Clone, Default)]
pub struct CircuitBreakerStats {
    pub total_requests: u64,
    pub successful_requests: u64,
    pub failed_requests: u64,
    pub rejected_requests: u64,
    pub state_transitions: u64,
    pub last_failure_time: Option<u64>,
    pub last_success_time: Option<u64>,
    pub current_consecutive_failures: u32,
    pub current_consecutive_successes: u32,
}

impl CircuitBreakerStats {
    pub fn failure_rate(&self) -> f64 {
        if self.total_requests == 0 {
            0.0
        } else {
            self.failed_requests as f64 / self.total_requests as f64
        }
    }

    pub fn success_rate(&self) -> f64 {
        1.0 - self.failure_rate()
    }
}

/// Circuit breaker implementation
pub struct CircuitBreaker {
    config: CircuitBreakerConfig,
    state: Arc<RwLock<CircuitState>>,
    stats: Arc<RwLock<CircuitBreakerStats>>,
    failure_count: Arc<AtomicU32>,
    success_count: Arc<AtomicU32>,
    last_failure_time: Arc<AtomicU64>,
    half_open_requests: Arc<AtomicU32>,
}

impl CircuitBreaker {
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            state: Arc::new(RwLock::new(CircuitState::Closed)),
            stats: Arc::new(RwLock::new(CircuitBreakerStats::default())),
            failure_count: Arc::new(AtomicU32::new(0)),
            success_count: Arc::new(AtomicU32::new(0)),
            last_failure_time: Arc::new(AtomicU64::new(0)),
            half_open_requests: Arc::new(AtomicU32::new(0)),
        }
    }

    pub fn with_default_config() -> Self {
        Self::new(CircuitBreakerConfig::default())
    }

    /// Get current circuit breaker state
    pub async fn state(&self) -> CircuitState {
        *self.state.read().await
    }

    /// Get circuit breaker statistics
    pub async fn stats(&self) -> CircuitBreakerStats {
        self.stats.read().await.clone()
    }

    /// Execute an operation through the circuit breaker
    pub async fn execute<F, Fut, T>(&self, operation: F) -> Result<T, YahooError>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T, YahooError>>,
    {
        // Check if request should be allowed
        if !self.should_allow_request().await {
            let mut stats = self.stats.write().await;
            stats.rejected_requests += 1;
            
            error!("Circuit breaker is OPEN - rejecting request");
            return Err(YahooError::FetchFailed(
                "Circuit breaker is open - service appears to be down".to_string()
            ));
        }

        // Track half-open requests
        let current_state = self.state().await;
        if current_state == CircuitState::HalfOpen {
            self.half_open_requests.fetch_add(1, Ordering::SeqCst);
        }

        // Execute the operation
        let result = operation().await;

        // Handle the result
        match &result {
            Ok(_) => {
                self.on_success().await;
            }
            Err(error) => {
                self.on_failure(error).await;
            }
        }

        // Decrement half-open request counter
        if current_state == CircuitState::HalfOpen {
            self.half_open_requests.fetch_sub(1, Ordering::SeqCst);
        }

        result
    }

    /// Check if a request should be allowed through
    async fn should_allow_request(&self) -> bool {
        let current_state = *self.state.read().await;
        
        match current_state {
            CircuitState::Closed => true,
            CircuitState::Open => {
                // Check if recovery timeout has elapsed
                let last_failure = self.last_failure_time.load(Ordering::SeqCst);
                let now = current_time_millis();
                
                if now.saturating_sub(last_failure) >= self.config.recovery_timeout_ms {
                    debug!("Recovery timeout elapsed, transitioning to half-open");
                    self.transition_to_half_open().await;
                    true
                } else {
                    false
                }
            }
            CircuitState::HalfOpen => {
                // Allow limited requests for testing
                self.half_open_requests.load(Ordering::SeqCst) < self.config.half_open_max_requests
            }
        }
    }

    /// Handle successful operation
    async fn on_success(&self) {
        let mut stats = self.stats.write().await;
        stats.total_requests += 1;
        stats.successful_requests += 1;
        stats.last_success_time = Some(current_time_millis());
        stats.current_consecutive_successes += 1;
        stats.current_consecutive_failures = 0;
        drop(stats);

        let success_count = self.success_count.fetch_add(1, Ordering::SeqCst) + 1;
        self.failure_count.store(0, Ordering::SeqCst);

        let current_state = self.state().await;
        
        if current_state == CircuitState::HalfOpen && success_count >= self.config.success_threshold {
            info!("Success threshold met in half-open state, closing circuit");
            self.transition_to_closed().await;
        }

        debug!("Operation succeeded (consecutive successes: {})", success_count);
    }

    /// Handle failed operation
    async fn on_failure(&self, error: &YahooError) {
        let mut stats = self.stats.write().await;
        stats.total_requests += 1;
        stats.failed_requests += 1;
        stats.last_failure_time = Some(current_time_millis());
        stats.current_consecutive_failures += 1;
        stats.current_consecutive_successes = 0;
        drop(stats);

        // Check if failure should count towards circuit breaking
        let should_count = if self.config.categorize_failures {
            let error_info = error.categorize_error();
            // Only count failures that indicate service issues, not client errors
            matches!(
                error_info.category,
                ErrorCategory::Transient | ErrorCategory::ServerError | ErrorCategory::RateLimit
            )
        } else {
            true
        };

        if !should_count {
            debug!("Failure not counted towards circuit breaking: {}", error);
            return;
        }

        let failure_count = self.failure_count.fetch_add(1, Ordering::SeqCst) + 1;
        self.success_count.store(0, Ordering::SeqCst);
        self.last_failure_time.store(current_time_millis(), Ordering::SeqCst);

        warn!("Operation failed (consecutive failures: {}): {}", failure_count, error);

        let current_state = self.state().await;
        
        match current_state {
            CircuitState::Closed => {
                if failure_count >= self.config.failure_threshold {
                    warn!("Failure threshold exceeded, opening circuit");
                    self.transition_to_open().await;
                }
            }
            CircuitState::HalfOpen => {
                warn!("Failure in half-open state, reopening circuit");
                self.transition_to_open().await;
            }
            CircuitState::Open => {
                // Already open, just update counters
                debug!("Additional failure while circuit is open");
            }
        }
    }

    /// Transition to closed state
    async fn transition_to_closed(&self) {
        let mut state = self.state.write().await;
        *state = CircuitState::Closed;
        drop(state);

        let mut stats = self.stats.write().await;
        stats.state_transitions += 1;
        drop(stats);

        self.failure_count.store(0, Ordering::SeqCst);
        self.success_count.store(0, Ordering::SeqCst);
        
        info!("Circuit breaker transitioned to CLOSED");
    }

    /// Transition to open state
    async fn transition_to_open(&self) {
        let mut state = self.state.write().await;
        *state = CircuitState::Open;
        drop(state);

        let mut stats = self.stats.write().await;
        stats.state_transitions += 1;
        drop(stats);
        
        error!("Circuit breaker transitioned to OPEN");
    }

    /// Transition to half-open state
    async fn transition_to_half_open(&self) {
        let mut state = self.state.write().await;
        *state = CircuitState::HalfOpen;
        drop(state);

        let mut stats = self.stats.write().await;
        stats.state_transitions += 1;
        drop(stats);

        self.success_count.store(0, Ordering::SeqCst);
        self.half_open_requests.store(0, Ordering::SeqCst);
        
        info!("Circuit breaker transitioned to HALF-OPEN");
    }

    /// Manually reset the circuit breaker to closed state
    pub async fn reset(&self) {
        info!("Manually resetting circuit breaker");
        self.transition_to_closed().await;
    }

    /// Force the circuit breaker to open (for testing/maintenance)
    pub async fn force_open(&self) {
        warn!("Manually forcing circuit breaker to OPEN state");
        self.transition_to_open().await;
    }

    /// Check if the circuit breaker is currently open (synchronous check)
    /// 
    /// Note: This performs a try_read which may return false positives if the lock is held.
    /// For definitive state checking, use the async `state()` method.
    pub fn is_open(&self) -> bool {
        self.state.try_read()
            .map(|state| *state == CircuitState::Open)
            .unwrap_or(false)
    }

    /// Manually record a successful operation for testing purposes
    /// 
    /// This bypasses the normal `execute()` flow and directly updates internal counters.
    /// Useful for integration tests that need to control circuit breaker state.
    pub async fn record_success(&self) {
        self.on_success().await;
    }

    /// Manually record a failed operation for testing purposes
    /// 
    /// This bypasses the normal `execute()` flow and directly updates internal counters.
    /// Useful for integration tests that need to control circuit breaker state.
    pub async fn record_failure(&self, error: &YahooError) {
        self.on_failure(error).await;
    }
}

/// Get current time in milliseconds since Unix epoch
fn current_time_millis() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or(Duration::ZERO)
        .as_millis() as u64
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::time::{sleep, Duration};

    #[tokio::test]
    async fn test_circuit_breaker_closed_state() {
        let config = CircuitBreakerConfig {
            failure_threshold: 2,
            ..Default::default()
        };
        let cb = CircuitBreaker::new(config);
        
        // Should start in closed state
        assert_eq!(cb.state().await, CircuitState::Closed);
        
        // Successful request should work
        let result = cb.execute(|| async { Ok::<_, YahooError>("success") }).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_circuit_breaker_opens_after_failures() {
        let config = CircuitBreakerConfig {
            failure_threshold: 2,
            recovery_timeout_ms: 100,
            ..Default::default()
        };
        let cb = CircuitBreaker::new(config);
        
        // First failure
        let _ = cb.execute(|| async { 
            Err::<(), _>(YahooError::FetchFailed("test failure".to_string())) 
        }).await;
        assert_eq!(cb.state().await, CircuitState::Closed);
        
        // Second failure should open the circuit
        let _ = cb.execute(|| async { 
            Err::<(), _>(YahooError::FetchFailed("test failure".to_string())) 
        }).await;
        assert_eq!(cb.state().await, CircuitState::Open);
        
        // Third request should be rejected
        let result = cb.execute(|| async { Ok::<_, YahooError>("should be rejected") }).await;
        assert!(result.is_err());
        assert_eq!(cb.state().await, CircuitState::Open);
    }

    #[tokio::test]
    async fn test_circuit_breaker_recovery() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            recovery_timeout_ms: 50, // Short timeout for testing
            success_threshold: 1,
            ..Default::default()
        };
        let cb = CircuitBreaker::new(config);
        
        // Cause failure to open circuit
        let _ = cb.execute(|| async { 
            Err::<(), _>(YahooError::FetchFailed("test failure".to_string())) 
        }).await;
        assert_eq!(cb.state().await, CircuitState::Open);
        
        // Wait for recovery timeout
        sleep(Duration::from_millis(60)).await;
        
        // Next request should transition to half-open and succeed
        let result = cb.execute(|| async { Ok::<_, YahooError>("recovery success") }).await;
        assert!(result.is_ok());
        assert_eq!(cb.state().await, CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_stats() {
        let cb = CircuitBreaker::with_default_config();
        
        // Execute some operations
        let _ = cb.execute(|| async { Ok::<_, YahooError>("success1") }).await;
        let _ = cb.execute(|| async { Ok::<_, YahooError>("success2") }).await;
        let _ = cb.execute(|| async { 
            Err::<(), _>(YahooError::FetchFailed("failure".to_string())) 
        }).await;
        
        let stats = cb.stats().await;
        assert_eq!(stats.total_requests, 3);
        assert_eq!(stats.successful_requests, 2);
        assert_eq!(stats.failed_requests, 1);
        assert!((stats.failure_rate() - 0.333).abs() < 0.01);
    }
}