""" HTTP Connection Pool Manager. Optimized for 150 concurrent users. Reuses TCP connections to external APIs. """ 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 ) def _create_session(pool_size, pool_block=True, retries=2): """ Create a requests.Session with connection pooling. pool_block=True prevents overwhelming external APIs. """ session = requests.Session() retry_strategy = Retry( total=retries, backoff_factor=0.5, status_forcelist=[502, 503, 504], allowed_methods=["POST", "GET"], ) 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 # ─── Pre-built sessions for each external service ─── gpt_session = _create_session(GPT_POOL_SIZE) tts_session = _create_session(TTS_POOL_SIZE) mistral_session = _create_session(MISTRAL_POOL_SIZE) transcript_session = _create_session(TRANSCRIPT_POOL_SIZE) cloudinary_session = _create_session(CLOUDINARY_POOL_SIZE) icons8_session = _create_session(10) board_processor_session = _create_session(BOARD_PROCESSOR_POOL_SIZE) print("✅ HTTP Connection Pools initialized:") print(f" GPT: {GPT_POOL_SIZE} connections") print(f" TTS: {TTS_POOL_SIZE} connections") print(f" Mistral: {MISTRAL_POOL_SIZE} connections") print(f" Transcript: {TRANSCRIPT_POOL_SIZE} connections") print(f" Cloudinary: {CLOUDINARY_POOL_SIZE} connections") print(f" Board Processor: {BOARD_PROCESSOR_POOL_SIZE} connections")