mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-11-04 03:52:34 +00:00
wx, rain and the weather service each built the same requests session (two retries on connection errors and 5xx for GET, 0.3 s backoff, status codes not raised, 10x20 connection pool). It now lives in modules/http_retry.py; each class keeps its _create_retry_session as a one-line delegate, and each keeps its own session lifetime (rain still builds one per call).
29 lines
953 B
Python
29 lines
953 B
Python
"""Shared ``requests`` session with retry/backoff for the weather and forecast APIs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import requests
|
|
from requests.adapters import HTTPAdapter
|
|
from urllib3.util.retry import Retry
|
|
|
|
|
|
def make_retry_session() -> requests.Session:
|
|
"""A session that retries GETs on connection errors and 5xx responses.
|
|
|
|
Two retries (three attempts) with 0.3 s / 0.6 s backoff. Status codes are
|
|
not raised; callers inspect the response. Connections are pooled
|
|
(10 pools, up to 20 connections each).
|
|
"""
|
|
session = requests.Session()
|
|
retry_strategy = Retry(
|
|
total=2,
|
|
backoff_factor=0.3,
|
|
status_forcelist=[500, 502, 503, 504],
|
|
allowed_methods=["GET"],
|
|
raise_on_status=False,
|
|
)
|
|
adapter = HTTPAdapter(max_retries=retry_strategy, pool_connections=10, pool_maxsize=20)
|
|
session.mount("https://", adapter)
|
|
session.mount("http://", adapter)
|
|
return session
|