1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Connection pooling for Geode connections.
use std::sync::Arc;
use tokio::sync::{Mutex, Semaphore};
use crate::client::{Client, Connection};
use crate::error::{Error, Result};
/// Connection pool for managing QUIC connections
pub struct ConnectionPool {
client: Client,
connections: Arc<Mutex<Vec<Connection>>>,
semaphore: Arc<Semaphore>,
max_size: usize,
}
impl ConnectionPool {
/// Create a new connection pool.
///
/// # Panics
///
/// Panics if `max_size` is 0. A connection pool must have at least one
/// connection slot to function properly. (Gap #18: CWE-400, CWE-835)
pub fn new(host: impl Into<String>, port: u16, max_size: usize) -> Self {
assert!(
max_size > 0,
"ConnectionPool max_size must be at least 1 (was 0). \
A pool with 0 connections would deadlock on acquire()."
);
Self {
client: Client::new(host, port),
connections: Arc::new(Mutex::new(Vec::new())),
semaphore: Arc::new(Semaphore::new(max_size)),
max_size,
}
}
/// Configure to skip TLS verification
pub fn skip_verify(mut self, skip: bool) -> Self {
self.client = self.client.skip_verify(skip);
self
}
/// Set page size for queries
pub fn page_size(mut self, size: usize) -> Self {
self.client = self.client.page_size(size);
self
}
/// Acquire a connection from the pool
///
/// Returns a healthy connection from the pool, or creates a new one if needed.
/// Stale connections (those where the underlying QUIC connection has been closed)
/// are automatically discarded during acquisition.
pub async fn acquire(&self) -> Result<PooledConnection> {
let permit = Arc::clone(&self.semaphore)
.acquire_owned()
.await
.map_err(|_| Error::pool("Connection pool has been closed"))?;
// Try to get a healthy existing connection
let connection = loop {
let conn = {
let mut connections = self.connections.lock().await;
connections.pop()
};
match conn {
Some(c) if c.is_healthy() => {
// Connection is healthy, use it
break c;
}
Some(_) => {
// Connection is stale, discard it and try another
// (the connection is dropped here, cleaning up resources)
continue;
}
None => {
// No pooled connections available, create a new one
let client = self.client.clone();
break client.connect().await?;
}
}
};
Ok(PooledConnection {
connection: Some(connection),
pool: self.connections.clone(),
_permit: permit,
})
}
/// Get current pool size
pub async fn size(&self) -> usize {
self.connections.lock().await.len()
}
/// Get the maximum pool size
pub fn max_size(&self) -> usize {
self.max_size
}
}
/// A pooled connection that returns to the pool when dropped
pub struct PooledConnection {
connection: Option<Connection>,
pool: Arc<Mutex<Vec<Connection>>>,
_permit: tokio::sync::OwnedSemaphorePermit,
}
impl PooledConnection {
/// Get a reference to the underlying connection.
///
/// # Panics
///
/// Panics if called after the connection has been dropped or taken.
/// This should never happen in normal usage as the connection is only
/// taken during Drop.
pub fn inner(&self) -> &Connection {
self.connection
.as_ref()
.expect("PooledConnection invariant violated: connection was None")
}
}
impl Drop for PooledConnection {
fn drop(&mut self) {
if let Some(conn) = self.connection.take() {
// Only return healthy connections to the pool
// Stale connections are dropped, cleaning up their resources
if conn.is_healthy() {
let pool = self.pool.clone();
tokio::spawn(async move {
let mut connections = pool.lock().await;
connections.push(conn);
});
}
// If unhealthy, conn is dropped here and not returned to pool
}
}
}
impl std::ops::Deref for PooledConnection {
type Target = Connection;
fn deref(&self) -> &Self::Target {
self.connection
.as_ref()
.expect("PooledConnection invariant violated: connection was None")
}
}
impl std::ops::DerefMut for PooledConnection {
fn deref_mut(&mut self) -> &mut Self::Target {
self.connection
.as_mut()
.expect("PooledConnection invariant violated: connection was None")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_connection_pool_new() {
let pool = ConnectionPool::new("localhost", 3141, 10);
assert_eq!(pool.max_size(), 10);
}
#[test]
fn test_connection_pool_new_different_host() {
let pool = ConnectionPool::new("192.168.1.100", 8443, 5);
assert_eq!(pool.max_size(), 5);
}
#[test]
fn test_connection_pool_new_string_host() {
let host = String::from("geode.example.com");
let pool = ConnectionPool::new(host, 3141, 20);
assert_eq!(pool.max_size(), 20);
}
#[test]
fn test_connection_pool_skip_verify() {
let pool = ConnectionPool::new("localhost", 3141, 10).skip_verify(true);
// Configuration is passed through to client
assert_eq!(pool.max_size(), 10);
}
#[test]
fn test_connection_pool_skip_verify_false() {
let pool = ConnectionPool::new("localhost", 3141, 10).skip_verify(false);
assert_eq!(pool.max_size(), 10);
}
#[test]
fn test_connection_pool_page_size() {
let pool = ConnectionPool::new("localhost", 3141, 10).page_size(500);
assert_eq!(pool.max_size(), 10);
}
#[test]
fn test_connection_pool_chained_config() {
let pool = ConnectionPool::new("localhost", 3141, 10)
.skip_verify(true)
.page_size(1000);
assert_eq!(pool.max_size(), 10);
}
#[tokio::test]
async fn test_connection_pool_initial_size() {
let pool = ConnectionPool::new("localhost", 3141, 10);
// Pool starts empty
assert_eq!(pool.size().await, 0);
}
#[test]
#[should_panic(expected = "ConnectionPool max_size must be at least 1")]
fn test_connection_pool_max_size_zero_panics() {
// Gap #18: max_size=0 would cause deadlock on acquire() since semaphore
// would have 0 permits. Now properly panics at construction time.
let _pool = ConnectionPool::new("localhost", 3141, 0);
}
#[test]
fn test_connection_pool_max_size_one() {
let pool = ConnectionPool::new("localhost", 3141, 1);
assert_eq!(pool.max_size(), 1);
}
#[test]
fn test_connection_pool_max_size_large() {
let pool = ConnectionPool::new("localhost", 3141, 1000);
assert_eq!(pool.max_size(), 1000);
}
// Note: Full integration tests for acquire() and health checking require
// a running Geode server and are covered in the integration test suite.
// The health check functionality (is_healthy(), stale connection discard)
// is verified in integration tests with real connections (Gap #16).
// The following tests verify the structural aspects of PooledConnection
// without actually establishing connections.
#[test]
fn test_semaphore_permits_match_max_size() {
let pool = ConnectionPool::new("localhost", 3141, 5);
// Semaphore should have permits equal to max_size
assert_eq!(pool.semaphore.available_permits(), 5);
}
#[test]
fn test_connections_vec_initially_empty() {
let pool = ConnectionPool::new("localhost", 3141, 10);
// We can't directly access the mutex contents in a sync test,
// but we verified size() returns 0 in the async test above
assert_eq!(pool.max_size(), 10);
}
}