File size: 708 Bytes
57272d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Shared HTTP session with retry + exponential backoff.

All concrete sources share this session so 429/5xx retries, timeouts,
and TLS reuse are uniform. The private `_http` name is also exposed
from the sources package for tests that introspect retry config.
"""

import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry


_retry_strategy = Retry(
    total=3,
    backoff_factor=0.5,  # 0.5s, 1s, 2s
    status_forcelist=[429, 500, 502, 503, 504],
    allowed_methods=["GET", "POST"],
    raise_on_status=False,
)

_http = requests.Session()
_http.mount("https://", HTTPAdapter(max_retries=_retry_strategy))
_http.mount("http://", HTTPAdapter(max_retries=_retry_strategy))