|
|
|
|
|
|
| """
|
| 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
|
| )
|
|
|
|
|
| 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)
|
|
|
| print("✅ HTTP Connection Pools initialized (frontend-offloaded):")
|
| print(f" GPT: {GPT_POOL_SIZE} connections (primary)")
|
| print(f" Board Processor: {BOARD_PROCESSOR_POOL_SIZE} connections (primary)")
|
| print(f" Mistral: {MISTRAL_POOL_SIZE} connections (primary)")
|
| print(f" Transcript: {TRANSCRIPT_POOL_SIZE} connections (primary)")
|
| print(f" Cloudinary: {CLOUDINARY_POOL_SIZE} connections (primary)")
|
| print(f" Telegram API: {TELEGRAM_API_POOL_SIZE} connections (verification)")
|
| print(f" TTS: {TTS_POOL_SIZE} connections (fallback only)")
|
| print(f" Icons8: {ICONS8_POOL_SIZE} connections (config endpoint only)")
|
|
|