fix: split HTTP connect/read timeouts (Jellyfin build + qBit stats)
Every HTTP client passed an integer timeout to requests, applying the same value to BOTH connect and read phases. A slow Jellyfin /Items page or qBit /sync/maindata blew through the 10s read budget → ReadTimeoutError. Split into a (connect=5s, read=60s default) tuple via shared http_timeout() helper. The media index build worker uses a 180s read floor. Existing services with low timeout_seconds benefit from bumping to 60+.
This commit is contained in:
@@ -13,13 +13,15 @@ from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
from media_library_viewer_api.clients.http_timeout import DEFAULT_READ_TIMEOUT, http_timeout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AuthentikClient:
|
||||
"""Small wrapper around the Authentik core directory API."""
|
||||
|
||||
def __init__(self, base_url: str, api_token: str, timeout: float = 10.0):
|
||||
def __init__(self, base_url: str, api_token: str, timeout: float = DEFAULT_READ_TIMEOUT):
|
||||
if not base_url:
|
||||
raise ValueError("Authentik base_url is required")
|
||||
if not api_token:
|
||||
@@ -29,7 +31,8 @@ class AuthentikClient:
|
||||
if self.base_url.endswith("/api/v3"):
|
||||
self.base_url = self.base_url[:-7]
|
||||
self.api_token = api_token
|
||||
self.timeout = timeout
|
||||
# timeout is the per-response READ timeout (seconds); connect timeout is fixed at 5s.
|
||||
self.timeout = http_timeout(timeout)
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
"""Shared HTTP timeout helpers.
|
||||
|
||||
``requests`` accepts a single integer timeout and applies it to BOTH the
|
||||
connect and read phases. For slow upstream services (large Jellyfin
|
||||
libraries, qBittorrent with many torrents), the read phase needs a much
|
||||
larger budget than connect. These helpers produce ``(connect, read)`` tuples
|
||||
so the two phases are decoupled.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
#: Short connect timeout — fail fast on unreachable/dead hosts.
|
||||
DEFAULT_CONNECT_TIMEOUT = 5.0
|
||||
|
||||
#: Generous read timeout — let slow responses complete.
|
||||
DEFAULT_READ_TIMEOUT = 60.0
|
||||
|
||||
|
||||
def http_timeout(
|
||||
read_timeout: float | int | None = None,
|
||||
connect_timeout: float = DEFAULT_CONNECT_TIMEOUT,
|
||||
) -> tuple[float, float]:
|
||||
"""Build a ``(connect, read)`` timeout tuple for ``requests``.
|
||||
|
||||
``read_timeout`` is the per-response read budget (seconds). When omitted
|
||||
or non-positive, :data:`DEFAULT_READ_TIMEOUT` applies.
|
||||
"""
|
||||
effective_read = DEFAULT_READ_TIMEOUT
|
||||
if read_timeout is not None:
|
||||
try:
|
||||
parsed = float(read_timeout)
|
||||
if parsed > 0:
|
||||
effective_read = parsed
|
||||
except (TypeError, ValueError):
|
||||
pass # fall back to default on non-numeric input
|
||||
return (connect_timeout, effective_read)
|
||||
@@ -12,6 +12,8 @@ from typing import Any, cast
|
||||
|
||||
import requests
|
||||
|
||||
from media_library_viewer_api.clients.http_timeout import DEFAULT_READ_TIMEOUT, http_timeout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -35,7 +37,7 @@ DEFAULT_FIELDS = ",".join(
|
||||
class JellyfinClient:
|
||||
"""Small wrapper around the Jellyfin/Emby-compatible HTTP API."""
|
||||
|
||||
def __init__(self, base_url: str, api_key: str, timeout: int = 30):
|
||||
def __init__(self, base_url: str, api_key: str, timeout: float = DEFAULT_READ_TIMEOUT):
|
||||
if not base_url:
|
||||
raise ValueError("Jellyfin URL is required")
|
||||
if not api_key:
|
||||
@@ -47,7 +49,8 @@ class JellyfinClient:
|
||||
if self.base_url.endswith("/web"):
|
||||
self.base_url = self.base_url[:-4]
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
# timeout is the per-response READ timeout (seconds); connect timeout is fixed at 5s.
|
||||
self.timeout = http_timeout(timeout)
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update(
|
||||
{
|
||||
|
||||
@@ -11,13 +11,15 @@ from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
from media_library_viewer_api.clients.http_timeout import DEFAULT_READ_TIMEOUT, http_timeout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class JellyseerrClient:
|
||||
"""Small wrapper around the Jellyseerr REST API."""
|
||||
|
||||
def __init__(self, base_url: str, api_key: str, timeout: int = 30):
|
||||
def __init__(self, base_url: str, api_key: str, timeout: float = DEFAULT_READ_TIMEOUT):
|
||||
if not base_url:
|
||||
raise ValueError("Jellyseerr URL is required")
|
||||
if not api_key:
|
||||
@@ -27,7 +29,8 @@ class JellyseerrClient:
|
||||
if self.base_url.endswith("/api/v1"):
|
||||
self.base_url = self.base_url[:-7]
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
# timeout is the per-response READ timeout (seconds); connect timeout is fixed at 5s.
|
||||
self.timeout = http_timeout(timeout)
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update(
|
||||
{
|
||||
|
||||
@@ -12,6 +12,8 @@ from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
from media_library_viewer_api.clients.http_timeout import DEFAULT_READ_TIMEOUT, http_timeout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -23,7 +25,7 @@ class QbittorrentClient:
|
||||
:class:`requests.Session` that carries the login cookie.
|
||||
"""
|
||||
|
||||
def __init__(self, base_url: str, username: str, password: str, timeout: int = 10) -> None:
|
||||
def __init__(self, base_url: str, username: str, password: str, timeout: float = DEFAULT_READ_TIMEOUT) -> None:
|
||||
if not base_url:
|
||||
raise ValueError("qBittorrent base_url is required")
|
||||
if not username:
|
||||
@@ -34,7 +36,8 @@ class QbittorrentClient:
|
||||
self.base_url += "/api/v2"
|
||||
self._username = username
|
||||
self._password = password
|
||||
self.timeout = timeout
|
||||
# timeout is the per-response READ timeout (seconds); connect timeout is fixed at 5s.
|
||||
self.timeout = http_timeout(timeout)
|
||||
self._session = requests.Session()
|
||||
self._logged_in = False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user