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	pub created_at:Instant,
19	pub last_used:Instant,
20	pub health_score:f64,
21	pub error_count:usize,
22	pub successful_operations:usize,
23	pub total_operations:usize,
24	pub is_active:bool,
25	pub reuse_count:u32,
26	pub health:ConnectionHealth::Enum,
27}
28
29impl Struct {
30	pub fn new() -> Self {
31		Self {
32			id:Uuid::new_v4().to_string(),
33			created_at:Instant::now(),
34			last_used:Instant::now(),
35			health_score:100.0,
36			error_count:0,
37			successful_operations:0,
38			total_operations:0,
39			is_active:true,
40			reuse_count:0,
41			health:ConnectionHealth::Enum::Healthy,
42		}
43	}
44
45	pub fn update_health(&mut self, success:bool) {
46		self.last_used = Instant::now();
47		self.total_operations += 1;
48
49		if success {
50			self.successful_operations += 1;
51			self.health_score = (self.health_score + 2.0).min(100.0);
52			self.error_count = 0;
53		} else {
54			self.error_count += 1;
55			self.health_score = (self.health_score - 10.0).max(0.0);
56		}
57
58		let success_rate = if self.total_operations > 0 {
59			self.successful_operations as f64 / self.total_operations as f64
60		} else {
61			1.0
62		};
63
64		self.health_score = (self.health_score * 0.7 + success_rate * 100.0 * 0.3).max(0.0).min(100.0);
65	}
66
67	pub fn is_healthy(&self) -> bool {
68		self.health_score > 50.0 && self.error_count < 5 && self.is_active && self.age().as_secs() < 300
69	}
70
71	pub fn age(&self) -> Duration { self.created_at.elapsed() }
72
73	pub fn idle_time(&self) -> Duration { self.last_used.elapsed() }
74
75	pub fn success_rate(&self) -> f64 {
76		if self.total_operations == 0 {
77			1.0
78		} else {
79			self.successful_operations as f64 / self.total_operations as f64
80		}
81	}
82}