Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/Enhanced/ConnectionPool/
ConnectionHandle.rs

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