Skip to main content

Mountain/IPC/Enhanced/ConnectionPool/
ConnectionHandle.rs

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