Coverage Report

Created: 2025-10-06 12:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/jgrusewski/Work/foxhunt/common/src/traits.rs
Line
Count
Source
1
//! Common traits used across services
2
//!
3
//! This module provides shared traits that define common interfaces
4
//! for services in the Foxhunt HFT trading system.
5
6
use crate::error::CommonResult;
7
use crate::types::{ServiceStatus, Timestamp};
8
use async_trait::async_trait;
9
use serde::{Deserialize, Serialize};
10
use std::collections::HashMap;
11
12
/// Trait for configurable components
13
#[async_trait]
14
pub trait Configurable {
15
    /// Configuration type for this component
16
    type Config: Clone + Send + Sync;
17
18
    /// Apply configuration changes
19
    async fn configure(&mut self, config: Self::Config) -> CommonResult<()>;
20
21
    /// Get current configuration
22
    fn get_config(&self) -> &Self::Config;
23
24
    /// Validate configuration before applying
25
    fn validate_config(config: &Self::Config) -> CommonResult<()>;
26
}
27
28
/// Trait for health check capabilities
29
#[async_trait]
30
pub trait HealthCheck {
31
    /// Perform a health check
32
    async fn health_check(&self) -> CommonResult<HealthStatus>;
33
34
    /// Get detailed health information
35
    async fn detailed_health(&self) -> CommonResult<DetailedHealth>;
36
}
37
38
/// Health status for components
39
#[derive(Debug, Clone, Serialize, Deserialize)]
40
pub struct HealthStatus {
41
    /// Overall health status
42
    pub status: ServiceStatus,
43
    /// Timestamp of the health check
44
    pub timestamp: Timestamp,
45
    /// Optional message
46
    pub message: Option<String>,
47
}
48
49
/// Detailed health information
50
#[derive(Debug, Clone, Serialize, Deserialize)]
51
pub struct DetailedHealth {
52
    /// Basic health status
53
    pub status: HealthStatus,
54
    /// Component-specific metrics
55
    pub metrics: HashMap<String, f64>,
56
    /// Sub-component health statuses
57
    pub components: HashMap<String, HealthStatus>,
58
}
59
60
/// Trait for metrics collection
61
pub trait Metrics {
62
    /// Metrics type for this component
63
    type Metrics: Clone + Send + Sync + Serialize;
64
65
    /// Get current metrics
66
    fn get_metrics(&self) -> Self::Metrics;
67
68
    /// Reset metrics counters
69
    fn reset_metrics(&mut self);
70
}
71
72
/// Trait for service lifecycle management
73
#[async_trait]
74
pub trait Service: Send + Sync {
75
    /// Start the service
76
    async fn start(&mut self) -> CommonResult<()>;
77
78
    /// Stop the service gracefully
79
    async fn stop(&mut self) -> CommonResult<()>;
80
81
    /// Get current service status
82
    fn status(&self) -> ServiceStatus;
83
84
    /// Get service name
85
    fn name(&self) -> &str;
86
87
    /// Get service version
88
    fn version(&self) -> &str;
89
}
90
91
/// Trait for components that can be reloaded
92
#[async_trait]
93
pub trait Reloadable {
94
    /// Reload the component (hot reload)
95
    async fn reload(&mut self) -> CommonResult<()>;
96
97
    /// Check if reload is supported
98
0
    fn supports_reload(&self) -> bool {
99
0
        true
100
0
    }
101
}
102
103
/// Trait for components with graceful shutdown
104
#[async_trait]
105
pub trait GracefulShutdown {
106
    /// Initiate graceful shutdown
107
    async fn shutdown(&mut self) -> CommonResult<()>;
108
109
    /// Force shutdown (emergency stop)
110
    async fn force_shutdown(&mut self) -> CommonResult<()>;
111
112
    /// Get shutdown timeout duration in seconds
113
0
    fn shutdown_timeout_seconds(&self) -> u64 {
114
0
        30 // Default 30 seconds
115
0
    }
116
}
117
118
/// Trait for components that support circuit breaking
119
pub trait CircuitBreaker {
120
    /// Check if circuit is open
121
    fn is_circuit_open(&self) -> bool;
122
123
    /// Get failure count
124
    fn failure_count(&self) -> u64;
125
126
    /// Reset circuit breaker
127
    fn reset_circuit(&mut self);
128
}
129
130
/// Trait for rate-limited operations
131
pub trait RateLimited {
132
    /// Check if operation is allowed under rate limits
133
    fn is_allowed(&self) -> bool;
134
135
    /// Get current rate limit status
136
    fn rate_limit_status(&self) -> RateLimitStatus;
137
}
138
139
/// Rate limit status information
140
#[derive(Debug, Clone, Serialize, Deserialize)]
141
pub struct RateLimitStatus {
142
    /// Current request count in the window
143
    pub current_count: u64,
144
    /// Maximum requests allowed in the window
145
    pub max_requests: u64,
146
    /// Time window in seconds
147
    pub window_seconds: u64,
148
    /// Seconds until window resets
149
    pub reset_in_seconds: u64,
150
}