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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Connection pooling for upstream HTTP connections.
//!
//! Provides pgbouncer-style connection management for HTTP upstreams:
//! - Configurable max concurrent connections per upstream
//! - Request queueing when pool is exhausted
//! - Metrics for pool utilization and queue depth
//! - Connection recycling and health tracking
//!
//! # Pooling Mode
//!
//! - **Request**: Connection returned after each request (default, only supported mode)
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use tokio::sync::{oneshot, Semaphore, SemaphorePermit};
/// Connection pool configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionPoolConfig {
/// Maximum concurrent connections to this upstream.
/// Default: 100
#[serde(default = "default_max_connections")]
pub max_connections: usize,
/// Maximum requests waiting in queue when pool is exhausted.
/// Default: 500
#[serde(default = "default_max_queue_size")]
pub max_queue_size: usize,
/// How long a request can wait in queue before timing out.
/// Default: 30 seconds
#[serde(default = "default_queue_timeout_ms")]
pub queue_timeout_ms: u64,
/// Connection idle timeout for recycling.
/// Default: 90 seconds
#[serde(default = "default_idle_timeout_ms")]
pub idle_timeout_ms: u64,
/// Enable fair queueing (FIFO) vs allowing some priority bypass.
#[serde(default = "default_fair_queueing")]
pub fair_queueing: bool,
}
fn default_max_connections() -> usize {
100
}
fn default_max_queue_size() -> usize {
500
}
fn default_queue_timeout_ms() -> u64 {
30000
}
fn default_idle_timeout_ms() -> u64 {
90000
}
fn default_fair_queueing() -> bool {
true
}
impl Default for ConnectionPoolConfig {
fn default() -> Self {
Self {
max_connections: default_max_connections(),
max_queue_size: default_max_queue_size(),
queue_timeout_ms: default_queue_timeout_ms(),
idle_timeout_ms: default_idle_timeout_ms(),
fair_queueing: default_fair_queueing(),
}
}
}
impl ConnectionPoolConfig {
/// Create a new pool config with specified max connections.
pub fn new(max_connections: usize) -> Self {
Self {
max_connections,
..Self::default()
}
}
/// Set the queue timeout.
pub fn with_queue_timeout(mut self, timeout: Duration) -> Self {
self.queue_timeout_ms = timeout.as_millis() as u64;
self
}
/// Set the max queue size.
pub fn with_max_queue_size(mut self, size: usize) -> Self {
self.max_queue_size = size;
self
}
/// Get queue timeout as Duration.
pub fn queue_timeout(&self) -> Duration {
Duration::from_millis(self.queue_timeout_ms)
}
/// Get idle timeout as Duration.
pub fn idle_timeout(&self) -> Duration {
Duration::from_millis(self.idle_timeout_ms)
}
}
/// Pooling mode for connection management.
///
/// Only `Request` mode is supported: connection returned after each request
/// (like pgbouncer transaction mode). Most efficient for stateless REST APIs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PoolingMode {
/// Return connection after each request.
#[default]
Request,
}
impl PoolingMode {
/// Get the mode as a string.
pub fn as_str(&self) -> &'static str {
"request"
}
}
/// A waiting request in the queue.
struct WaitingRequest {
/// Channel to signal when permit is available.
ready_tx: oneshot::Sender<()>,
/// When this request started waiting.
enqueued_at: Instant,
}
/// Connection pool for a single upstream.
///
/// Manages concurrent connections to an upstream service with:
/// - Semaphore-based concurrency limiting
/// - Request queueing when pool is exhausted
/// - Metrics for monitoring
pub struct ConnectionPool {
/// Semaphore to limit concurrent connections.
permits: Arc<Semaphore>,
/// Waiting queue for requests when pool is exhausted.
waiting: Mutex<VecDeque<WaitingRequest>>,
/// Pool configuration.
config: ConnectionPoolConfig,
/// Metrics.
stats: ConnectionPoolStats,
}
/// Connection pool statistics.
#[derive(Debug, Default)]
pub struct ConnectionPoolStats {
/// Current active connections.
pub active: AtomicUsize,
/// Current queue depth.
pub queued: AtomicUsize,
/// Total requests processed.
pub total_requests: AtomicU64,
/// Requests that acquired a permit immediately.
pub immediate_permits: AtomicU64,
/// Requests that had to wait in queue.
pub queued_requests: AtomicU64,
/// Requests that timed out waiting.
pub queue_timeouts: AtomicU64,
/// Requests rejected due to queue full.
pub queue_rejections: AtomicU64,
/// Total wait time in microseconds (for averaging).
wait_time_us: AtomicU64,
/// Max wait time observed in microseconds.
max_wait_time_us: AtomicU64,
}
impl ConnectionPoolStats {
/// Get a snapshot of the stats.
pub fn snapshot(&self) -> ConnectionPoolSnapshot {
let total = self.total_requests.load(Ordering::Relaxed);
let queued = self.queued_requests.load(Ordering::Relaxed);
let wait_sum = self.wait_time_us.load(Ordering::Relaxed);
ConnectionPoolSnapshot {
active_connections: self.active.load(Ordering::Relaxed),
queue_depth: self.queued.load(Ordering::Relaxed),
total_requests: total,
immediate_permits: self.immediate_permits.load(Ordering::Relaxed),
queued_requests: queued,
queue_timeouts: self.queue_timeouts.load(Ordering::Relaxed),
queue_rejections: self.queue_rejections.load(Ordering::Relaxed),
avg_wait_time_ms: (wait_sum.checked_div(queued).unwrap_or(0)) as f64 / 1000.0,
max_wait_time_ms: self.max_wait_time_us.load(Ordering::Relaxed) as f64 / 1000.0,
utilization: if total > 0 {
1.0 - (self.immediate_permits.load(Ordering::Relaxed) as f64 / total as f64)
} else {
0.0
},
}
}
fn record_wait_time(&self, duration: Duration) {
let us = duration.as_micros() as u64;
self.wait_time_us.fetch_add(us, Ordering::Relaxed);
// Update max (compare-and-swap loop)
let mut current_max = self.max_wait_time_us.load(Ordering::Relaxed);
while us > current_max {
match self.max_wait_time_us.compare_exchange_weak(
current_max,
us,
Ordering::Relaxed,
Ordering::Relaxed,
) {
Ok(_) => break,
Err(actual) => current_max = actual,
}
}
}
}
/// Point-in-time snapshot of pool stats.
#[derive(Debug, Clone, Serialize)]
pub struct ConnectionPoolSnapshot {
/// Current active connections.
pub active_connections: usize,
/// Current queue depth.
pub queue_depth: usize,
/// Total requests processed.
pub total_requests: u64,
/// Requests that acquired permit immediately.
pub immediate_permits: u64,
/// Requests that had to wait in queue.
pub queued_requests: u64,
/// Requests that timed out waiting.
pub queue_timeouts: u64,
/// Requests rejected due to queue full.
pub queue_rejections: u64,
/// Average wait time in milliseconds.
pub avg_wait_time_ms: f64,
/// Maximum wait time in milliseconds.
pub max_wait_time_ms: f64,
/// Pool utilization (0.0 = always available, 1.0 = always queued).
pub utilization: f64,
}
/// Error acquiring a connection from the pool.
#[derive(Debug, Clone)]
pub enum PoolError {
/// Queue is full, request rejected.
QueueFull,
/// Timed out waiting for a connection.
Timeout,
/// Pool is shutting down.
Shutdown,
}
impl std::fmt::Display for PoolError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::QueueFull => write!(f, "Connection pool queue is full"),
Self::Timeout => write!(f, "Timed out waiting for connection"),
Self::Shutdown => write!(f, "Connection pool is shutting down"),
}
}
}
impl std::error::Error for PoolError {}
/// Guard that returns the permit to the pool when dropped.
pub struct PooledConnection<'a> {
_permit: SemaphorePermit<'a>,
pool: &'a ConnectionPool,
acquired_at: Instant,
}
impl Drop for PooledConnection<'_> {
fn drop(&mut self) {
// Decrement active count
self.pool.stats.active.fetch_sub(1, Ordering::Relaxed);
// Wake next waiter if any
self.pool.wake_next_waiter();
}
}
impl<'a> PooledConnection<'a> {
/// Get how long this connection has been held.
pub fn held_duration(&self) -> Duration {
self.acquired_at.elapsed()
}
}
impl ConnectionPool {
/// Create a new connection pool with the given configuration.
pub fn new(config: ConnectionPoolConfig) -> Self {
Self {
permits: Arc::new(Semaphore::new(config.max_connections)),
waiting: Mutex::new(VecDeque::new()),
config,
stats: ConnectionPoolStats::default(),
}
}
/// Create a pool with default configuration.
pub fn with_max_connections(max: usize) -> Self {
Self::new(ConnectionPoolConfig::new(max))
}
/// Get the pool configuration.
pub fn config(&self) -> &ConnectionPoolConfig {
&self.config
}
/// Get current pool statistics.
pub fn stats(&self) -> ConnectionPoolSnapshot {
self.stats.snapshot()
}
/// Get current active connection count.
pub fn active_connections(&self) -> usize {
self.stats.active.load(Ordering::Relaxed)
}
/// Get current queue depth.
pub fn queue_depth(&self) -> usize {
self.stats.queued.load(Ordering::Relaxed)
}
/// Check if the pool has available capacity.
pub fn has_capacity(&self) -> bool {
self.permits.available_permits() > 0
}
/// Acquire a connection from the pool.
///
/// If a permit is available, returns immediately.
/// Otherwise, queues the request and waits up to `queue_timeout`.
///
/// # Errors
///
/// Returns `PoolError::QueueFull` if the queue is at capacity.
/// Returns `PoolError::Timeout` if waiting times out.
pub async fn acquire(&self) -> Result<PooledConnection<'_>, PoolError> {
self.stats.total_requests.fetch_add(1, Ordering::Relaxed);
// Try to acquire immediately
match self.permits.try_acquire() {
Ok(permit) => {
self.stats.immediate_permits.fetch_add(1, Ordering::Relaxed);
self.stats.active.fetch_add(1, Ordering::Relaxed);
return Ok(PooledConnection {
_permit: permit,
pool: self,
acquired_at: Instant::now(),
});
}
Err(_) => {
// Pool exhausted, need to queue
}
}
// Check queue capacity and enqueue in a single lock acquisition
let (tx, rx) = oneshot::channel();
let enqueued_at = Instant::now();
{
let mut queue = self.waiting.lock();
if queue.len() >= self.config.max_queue_size {
self.stats.queue_rejections.fetch_add(1, Ordering::Relaxed);
return Err(PoolError::QueueFull);
}
queue.push_back(WaitingRequest {
ready_tx: tx,
enqueued_at,
});
self.stats.queued.fetch_add(1, Ordering::Relaxed);
}
self.stats.queued_requests.fetch_add(1, Ordering::Relaxed);
// Wait for signal or timeout
let timeout = self.config.queue_timeout();
match tokio::time::timeout(timeout, rx).await {
Ok(Ok(())) => {
// Signaled, try to acquire
let wait_time = enqueued_at.elapsed();
self.stats.record_wait_time(wait_time);
match self.permits.try_acquire() {
Ok(permit) => {
self.stats.active.fetch_add(1, Ordering::Relaxed);
Ok(PooledConnection {
_permit: permit,
pool: self,
acquired_at: Instant::now(),
})
}
Err(_) => {
// Shouldn't happen if signaling is correct
self.stats.queue_timeouts.fetch_add(1, Ordering::Relaxed);
Err(PoolError::Timeout)
}
}
}
Ok(Err(_)) => {
// Channel closed, pool shutting down
self.stats.queued.fetch_sub(1, Ordering::Relaxed);
Err(PoolError::Shutdown)
}
Err(_) => {
// Timeout — stale entry will be skipped in wake_next_waiter()
// when the closed channel is detected (R12: removed useless lock)
self.stats.queue_timeouts.fetch_add(1, Ordering::Relaxed);
self.stats.queued.fetch_sub(1, Ordering::Relaxed);
Err(PoolError::Timeout)
}
}
}
/// Wake the next waiter in the queue.
fn wake_next_waiter(&self) {
let mut queue = self.waiting.lock();
let timeout = self.config.queue_timeout();
while let Some(waiter) = queue.pop_front() {
self.stats.queued.fetch_sub(1, Ordering::Relaxed);
// Check if waiter has already timed out
if waiter.enqueued_at.elapsed() > timeout {
// Skip stale waiters
continue;
}
// Try to signal
if waiter.ready_tx.send(()).is_ok() {
// Successfully signaled
return;
}
// Receiver dropped (timed out), try next
}
}
/// Drain the queue, rejecting all waiters.
///
/// Called during shutdown.
pub fn drain_queue(&self) {
let mut queue = self.waiting.lock();
while queue.pop_front().is_some() {
self.stats.queued.fetch_sub(1, Ordering::Relaxed);
}
}
/// Get max connections setting.
pub fn max_connections(&self) -> usize {
self.config.max_connections
}
/// Get available permits.
pub fn available_permits(&self) -> usize {
self.permits.available_permits()
}
}
// =============================================================================
// Tests
// =============================================================================
#[cfg(test)]
#[path = "tests/connection_pool_tests.rs"]
mod tests;