17 - Connection Pooling
Overview
Architecture Diagram
┌──────────────────────────────────────────────────────────────────────────────┐
│ Connection Pooling Architecture │
└──────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ ConnectionPool (per broker/user) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Configuration: │ │
│ │ MAX_SYMBOLS_PER_WEBSOCKET = 1000 (default) │ │
│ │ MAX_WEBSOCKET_CONNECTIONS = 3 (default) │ │
│ │ Total capacity: 3000 symbols per user │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ │ Adapter 0 │ │ Adapter 1 │ │ Adapter 2 │ │
│ │ 1000 symbols │ │ 1000 symbols │ │ 1000 symbols │ │
│ │ │ │ │ │ │ │
│ │ SBIN, INFY, │ │ TCS, WIPRO, │ │ NIFTY opts, │ │
│ │ RELIANCE... │ │ HDFC... │ │ BANKNIFTY... │ │
│ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │
│ │ │ │ │
│ └──────────────────┼──────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ SharedZmqPublisher (Singleton) │ │
│ │ Binds to ZMQ_PORT (default: 5555) │ │
│ │ Thread-safe publish with _publish_lock │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
│ ZeroMQ PUB/SUB
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ WebSocketProxy (server.py) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ ZeroMQ SUB socket connects to ZMQ_PORT │ │
│ │ Routes data to WebSocket clients (port 8765) │ │
│ │ O(1) subscription lookup via subscription_index │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘Configuration
Core Components
1. SharedZmqPublisher (Singleton)
2. ConnectionPool
Key Methods
3. Thread-Local Context for Pooled Adapters
Connection Balancing Flow
Pool Statistics
WebSocketProxy Integration
Benefits
Performance
Aspect
Without Pooling
With Pooling
Reliability
Logging
Key Files Reference
File
Purpose
Last updated