sentinel_proxy/agents/
pool.rs

1//! Agent connection pooling.
2
3use std::sync::atomic::{AtomicU32, AtomicU64};
4use std::sync::Arc;
5use std::time::{Duration, Instant};
6
7use sentinel_agent_protocol::AgentClient;
8use tokio::sync::RwLock;
9
10/// Agent connection pool for efficient connection reuse.
11pub struct AgentConnectionPool {
12    /// Pool configuration
13    pub(super) max_connections: usize,
14    pub(super) min_idle: usize,
15    pub(super) max_idle: usize,
16    pub(super) idle_timeout: Duration,
17    /// Available connections
18    pub(super) connections: Arc<RwLock<Vec<AgentConnection>>>,
19    /// Active connections count
20    pub(super) active_count: AtomicU32,
21    /// Total connections created
22    pub(super) total_created: AtomicU64,
23}
24
25/// Pooled agent connection.
26pub(super) struct AgentConnection {
27    /// The actual client
28    pub client: AgentClient,
29    /// Creation time
30    pub created_at: Instant,
31    /// Last used time
32    pub last_used: Instant,
33    /// Is healthy
34    pub healthy: bool,
35}
36
37impl AgentConnectionPool {
38    /// Create a new connection pool.
39    pub fn new(
40        max_connections: usize,
41        min_idle: usize,
42        max_idle: usize,
43        idle_timeout: Duration,
44    ) -> Self {
45        Self {
46            max_connections,
47            min_idle,
48            max_idle,
49            idle_timeout,
50            connections: Arc::new(RwLock::new(Vec::new())),
51            active_count: AtomicU32::new(0),
52            total_created: AtomicU64::new(0),
53        }
54    }
55
56    /// Get active connection count.
57    pub fn active_count(&self) -> u32 {
58        self.active_count.load(std::sync::atomic::Ordering::Relaxed)
59    }
60
61    /// Get total connections created.
62    pub fn total_created(&self) -> u64 {
63        self.total_created.load(std::sync::atomic::Ordering::Relaxed)
64    }
65}