Connection Pool
A flexible, 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
- ๐งฉ Extensible: Custom connection types and validation logic via the
ConnectionManagertrait - ๐ Versatile: Suitable for TCP, database, and any custom connection pooling
Quick Start
// 1. Define your ConnectionManager
use ;
use Future;
use Pin;
use TcpStream;
async
Advanced Usage
- You can pool any connection type (e.g. database, API client) by implementing the
ConnectionManagertrait. - See
examples/db_example.rsfor a custom type example.
Testing
๐๏ธ 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 is now based on a single ConnectionManager abstraction:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ConnectionPool<M: Manager> โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข Holds a user-defined ConnectionManager โ
โ โข Manages a queue of pooled connections โ
โ โข Semaphore for max concurrent connections โ
โ โข Background cleanup for idle connections โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโดโโโโโโโโโโ
โ โ
โโโโโโโผโโโโโโ โโโโโโโโโโโผโโโโโโโโโ
โ Semaphore โ โ Background Task โ
โ (Limits โ โ (Cleans up idle โ
โ max conn)โ โ connections) โ
โโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโผโโโโโโโโโโโโโ
โ Connection Queue โ
โ (VecDeque) โ
โโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโผโโโโโโโโโโโโโ
โ PooledStream โ
โ (RAII wrapper โ
โ auto-returns) โ
โโโโโโโโโโโโโโโโโโโโ
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
๐งช Testing
Run the examples to see the pool in action:
# Basic TCP example
# Database connection example
# Background cleanup demonstration
๐ค 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.