ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/networker/tasks/task-5-1-2-connection-pool
TASK

Implementation

Extend your TCP server to accept up to N concurrent connections. When N is exceeded, queue new connections in a backlog. If the backlog is also full, reject the connection.

Implement handlers:

Request:  {"type": "pool_config", "msg_id": 1, "max_connections": 3, "backlog_size": 5}
Response: {"type": "pool_config_ok", "in_reply_to": 1}

Request:  {"type": "pool_connect", "msg_id": 2, "client_id": "c1"}
Response: {"type": "pool_connect_ok", "in_reply_to": 2, "status": "connected", "active": 1, "queued": 0}

Request:  {"type": "pool_connect", "msg_id": 5, "client_id": "c4"}
Response: {"type": "pool_connect_ok", "in_reply_to": 5, "status": "queued", "active": 3, "queued": 1}

Request:  {"type": "pool_disconnect", "msg_id": 6, "client_id": "c1"}
Response: {"type": "pool_disconnect_ok", "in_reply_to": 6, "promoted": "c4", "active": 3, "queued": 0}

Sample Test Cases

Configure pool and connectTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"pool_config","msg_id":2,"max_connections":2,"backlog_size":3}}
{"src":"c1","dest":"n1","body":{"type":"pool_connect","msg_id":3,"client_id":"c1"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "pool_config_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "pool_connect_ok", "in_reply_to": 3, "status": "connected", "active": 1, "queued": 0, "msg_id": 2}}
Exceeding max connections queuesTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"pool_config","msg_id":2,"max_connections":2,"backlog_size":3}}
{"src":"c1","dest":"n1","body":{"type":"pool_connect","msg_id":3,"client_id":"c1"}}
{"src":"c1","dest":"n1","body":{"type":"pool_connect","msg_id":4,"client_id":"c2"}}
{"src":"c1","dest":"n1","body":{"type":"pool_connect","msg_id":5,"client_id":"c3"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Track the number of active connections with a counter
Hint 2
When max connections is reached, either queue or reject new connections
Hint 3
Use a configurable backlog size for queued connections
Hint 4
Decrement the counter when a connection is closed
Hint 5
Consider what happens when the queue itself is full
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

connection poolbacklogconcurrencyresource management
main.py
python
Add a Connection Pool with Configurable Backlog - The Networker | Build Distributed Systems