File size: 2,270 Bytes
4efd266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
# http_pool.py
# COMPLETE FILE

"""

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


# PRIMARY pools
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)

# FALLBACK-ONLY pools
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)")