File size: 5,876 Bytes
22087d0 | 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | """
Shared HTTP Client Service for Third-Party API Calls
This module provides a centralized, optimized HTTP client for making
requests to third-party services from containers. It addresses common
bottlenecks:
1. Connection pooling - reuses connections instead of creating new ones
2. Proper timeout configuration - prevents hanging requests
3. DNS caching - reduces DNS lookup latency
4. Connection limits - prevents resource exhaustion
5. Retry logic - handles transient network failures
Usage:
from core.services.http_client import get_http_client
async with get_http_client() as client:
response = await client.get("https://api.example.com/data")
"""
import os
import httpx
from typing import Optional
from contextlib import asynccontextmanager
from core.utils.logger import logger
# Connection pool settings
HTTP_MAX_CONNECTIONS = int(os.getenv('HTTP_MAX_CONNECTIONS', '100'))
HTTP_MAX_KEEPALIVE = int(os.getenv('HTTP_MAX_KEEPALIVE', '50'))
HTTP_KEEPALIVE_EXPIRY = 30.0
# Timeout settings (seconds)
HTTP_CONNECT_TIMEOUT = float(os.getenv('HTTP_CONNECT_TIMEOUT', '10.0'))
HTTP_READ_TIMEOUT = float(os.getenv('HTTP_READ_TIMEOUT', '60.0'))
HTTP_WRITE_TIMEOUT = float(os.getenv('HTTP_WRITE_TIMEOUT', '30.0'))
HTTP_POOL_TIMEOUT = float(os.getenv('HTTP_POOL_TIMEOUT', '30.0'))
# Retry settings
HTTP_RETRIES = int(os.getenv('HTTP_RETRIES', '3'))
# Global shared client instance (per worker process)
_shared_client: Optional[httpx.AsyncClient] = None
_client_lock = None
def _create_transport() -> httpx.AsyncHTTPTransport:
"""Create HTTP transport with optimized connection pooling."""
return httpx.AsyncHTTPTransport(
http2=True, # Enable HTTP/2 for better connection multiplexing
limits=httpx.Limits(
max_connections=HTTP_MAX_CONNECTIONS,
max_keepalive_connections=HTTP_MAX_KEEPALIVE,
keepalive_expiry=HTTP_KEEPALIVE_EXPIRY,
),
retries=HTTP_RETRIES,
)
def _create_timeout() -> httpx.Timeout:
"""Create timeout configuration."""
return httpx.Timeout(
connect=HTTP_CONNECT_TIMEOUT,
read=HTTP_READ_TIMEOUT,
write=HTTP_WRITE_TIMEOUT,
pool=HTTP_POOL_TIMEOUT,
)
async def _get_shared_client() -> httpx.AsyncClient:
"""Get or create the shared HTTP client instance."""
global _shared_client, _client_lock
if _shared_client is not None:
return _shared_client
# Lazy import to avoid circular dependencies
import asyncio
if _client_lock is None:
_client_lock = asyncio.Lock()
async with _client_lock:
# Double-check after acquiring lock
if _shared_client is not None:
return _shared_client
logger.info(
f"Initializing shared HTTP client: "
f"max_connections={HTTP_MAX_CONNECTIONS}, "
f"connect_timeout={HTTP_CONNECT_TIMEOUT}s, "
f"read_timeout={HTTP_READ_TIMEOUT}s"
)
_shared_client = httpx.AsyncClient(
transport=_create_transport(),
timeout=_create_timeout(),
# Enable DNS caching at the OS level (if supported)
# This helps reduce DNS lookup latency in containers
follow_redirects=True,
)
return _shared_client
async def close_shared_client():
"""Close the shared HTTP client (called during shutdown)."""
global _shared_client
if _shared_client is not None:
try:
await _shared_client.aclose()
logger.debug("Shared HTTP client closed")
except Exception as e:
logger.error(f"Error closing shared HTTP client: {e}")
finally:
_shared_client = None
@asynccontextmanager
async def get_http_client(
timeout: Optional[httpx.Timeout] = None,
**kwargs
) -> httpx.AsyncClient:
"""
Get a shared HTTP client with optimized connection pooling.
Args:
timeout: Optional custom timeout (overrides default)
**kwargs: Additional httpx.AsyncClient parameters
Yields:
httpx.AsyncClient: Configured HTTP client
Example:
async with get_http_client() as client:
response = await client.get("https://api.example.com/data")
"""
client = await _get_shared_client()
# If custom timeout or other params provided, create a new client
# Otherwise, reuse the shared client
if timeout is not None or kwargs:
# Create a temporary client with custom settings
# Still uses connection pooling via transport
custom_client = httpx.AsyncClient(
transport=_create_transport(),
timeout=timeout or _create_timeout(),
**kwargs
)
try:
yield custom_client
finally:
await custom_client.aclose()
else:
# Reuse shared client (no cleanup needed)
yield client
@asynccontextmanager
async def get_ephemeral_client(
timeout: Optional[float] = None,
**kwargs
) -> httpx.AsyncClient:
"""
Get an ephemeral HTTP client for one-off requests.
Use this when you need a client that will be closed after use,
or when you need different settings than the shared client.
Args:
timeout: Optional timeout in seconds (creates httpx.Timeout)
**kwargs: Additional httpx.AsyncClient parameters
Yields:
httpx.AsyncClient: Ephemeral HTTP client
"""
if timeout is not None and not isinstance(timeout, httpx.Timeout):
timeout = httpx.Timeout(timeout)
elif timeout is None:
timeout = _create_timeout()
client = httpx.AsyncClient(
transport=_create_transport(),
timeout=timeout,
**kwargs
)
try:
yield client
finally:
await client.aclose()
|