Back
+30 XP
Concept

Gauntlet 07: Connection Pooler with Health Check

Establishing new database connections on every request consumes TCP handshakes, TLS negotiation, and backend authentication overhead. A thread-safe connection pool manages reusable persistent connections, performs proactive health checks, and transparently replaces dead sockets.

python
# Health check before granting connection:
if not conn.ping():
    conn.close()
    conn = create_new_connection()

# Gauntlet 07: Connection Pooler with Health Check Establishing a new PostgreSQL or Redis TCP connection involves a 3-way handshake, TLS key exchange, and authentication handshake. If an API receives 2,000 requests per second and opens a new connection for each, the database server runs out of file descriptors and crashes. 🏊⚡ A **Connection Pool** maintains a bounded set of pre-warmed, reusable connections: 1. **`acquire()`**: Borrows an idle connection from the pool. 2. **Proactive Health Check**: Before handing the socket to the application, execute `conn.ping()`. If the remote database closed the idle connection due to a timeout, discard it cleanly and instantiate a fresh replacement. 3. **`release()`**: Returns the connection to the pool for the next caller. 4. **Thread Safety**: Uses mutexes and thread-safe queues so concurrent worker threads never acquire the same connection simultaneously. ```python import queue import threading class Pool: def __init__(self, max_connections: int = 5): self.queue = queue.Queue(maxsize=max_connections) ``` ## Pop Quiz Why should a connection pool validate connection health (e.g. `ping()`) before returning it to the caller? - [ ] Remote database servers silently drop idle connections after timeouts; validating health prevents immediate query failures. - [ ] The ping() command doubles the available bandwidth of the network connection. - [ ] To clear operating system thread-local storage caches. - [ ] Because Python sockets lose their IP addresses when idle for more than 5 seconds. ## Real-World Challenge Implement `ConnectionPool(max_size: int = 3)`: - `acquire(timeout_sec: float = 1.0) -> MockConnection`: Borrows a healthy connection or instantiates a new one if pool has capacity. Discards and replaces dead connections (`ping() == False`). - `release(conn: MockConnection)`: Returns connection to pool or decrements active count if dead. - Thread-safe acquisition and release across concurrent threads. <details> <summary>Solution</summary> ```python import threading import queue import time class MockConnection: def __init__(self, conn_id: int): self.conn_id = conn_id self.is_alive = True def ping(self) -> bool: return self.is_alive def close(self): self.is_alive = False class ConnectionPool: def __init__(self, max_size: int = 3): self.max_size = max_size self._pool = queue.Queue(maxsize=max_size) self._active_count = 0 self._seq = 0 self._lock = threading.Lock() def _create_connection(self) -> MockConnection | None: with self._lock: if self._active_count < self.max_size: self._active_count += 1 self._seq += 1 return MockConnection(self._seq) return None def acquire(self, timeout_sec: float = 1.0) -> MockConnection: deadline = time.time() + timeout_sec while time.time() < deadline: try: conn = self._pool.get_nowait() if conn.ping(): return conn else: # Connection died while idle; close and replace conn.close() with self._lock: self._active_count -= 1 except queue.Empty: new_conn = self._create_connection() if new_conn: return new_conn time.sleep(0.01) raise TimeoutError("Connection pool exhausted: could not acquire within timeout") def release(self, conn: MockConnection): if conn.is_alive: self._pool.put(conn) else: with self._lock: self._active_count -= 1 # Verification pool = ConnectionPool(max_size=2) # 1. Acquire up to limit c1 = pool.acquire() c2 = pool.acquire() assert c1.conn_id == 1 assert c2.conn_id == 2 # 2. Reusing returned connection pool.release(c1) c1_again = pool.acquire() assert c1_again.conn_id == 1 # 3. Handling dropped connection c1_again.is_alive = False # Simulate remote database idle timeout pool.release(c1_again) # Next acquire detects dead connection and provisions a fresh one c3 = pool.acquire() assert c3.conn_id == 3 assert c3.is_alive is True ``` </details>