sentinel_proxy/agents/
pool.rs1use std::sync::atomic::{AtomicU32, AtomicU64};
4use std::sync::Arc;
5use std::time::{Duration, Instant};
6
7use sentinel_agent_protocol::AgentClient;
8use tokio::sync::RwLock;
9use tracing::{debug, trace};
10
11pub struct AgentConnectionPool {
13 pub(super) max_connections: usize,
15 pub(super) min_idle: usize,
16 pub(super) max_idle: usize,
17 pub(super) idle_timeout: Duration,
18 pub(super) connections: Arc<RwLock<Vec<AgentConnection>>>,
20 pub(super) active_count: AtomicU32,
22 pub(super) total_created: AtomicU64,
24}
25
26pub(super) struct AgentConnection {
28 pub client: AgentClient,
30 pub created_at: Instant,
32 pub last_used: Instant,
34 pub healthy: bool,
36}
37
38impl AgentConnectionPool {
39 pub fn new(
41 max_connections: usize,
42 min_idle: usize,
43 max_idle: usize,
44 idle_timeout: Duration,
45 ) -> Self {
46 trace!(
47 max_connections = max_connections,
48 min_idle = min_idle,
49 max_idle = max_idle,
50 idle_timeout_secs = idle_timeout.as_secs(),
51 "Creating agent connection pool"
52 );
53
54 debug!(
55 max_connections = max_connections,
56 "Agent connection pool initialized"
57 );
58
59 Self {
60 max_connections,
61 min_idle,
62 max_idle,
63 idle_timeout,
64 connections: Arc::new(RwLock::new(Vec::new())),
65 active_count: AtomicU32::new(0),
66 total_created: AtomicU64::new(0),
67 }
68 }
69
70 pub fn active_count(&self) -> u32 {
72 let count = self.active_count.load(std::sync::atomic::Ordering::Relaxed);
73 trace!(active_connections = count, "Retrieved active connection count");
74 count
75 }
76
77 pub fn total_created(&self) -> u64 {
79 let total = self.total_created.load(std::sync::atomic::Ordering::Relaxed);
80 trace!(total_created = total, "Retrieved total connections created");
81 total
82 }
83}