network-protocol 1.2.1

Secure, high-performance protocol core with backpressure control, structured logging, timeout handling, TLS support, and comprehensive benchmarking for robust Rust networked applications and services.
Documentation
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! # Connection Pooling
//!
//! Generic connection pool for all transport types.
//!
//! This module provides a thread-safe, async-aware connection pooling mechanism
//! that eliminates expensive TLS handshakes for repeated connections to the same
//! endpoint. Essential for database and RPC scenarios where clients make many
//! short-lived requests.
//!
//! ## Features
//! - Generic over any transport type `T`
//! - Configurable pool size (min/max connections)
//! - TTL-based connection expiration
//! - Health checks with automatic eviction
//! - FIFO acquisition with LRU eviction on overflow
//! - Thread-safe async operations

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, Semaphore};
use tracing::{debug, error, warn};

use crate::error::{ProtocolError, Result};

/// Configuration for connection pooling
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Minimum connections to maintain (pre-warmed)
    pub min_size: usize,
    /// Maximum connections in pool
    pub max_size: usize,
    /// Time-to-live for idle connections before eviction
    pub idle_timeout: Duration,
    /// Maximum lifetime of a connection regardless of idle time
    pub max_lifetime: Duration,
    /// Maximum concurrent waiters for connections (backpressure limit)
    pub max_waiters: usize,
    /// Circuit breaker: consecutive failures before opening
    pub circuit_breaker_threshold: usize,
    /// Circuit breaker: time to wait before trying again
    pub circuit_breaker_timeout: Duration,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            min_size: 5,
            max_size: 50,
            idle_timeout: Duration::from_secs(300), // 5 minutes
            max_lifetime: Duration::from_secs(3600), // 1 hour
            max_waiters: 1000,                      // Prevent OOM
            circuit_breaker_threshold: 5,
            circuit_breaker_timeout: Duration::from_secs(10),
        }
    }
}

impl PoolConfig {
    /// Validate configuration parameters
    pub fn validate(&self) -> Result<()> {
        let mut errors = Vec::new();

        // Validate pool sizes
        if self.max_size == 0 {
            errors.push("Pool max_size must be greater than 0".to_string());
        }

        if self.min_size > self.max_size {
            errors.push(format!(
                "Pool min_size ({}) cannot exceed max_size ({})",
                self.min_size, self.max_size
            ));
        }

        // Validate reasonable limits
        if self.max_size > 10_000 {
            errors.push(format!(
                "Pool max_size ({}) exceeds recommended limit (10,000)",
                self.max_size
            ));
        }

        if self.max_waiters == 0 {
            errors.push("Pool max_waiters must be greater than 0".to_string());
        }

        if self.max_waiters > 1_000_000 {
            errors.push(format!(
                "Pool max_waiters ({}) exceeds recommended limit (1,000,000)",
                self.max_waiters
            ));
        }

        // Validate timeouts
        if self.idle_timeout.is_zero() {
            errors.push("Pool idle_timeout must be greater than 0".to_string());
        }

        if self.max_lifetime.is_zero() {
            errors.push("Pool max_lifetime must be greater than 0".to_string());
        }

        if self.idle_timeout >= self.max_lifetime {
            errors.push(format!(
                "Pool idle_timeout ({:?}) should be less than max_lifetime ({:?})",
                self.idle_timeout, self.max_lifetime
            ));
        }

        if self.idle_timeout.as_secs() > 3600 {
            errors.push(format!(
                "Pool idle_timeout ({} seconds) is unusually long (recommended: < 1 hour)",
                self.idle_timeout.as_secs()
            ));
        }

        if self.max_lifetime.as_secs() > 86400 {
            errors.push(format!(
                "Pool max_lifetime ({} seconds) is unusually long (recommended: < 24 hours)",
                self.max_lifetime.as_secs()
            ));
        }

        // Validate circuit breaker settings
        if self.circuit_breaker_threshold == 0 {
            errors.push("Circuit breaker threshold must be greater than 0".to_string());
        }

        if self.circuit_breaker_threshold > 100 {
            errors.push(format!(
                "Circuit breaker threshold ({}) is unusually high (recommended: < 100)",
                self.circuit_breaker_threshold
            ));
        }

        if self.circuit_breaker_timeout.is_zero() {
            errors.push("Circuit breaker timeout must be greater than 0".to_string());
        }

        if self.circuit_breaker_timeout.as_secs() > 300 {
            errors.push(format!(
                "Circuit breaker timeout ({} seconds) is unusually long (recommended: < 5 minutes)",
                self.circuit_breaker_timeout.as_secs()
            ));
        }

        // Return aggregated errors
        if errors.is_empty() {
            Ok(())
        } else {
            Err(ProtocolError::ConfigError(format!(
                "Pool configuration validation failed:\n  - {}",
                errors.join("\n  - ")
            )))
        }
    }
}

/// Pooled connection wrapper with metadata
struct PooledConnection<T> {
    connection: T,
    created_at: Instant,
    last_used_at: Instant,
}

impl<T> PooledConnection<T> {
    fn new(connection: T) -> Self {
        let now = Instant::now();
        Self {
            connection,
            created_at: now,
            last_used_at: now,
        }
    }

    fn is_expired(&self, config: &PoolConfig) -> bool {
        let now = Instant::now();
        // Check max lifetime exceeded
        if now.duration_since(self.created_at) > config.max_lifetime {
            return true;
        }
        // Check idle timeout exceeded
        if now.duration_since(self.last_used_at) > config.idle_timeout {
            return true;
        }
        false
    }

    fn touch(&mut self) {
        self.last_used_at = Instant::now();
    }
}

/// Factory trait for creating new connections
pub trait ConnectionFactory<T>: Send + Sync {
    /// Create a new connection asynchronously
    fn create(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T>> + Send>>;

    /// Check if a connection is still healthy
    fn is_healthy(&self, _conn: &T) -> bool {
        true
    }
}

/// Pool metrics for monitoring and capacity planning
#[derive(Debug, Default)]
pub struct PoolMetrics {
    /// Total connections created
    pub connections_created: AtomicU64,
    /// Total connections reused from pool
    pub connections_reused: AtomicU64,
    /// Total connections evicted (expired/unhealthy)
    pub connections_evicted: AtomicU64,
    /// Total acquisition errors
    pub acquisition_errors: AtomicU64,
    /// Current active (checked out) connections
    pub active_connections: AtomicUsize,
    /// Current idle (in pool) connections
    pub idle_connections: AtomicUsize,
    /// Total wait time in microseconds (for avg calculation)
    pub total_wait_time_us: AtomicU64,
    /// Total successful acquisitions
    pub total_acquisitions: AtomicU64,
}

impl PoolMetrics {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn average_wait_time_us(&self) -> u64 {
        let total = self.total_acquisitions.load(Ordering::Relaxed);
        if total == 0 {
            return 0;
        }
        self.total_wait_time_us.load(Ordering::Relaxed) / total
    }

    pub fn utilization_percent(&self) -> f64 {
        let active = self.active_connections.load(Ordering::Relaxed) as f64;
        let idle = self.idle_connections.load(Ordering::Relaxed) as f64;
        let total = active + idle;
        if total == 0.0 {
            return 0.0;
        }
        (active / total) * 100.0
    }
}

/// Circuit breaker for fail-fast behavior
#[derive(Debug)]
struct CircuitBreaker {
    consecutive_failures: AtomicUsize,
    threshold: usize,
    timeout: Duration,
    opened_at: Mutex<Option<Instant>>,
}

impl CircuitBreaker {
    fn new(threshold: usize, timeout: Duration) -> Self {
        Self {
            consecutive_failures: AtomicUsize::new(0),
            threshold,
            timeout,
            opened_at: Mutex::new(None),
        }
    }

    async fn check(&self) -> Result<()> {
        let mut opened_at = self.opened_at.lock().await;
        if let Some(opened_time) = *opened_at {
            // Circuit is open, check if timeout elapsed
            if opened_time.elapsed() < self.timeout {
                return Err(ProtocolError::CircuitBreakerOpen);
            }
            // Timeout elapsed, enter half-open state
            *opened_at = None;
            self.consecutive_failures.store(0, Ordering::SeqCst);
            debug!("Circuit breaker entering half-open state");
        }
        Ok(())
    }

    async fn record_success(&self) {
        self.consecutive_failures.store(0, Ordering::SeqCst);
        let mut opened_at = self.opened_at.lock().await;
        if opened_at.is_some() {
            *opened_at = None;
            debug!("Circuit breaker closed after successful operation");
        }
    }

    async fn record_failure(&self) {
        let failures = self.consecutive_failures.fetch_add(1, Ordering::SeqCst) + 1;
        if failures >= self.threshold {
            let mut opened_at = self.opened_at.lock().await;
            *opened_at = Some(Instant::now());
            error!(
                "Circuit breaker opened after {} consecutive failures",
                failures
            );
        }
    }
}

/// Generic connection pool for any transport type
pub struct ConnectionPool<T> {
    config: PoolConfig,
    factory: Arc<dyn ConnectionFactory<T>>,
    connections: Arc<Mutex<VecDeque<PooledConnection<T>>>>,
    metrics: Arc<PoolMetrics>,
    circuit_breaker: Arc<CircuitBreaker>,
    backpressure: Arc<Semaphore>,
}

impl<T: Send + 'static> ConnectionPool<T> {
    /// Create a new connection pool
    pub fn new(factory: Arc<dyn ConnectionFactory<T>>, config: PoolConfig) -> Result<Self> {
        config.validate()?;

        let metrics = Arc::new(PoolMetrics::new());
        let circuit_breaker = Arc::new(CircuitBreaker::new(
            config.circuit_breaker_threshold,
            config.circuit_breaker_timeout,
        ));

        let pool = Self {
            config: config.clone(),
            factory: factory.clone(),
            connections: Arc::new(Mutex::new(VecDeque::new())),
            metrics: metrics.clone(),
            circuit_breaker,
            backpressure: Arc::new(Semaphore::new(config.max_waiters)),
        };

        // Spawn connection warming task
        if config.min_size > 0 {
            let factory_clone = factory;
            let connections_clone = pool.connections.clone();
            let metrics_clone = metrics;
            let min_size = config.min_size;

            tokio::spawn(async move {
                debug!("Warming connection pool with {} connections", min_size);
                for _ in 0..min_size {
                    match factory_clone.create().await {
                        Ok(conn) => {
                            let mut connections = connections_clone.lock().await;
                            connections.push_back(PooledConnection::new(conn));
                            metrics_clone
                                .connections_created
                                .fetch_add(1, Ordering::Relaxed);
                            metrics_clone
                                .idle_connections
                                .fetch_add(1, Ordering::Relaxed);
                        }
                        Err(e) => {
                            warn!("Failed to warm connection: {}", e);
                            break;
                        }
                    }
                }
                debug!("Connection pool warming complete");
            });
        }

        Ok(pool)
    }

    /// Get a connection from the pool or create a new one
    pub async fn acquire(&self) -> Result<PooledConnectionGuard<T>> {
        let start = Instant::now();

        // Enforce backpressure limit
        let _permit = self
            .backpressure
            .acquire()
            .await
            .map_err(|_| ProtocolError::PoolExhausted)?;

        // Check circuit breaker
        self.circuit_breaker.check().await?;

        let mut connections = self.connections.lock().await;

        // Try to find a valid connection in the pool (LRU: take from back)
        while let Some(mut pooled) = connections.pop_back() {
            if !pooled.is_expired(&self.config) && self.factory.is_healthy(&pooled.connection) {
                pooled.touch();
                self.metrics
                    .connections_reused
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .idle_connections
                    .fetch_sub(1, Ordering::Relaxed);
                self.metrics
                    .active_connections
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .total_acquisitions
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .total_wait_time_us
                    .fetch_add(start.elapsed().as_micros() as u64, Ordering::Relaxed);

                debug!("Reused connection from pool (LRU)");
                return Ok(PooledConnectionGuard {
                    connection: Some(pooled.connection),
                    pool: self.connections.clone(),
                    metrics: self.metrics.clone(),
                });
            }
            debug!("Evicted expired/unhealthy connection from pool");
            self.metrics
                .connections_evicted
                .fetch_add(1, Ordering::Relaxed);
            self.metrics
                .idle_connections
                .fetch_sub(1, Ordering::Relaxed);
        }

        // No valid connection found, create new one
        drop(connections); // Release lock before creating new connection

        match self.factory.create().await {
            Ok(new_conn) => {
                self.circuit_breaker.record_success().await;
                self.metrics
                    .connections_created
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .active_connections
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .total_acquisitions
                    .fetch_add(1, Ordering::Relaxed);
                self.metrics
                    .total_wait_time_us
                    .fetch_add(start.elapsed().as_micros() as u64, Ordering::Relaxed);

                debug!("Created new connection for pool");

                Ok(PooledConnectionGuard {
                    connection: Some(new_conn),
                    pool: self.connections.clone(),
                    metrics: self.metrics.clone(),
                })
            }
            Err(e) => {
                self.circuit_breaker.record_failure().await;
                self.metrics
                    .acquisition_errors
                    .fetch_add(1, Ordering::Relaxed);
                Err(e)
            }
        }
    }

    /// Get pool metrics
    pub fn metrics(&self) -> Arc<PoolMetrics> {
        self.metrics.clone()
    }

    /// Current number of connections in pool
    pub async fn size(&self) -> usize {
        self.connections.lock().await.len()
    }

    /// Clear all connections from the pool
    pub async fn clear(&self) {
        self.connections.lock().await.clear();
        debug!("Cleared all connections from pool");
    }

    /// Get pool configuration
    pub fn config(&self) -> &PoolConfig {
        &self.config
    }
}

/// RAII guard for pooled connections
///
/// Returns the connection to the pool on drop.
pub struct PooledConnectionGuard<T: Send + 'static> {
    connection: Option<T>,
    pool: Arc<Mutex<VecDeque<PooledConnection<T>>>>,
    metrics: Arc<PoolMetrics>,
}

impl<T: Send + 'static> PooledConnectionGuard<T> {
    /// Get a reference to the underlying connection
    pub fn get(&self) -> Option<&T> {
        self.connection.as_ref()
    }

    /// Get a mutable reference to the underlying connection
    pub fn get_mut(&mut self) -> Option<&mut T> {
        self.connection.as_mut()
    }

    /// Extract the connection (won't be returned to pool)
    pub fn into_inner(mut self) -> Option<T> {
        self.connection.take()
    }
}

impl<T: Send + 'static> AsRef<T> for PooledConnectionGuard<T> {
    #[allow(clippy::expect_used)] // Connection is guaranteed to exist unless into_inner() was called
    fn as_ref(&self) -> &T {
        self.connection.as_ref().expect("Connection should exist")
    }
}

impl<T: Send + 'static> AsMut<T> for PooledConnectionGuard<T> {
    #[allow(clippy::expect_used)] // Connection is guaranteed to exist unless into_inner() was called
    fn as_mut(&mut self) -> &mut T {
        self.connection.as_mut().expect("Connection should exist")
    }
}

impl<T: Send + 'static> Drop for PooledConnectionGuard<T> {
    fn drop(&mut self) {
        if let Some(conn) = self.connection.take() {
            let pool = self.pool.clone();
            let metrics = self.metrics.clone();
            let pooled = PooledConnection::new(conn);

            // Update metrics
            metrics.active_connections.fetch_sub(1, Ordering::Relaxed);

            // Try to return connection to pool (async context may not be available)
            // This spawns a background task to handle the return
            tokio::spawn(async move {
                let mut connections = pool.lock().await;
                if connections.len() < 100 {
                    // Reasonable max to prevent memory issues
                    connections.push_back(pooled);
                    metrics.idle_connections.fetch_add(1, Ordering::Relaxed);
                } else {
                    warn!("Connection pool at capacity, discarding connection");
                }
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[allow(dead_code)]
    struct TestConnection {
        id: usize,
    }

    struct TestFactory {
        counter: Arc<AtomicUsize>,
    }

    impl TestFactory {
        fn new() -> Self {
            Self {
                counter: Arc::new(AtomicUsize::new(0)),
            }
        }

        fn count(&self) -> usize {
            self.counter.load(Ordering::SeqCst)
        }
    }

    impl ConnectionFactory<TestConnection> for TestFactory {
        fn create(
            &self,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<TestConnection>> + Send>>
        {
            let id = self.counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move { Ok(TestConnection { id }) })
        }
    }

    #[tokio::test]
    async fn test_pool_creation() {
        let factory = Arc::new(TestFactory::new());
        let pool = ConnectionPool::new(
            factory.clone(),
            PoolConfig {
                min_size: 2,
                max_size: 10,
                idle_timeout: Duration::from_secs(60),
                max_lifetime: Duration::from_secs(600),
                ..Default::default()
            },
        );

        assert!(pool.is_ok());
    }

    #[tokio::test]
    #[allow(clippy::unwrap_used)] // Test code
    async fn test_pool_acquire_creates_connection() {
        let factory = Arc::new(TestFactory::new());
        let pool = ConnectionPool::new(factory.clone(), PoolConfig::default()).unwrap();

        let guard = pool.acquire().await.unwrap();
        assert!(guard.get().is_some());
        assert_eq!(factory.count(), 1);
    }

    #[tokio::test]
    async fn test_config_validation() {
        let invalid_config = PoolConfig {
            min_size: 100,
            max_size: 10,
            idle_timeout: Duration::from_secs(60),
            max_lifetime: Duration::from_secs(600),
            ..Default::default()
        };

        let factory = Arc::new(TestFactory::new());
        let result = ConnectionPool::new(factory, invalid_config);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_config_validation_zero_max_size() {
        let config = PoolConfig {
            max_size: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[tokio::test]
    async fn test_config_validation_zero_timeouts() {
        let config = PoolConfig {
            idle_timeout: Duration::from_secs(0),
            ..Default::default()
        };
        assert!(config.validate().is_err());

        let config2 = PoolConfig {
            max_lifetime: Duration::from_secs(0),
            ..Default::default()
        };
        assert!(config2.validate().is_err());
    }

    #[tokio::test]
    async fn test_config_validation_idle_exceeds_lifetime() {
        let config = PoolConfig {
            idle_timeout: Duration::from_secs(600),
            max_lifetime: Duration::from_secs(300),
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[tokio::test]
    async fn test_config_validation_circuit_breaker() {
        let config = PoolConfig {
            circuit_breaker_threshold: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());

        let config2 = PoolConfig {
            circuit_breaker_timeout: Duration::from_secs(0),
            ..Default::default()
        };
        assert!(config2.validate().is_err());
    }

    #[tokio::test]
    async fn test_config_validation_valid_config() {
        let config = PoolConfig::default();
        assert!(config.validate().is_ok());
    }
}