| """ |
| HTTP Connection Pool Manager. |
| Optimized for 150 concurrent users with frontend-offloaded architecture. |
| """ |
|
|
| import requests |
| from requests.adapters import HTTPAdapter |
| from urllib3.util.retry import Retry |
| from config import ( |
| GPT_POOL_SIZE, TTS_POOL_SIZE, MISTRAL_POOL_SIZE, |
| TRANSCRIPT_POOL_SIZE, CLOUDINARY_POOL_SIZE, BOARD_PROCESSOR_POOL_SIZE, |
| ICONS8_POOL_SIZE, TELEGRAM_API_POOL_SIZE, CARDS_API_POOL_SIZE |
| ) |
|
|
|
|
| def _create_session(pool_size, pool_block=True, retries=2): |
| """Create a requests.Session with connection pooling.""" |
| session = requests.Session() |
|
|
| retry_strategy = Retry( |
| total=retries, |
| backoff_factor=0.5, |
| status_forcelist=[502, 503, 504], |
| allowed_methods=["POST", "GET", "PUT"], |
| ) |
|
|
| adapter = HTTPAdapter( |
| pool_connections=pool_size, |
| pool_maxsize=pool_size, |
| pool_block=pool_block, |
| max_retries=retry_strategy, |
| ) |
|
|
| session.mount("https://", adapter) |
| session.mount("http://", adapter) |
|
|
| return session |
|
|
|
|
| |
| gpt_session = _create_session(GPT_POOL_SIZE) |
| board_processor_session = _create_session(BOARD_PROCESSOR_POOL_SIZE) |
| mistral_session = _create_session(MISTRAL_POOL_SIZE) |
| transcript_session = _create_session(TRANSCRIPT_POOL_SIZE) |
| cloudinary_session = _create_session(CLOUDINARY_POOL_SIZE) |
| telegram_api_session = _create_session(TELEGRAM_API_POOL_SIZE) |
|
|
| |
| tts_session = _create_session(TTS_POOL_SIZE) |
| icons8_session = _create_session(ICONS8_POOL_SIZE) |
|
|
| |
| cards_service_session = _create_session(CARDS_API_POOL_SIZE) |
|
|
| print("HTTP Connection Pools initialized:") |
| print(" GPT: {} connections".format(GPT_POOL_SIZE)) |
| print(" Board Processor: {} connections".format(BOARD_PROCESSOR_POOL_SIZE)) |
| print(" Mistral: {} connections".format(MISTRAL_POOL_SIZE)) |
| print(" Transcript: {} connections".format(TRANSCRIPT_POOL_SIZE)) |
| print(" Cloudinary: {} connections".format(CLOUDINARY_POOL_SIZE)) |
| print(" Telegram API: {} connections".format(TELEGRAM_API_POOL_SIZE)) |
| print(" TTS: {} connections (fallback)".format(TTS_POOL_SIZE)) |
| print(" Icons8: {} connections (config only)".format(ICONS8_POOL_SIZE)) |
| print(" Cards Service: {} connections (microservice)".format(CARDS_API_POOL_SIZE)) |