Files
meshcore-bot/modules/http_retry.py
T
agessaman f279f2be0e refactor: one make_retry_session for the weather HTTP clients
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).
2026-10-01 01:38:51 -07:00

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