Skip to main content

Crate connection_pool

Crate connection_pool 

Source
Expand description

Β§Connection Pool

A flexible, high-performance, generic async connection pool for Rust with background cleanup and comprehensive logging.

Crates.io Documentation License

§✨ 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 ConnectionManager trait
  • 🌐 Versatile: Suitable for TCP, database, and any custom connection pooling

Β§Quick Start

Add connection-pool to your Cargo.toml:

[dependencies]
connection-pool = "0.2"
tokio = { version = "1.47", features = ["full"] }

Then you can use the connection pool in your application:

// 1. Define your ConnectionManager
use connection_pool::{ConnectionManager, ConnectionPool};
use std::future::Future;
use std::pin::Pin;
use tokio::net::TcpStream;

#[derive(Clone)]
pub struct TcpConnectionManager {
    pub address: std::net::SocketAddr,
}

impl ConnectionManager for TcpConnectionManager {
    type Connection = TcpStream;
    type Error = std::io::Error;
    type CreateFut = Pin<Box<dyn Future<Output = Result<TcpStream, Self::Error>> + Send>>;
    type ValidFut<'a> = Pin<Box<dyn Future<Output = bool> + Send + 'a>>;

    fn create_connection(&self) -> Self::CreateFut {
        let addr = self.address;
        Box::pin(async move { TcpStream::connect(addr).await })
    }

    fn is_valid<'a>(&'a self, conn: &'a mut Self::Connection) -> Self::ValidFut<'a> {
        Box::pin(async move {
            // Lightweight socket check only; for real liveness use a protocol heartbeat.
            conn.peer_addr().is_ok()
        })
    }
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    // 2. Create a connection pool
    let manager = TcpConnectionManager { address: "127.0.0.1:8080".parse().unwrap() };
    let pool = ConnectionPool::new(
        Some(10), // max pool size
        None,     // default idle timeout
        None,     // default connection timeout
        None,     // default cleanup config
        manager,
    );

    // 3. Acquire and use a connection
    let mut conn = pool.clone().get_connection().await.unwrap();
    use tokio::io::AsyncWriteExt;
    conn.write_all(b"hello").await.unwrap();
    Ok(())
}

peer_addr() is only a cheap socket-level check. It can tell you whether the stream still exists locally, but it does not prove that the remote service is actually healthy or ready to speak your protocol.

If you need stronger validation, add an application-level heartbeat such as ping/pong, and make is_valid send and verify that message instead of touching the data stream with a read.

Β§Advanced Usage

  • You can pool any connection type (e.g. database, API client) by implementing the ConnectionManager trait.
  • For TCP, prefer a protocol-level health check when you need to prove the peer is responsive.
  • See examples/db_example.rs for a custom type example.

Β§Testing

cargo test

Β§πŸŽ›οΈ Configuration Options

ParameterDescriptionDefault
max_sizeMaximum number of connections10
max_idle_timeConnection idle timeout5 minutes
connection_timeoutConnection creation timeout10 seconds
cleanup_intervalBackground cleanup interval30 seconds
MAX_IDLE_KEEPMaximum number of idle connections kept (≀ max_size)8

max_size still governs concurrency (via the semaphore); MAX_IDLE_KEEP only bounds how many idle connections are retained for reuse. Keeping the full max_size idle made the pool validate dozens of idle connections and churn under load, so the idle reserve is deliberately small.

Β§πŸ—οΈ 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: PooledStream for automatic connection return

Β§πŸ§ͺ Testing

Run the examples to see the pool in action:

# Basic TCP example
cargo run --example echo_example

# Database connection example
cargo run --example db_example

# Background cleanup demonstration
cargo run --example background_cleanup_example

§🀝 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.

StructsΒ§

CleanupConfig
Configuration for background cleanup task
ConnectionPool
Connection pool
ManagedConnection
Pooled managed stream, provided for the outer world usage
TcpConnectionManager
Example implementation for TcpStream

EnumsΒ§

PoolError
Connection pool errors

ConstantsΒ§

MAX_IDLE_KEEP
Maximum number of idle connections kept in the pool, regardless of max_size. Bounding the idle reserve prevents the pool from hoarding dozens of idle tunnels after a burst (which then all get validated with a 3s Ping and discarded -> β€œConnection validation failed” churn).

TraitsΒ§

ConnectionManager
Manager responsible for creating new ConnectionManager::Connections or checking existing ones.

Type AliasesΒ§

TcpConnectionPool
Convenience type aliases for TCP connections
TcpManagedConnection
Managed TCP stream