Mountain/IPC/Enhanced/ConnectionPool/
ConnectionHandle.rs1
2use std::time::{Duration, Instant};
9
10use uuid::Uuid;
11
12use crate::IPC::Enhanced::ConnectionPool::ConnectionHealth;
13
14#[derive(Debug, Clone)]
15pub struct Struct {
16 pub id:String,
17
18 pub created_at:Instant,
19
20 pub last_used:Instant,
21
22 pub health_score:f64,
23
24 pub error_count:usize,
25
26 pub successful_operations:usize,
27
28 pub total_operations:usize,
29
30 pub is_active:bool,
31
32 pub reuse_count:u32,
33
34 pub health:ConnectionHealth::Enum,
35}
36
37impl Struct {
38 pub fn new() -> Self {
39 Self {
40 id:Uuid::new_v4().to_string(),
41
42 created_at:Instant::now(),
43
44 last_used:Instant::now(),
45
46 health_score:100.0,
47
48 error_count:0,
49
50 successful_operations:0,
51
52 total_operations:0,
53
54 is_active:true,
55
56 reuse_count:0,
57
58 health:ConnectionHealth::Enum::Healthy,
59 }
60 }
61
62 pub fn update_health(&mut self, success:bool) {
63 self.last_used = Instant::now();
64
65 self.total_operations += 1;
66
67 if success {
68 self.successful_operations += 1;
69
70 self.health_score = (self.health_score + 2.0).min(100.0);
71
72 self.error_count = 0;
73 } else {
74 self.error_count += 1;
75
76 self.health_score = (self.health_score - 10.0).max(0.0);
77 }
78
79 let success_rate = if self.total_operations > 0 {
80 self.successful_operations as f64 / self.total_operations as f64
81 } else {
82 1.0
83 };
84
85 self.health_score = (self.health_score * 0.7 + success_rate * 100.0 * 0.3).max(0.0).min(100.0);
86 }
87
88 pub fn is_healthy(&self) -> bool {
89 self.health_score > 50.0 && self.error_count < 5 && self.is_active && self.age().as_secs() < 300
90 }
91
92 pub fn age(&self) -> Duration { self.created_at.elapsed() }
93
94 pub fn idle_time(&self) -> Duration { self.last_used.elapsed() }
95
96 pub fn success_rate(&self) -> f64 {
97 if self.total_operations == 0 {
98 1.0
99 } else {
100 self.successful_operations as f64 / self.total_operations as f64
101 }
102 }
103}