File size: 1,941 Bytes
c37398c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | """
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") |