Connection Pool
A high-performance, generic async connection pool for Rust with background cleanup and comprehensive logging.
โจ Features
- ๐ High Performance: Background cleanup eliminates connection acquisition latency
- ๐ง Fully Generic: Support for any connection type (TCP, Database, HTTP, etc.)
- โก Async/Await Native: Built on tokio with modern async Rust patterns
- ๐ก๏ธ Thread Safe: Concurrent connection sharing with proper synchronization
- ๐งน Smart Cleanup: Configurable background task for expired connection removal
- ๐ Rich Logging: Comprehensive observability with configurable log levels
- ๐ Auto-Return: RAII-based automatic connection return to pool
- โฑ๏ธ Timeout Control: Configurable timeouts for connection creation
- ๐ฏ Type Safe: Compile-time guarantees with zero-cost abstractions
๐ Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["full"] }
= "0.4"
Simple TCP Connection Pool
use TcpConnectionPool;
use Duration;
use AsyncWriteExt;
async
๐๏ธ Custom Connection Types
Database Connection Example
use ;
use Future;
// Define your connection type
// Define connection parameters
// Implement connection creator
;
// Implement connection validator
;
// Create your custom pool type
type DbPool = ;
async
๐งน Background Cleanup Configuration
Control the background cleanup behavior for optimal performance:
use ;
use Duration;
let cleanup_config = CleanupConfig ;
let pool = new_tcp;
// Runtime control
pool.stop_cleanup_task.await; // stop cleanup
pool.restart_cleanup_task.await; // restart with new config
๐ Logging and Observability
Enable comprehensive logging to monitor pool behavior:
// Initialize logger (using env_logger)
from_default_env
.filter_level // or Debug for detailed info
.init;
// The pool will log:
// - Connection creation and reuse
// - Background cleanup operations
// - Error conditions and warnings
// - Performance metrics
Log levels:
ERROR: Connection creation failuresWARN: Validation failures, runtime issuesINFO: Pool creation, connection lifecycleDEBUG: Detailed operation info, cleanup resultsTRACE: Fine-grained debugging information
๐๏ธ Configuration Options
| Parameter | Description | Default |
|---|---|---|
max_size |
Maximum number of connections | 10 |
max_idle_time |
Connection idle timeout | 5 minutes |
connection_timeout |
Connection creation timeout | 10 seconds |
cleanup_interval |
Background cleanup interval | 30 seconds |
๐๏ธ Architecture
The connection pool uses a sophisticated multi-layered architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ConnectionPool<T,P,C,V> โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข Generic over connection type T โ
โ โข Parameterized by P (connection params) โ
โ โข Uses C: ConnectionCreator for connection creation โ
โ โข Uses V: ConnectionValidator for health checks โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโผโโโโโโโ โโโโโโโโโโผโโโโโโโโโโ โโโโโโโโผโโโโโโโ
โ Semaphore โ โ Background โ โ Connection โ
โ (Capacity โ โ Cleanup Task โ โ Queue โ
โ Control) โ โ (Async Worker) โ โ (VecDeque) โ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
Key Components
- Semaphore: Controls maximum concurrent connections
- Background Cleanup: Async task for removing expired connections
- Connection Queue: FIFO queue of available connections
- RAII Wrapper:
PooledStreamfor automatic connection return
๐ง Advanced Usage
Multiple Access Patterns
let conn = pool.get_connection.await?;
// Method 1: Auto-deref (most common)
conn.write_all.await?;
// Method 2: Explicit reference
let tcp_stream: &TcpStream = conn.as_ref;
tcp_stream.write_all.await?;
// Method 3: Mutable reference
let tcp_stream: &mut TcpStream = conn.as_mut;
tcp_stream.write_all.await?;
Error Handling
use PoolError;
match pool.get_connection.await
๐งช Testing
Run the examples to see the pool in action:
# Basic TCP example
# Database connection example
# Background cleanup demonstration
๐ Performance
The background cleanup mechanism provides significant performance improvements:
- Before: Every
get_connection()call scanned for expired connections - After: Background task handles cleanup,
get_connection()is much faster - Result: 50-80% reduction in connection acquisition latency under load
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.