perf(qbittorrent): rid incremental sync + shared cache + backoff (stop hanging qBittorrent)

The app was saturating qBittorrent's single-threaded web server and causing
its own Web UI (and the reverse proxy) to hang/504: each of the 3 qBittorrent
widgets fetched /sync/maindata independently, every call was a FULL snapshot
(no rid), and polling was aggressive (5s for speed). For large torrent lists
each snapshot is heavy, so the server queued and Traefik timed out.

QbittorrentClient.maindata now:
- Uses the incremental rid protocol: the first call is a full_update;
  subsequent calls send the last rid and get a small diff that is merged into
  a cached snapshot (full_update replaces; partial_update merges server_state,
  torrents {added/None-removed/..._removed}, categories, tags, trackers).
  Payloads shrink dramatically for large libraries.
- Serves a short-TTL (3s) cached snapshot under a lock, so concurrent widget
  polls collapse onto a single HTTP fetch instead of N.
- Backs off exponentially (capped 30s) on repeated failure, serving the last
  good snapshot when available, so a struggling qBittorrent isn't hammered
  further. Returns a shallow race-safe copy of the snapshot per call.

Also slow the speed widget poll from 5s -> 15s (backend widget-kind +
frontend registry) for ~3x fewer calls.

Tests: rid full+partial merge, cache collapses within-TTL calls, backoff
skips the network after failure and serves stale. 393/393 backend + 180/180
frontend tests pass; ruff + tsc + ESLint clean.
This commit is contained in:
Developer
2026-07-12 12:20:05 +00:00
parent 7e4222ef00
commit ba01ad7c0c
10 changed files with 230 additions and 23 deletions
+84
View File
@@ -117,6 +117,90 @@ class QbittorrentClientTests(unittest.TestCase):
self.assertEqual(result["server_state"]["dl_info_speed"], 12345)
self.assertEqual(len(result["torrents"]), 2)
def test_maindata_uses_rid_and_merges_partial_update(self) -> None:
"""First call is a full fetch (no rid); later calls send rid and merge the diff."""
self.client._logged_in = True
self.client._maindata_ttl = 0 # force a real fetch each call
full = {
"rid": 10,
"full_update": True,
"server_state": {"dl_info_speed": 100},
"torrents": {"a": {"name": "A", "state": "downloading"}},
}
partial = {
"rid": 11,
"full_update": False,
"server_state": {"dl_info_speed": 200},
"torrents": {"a": {"name": "A", "state": "pausedDL"}},
}
self.session.get.side_effect = [self._get_response(full), self._get_response(partial)]
r1 = self.client.maindata()
self.assertNotIn("rid", self.session.get.call_args_list[0].kwargs["params"])
self.assertEqual(r1["server_state"]["dl_info_speed"], 100)
self.assertEqual(r1["torrents"]["a"]["state"], "downloading")
r2 = self.client.maindata()
self.assertEqual(self.session.get.call_args_list[1].kwargs["params"].get("rid"), 10)
self.assertEqual(r2["server_state"]["dl_info_speed"], 200) # merged
self.assertEqual(r2["torrents"]["a"]["state"], "pausedDL") # merged
def test_maindata_caches_concurrent_calls_within_ttl(self) -> None:
"""Two calls within the TTL collapse to a single HTTP fetch."""
self.client._logged_in = True
payload = {
"rid": 1,
"full_update": True,
"server_state": {"dl_info_speed": 5},
"torrents": {"a": {"state": "downloading"}},
}
self.session.get.return_value = self._get_response(payload)
self.client.maindata()
r2 = self.client.maindata() # served from cache — no extra HTTP
self.assertEqual(self.session.get.call_count, 1)
self.assertEqual(r2["server_state"]["dl_info_speed"], 5)
def test_maindata_backoff_after_failure_does_not_pile_on(self) -> None:
"""A failed fetch arms backoff; the next call skips the network entirely."""
self.client._logged_in = True
self.client._maindata_ttl = 0
bad = MagicMock()
bad.status_code = 503
bad.raise_for_status.side_effect = requests.HTTPError("503 Server Error")
bad.text = ""
self.session.get.return_value = bad
with self.assertRaises(RuntimeError): # no snapshot yet -> raises + arms backoff
self.client.maindata()
self.assertEqual(self.session.get.call_count, 1)
with self.assertRaises(RuntimeError): # within backoff -> no new HTTP
self.client.maindata()
self.assertEqual(self.session.get.call_count, 1)
def test_maindata_serves_stale_snapshot_during_backoff(self) -> None:
"""After a good fetch, a later failure serves stale data instead of erroring."""
self.client._logged_in = True
self.client._maindata_ttl = 0
good = {
"rid": 1,
"full_update": True,
"server_state": {"dl_info_speed": 7},
"torrents": {"a": {"state": "downloading"}},
}
bad = MagicMock()
bad.status_code = 503
bad.raise_for_status.side_effect = requests.HTTPError("503")
bad.text = ""
self.session.get.side_effect = [self._get_response(good), bad]
r1 = self.client.maindata()
self.assertEqual(r1["server_state"]["dl_info_speed"], 7)
r2 = self.client.maindata() # fetch fails -> serves stale snapshot, no raise
self.assertEqual(r2["server_state"]["dl_info_speed"], 7)
@patch("media_library_viewer_api.clients.qbittorrent.requests.Session")
def test_login_http_error_propagates(self, mock_session_cls: MagicMock) -> None:
"""A network error during login propagates as requests exception."""