Files
DinQuant/backend_api_python/app/utils/http.py
T

42 lines
969 B
Python
Raw Normal View History

2025-12-29 03:06:49 +08:00
"""
HTTP tool module
2025-12-29 03:06:49 +08:00
"""
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
def get_retry_session(
retries: int = 3,
backoff_factor: float = 0.5,
status_forcelist: tuple = (500, 502, 503, 504)
) -> requests.Session:
"""
Get HTTP Session with retry mechanism
2025-12-29 03:06:49 +08:00
Args:
retries: number of retries
backoff_factor: retry interval factor
status_forcelist: HTTP status codes that need to be retried
2025-12-29 03:06:49 +08:00
Returns:
Configured Session instance
2025-12-29 03:06:49 +08:00
"""
session = requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
# Global shared session
2025-12-29 03:06:49 +08:00
global_session = get_retry_session()