Stop spamming upstream: drop video download from probe, kill re-rolls

- CDN probe no longer downloads 64KB of video (or even a variant
  playlist): one small master-playlist fetch proves reachability.
  State stays OK/DEGRADED, kbps is no longer measured, and the pill/
  panel only show a rate when one exists.
- hianime: 5x getSourcesNew re-roll -> 1 attempt. The enc payload is
  deterministic per data-id (observed identical across calls), so
  re-rolling only hammered the provider.
- anikoto: 2x re-roll -> 1 attempt per server, same reason.

Kept: idle-gated + 30-min degraded backoff from the previous commit,
the single _get retry (protects real user playback from CF blips),
and the origin page check (the 'down' signal). Net upstream traffic is
now what an actual viewer would generate, nothing more.
This commit is contained in:
jozamudi committed 2026-09-29 17:33:54 +00:00
1 parent 1154203569
commit e3ef51d141
4 files changed
+43 -53

No files matched your search

+11 -23
View File
@@ -61,23 +61,19 @@ _DEGRADED_RETRY = 1800
# origin (primary provider pages/API) health — separate host, separate failure
# mode (observed 2026-09-03: CDN fine while origin 503'd "Under Maintenance")
_ORIGIN = {"state": "unknown", "msg": "", "ts": 0.0, "hist": []}
# ponytail: fixed threshold; if the CDN's normal throughput drifts below it
# the pill sits on "slow" — tune via ANIWEB_CDN_KBPS, not a percentile.
# kept for the /api/cdn response shape (the panel shows it only when a rate
# was actually measured; the probe now checks reachability, not throughput)
_CDN_THRESHOLD = float(os.environ.get("ANIWEB_CDN_KBPS", "40"))
def _cdn_probe():
"""Background probe: pull 64KB from the CDN and log state flips, so
server.log shows when a degraded window ends (CDN RECOVERED). Skipped
"""Background probe: fetch ONE small playlist and log state flips, so
server.log shows when a degraded window ends (CDN RECOVERED). No video
is downloaded just to light a status dot. Skipped
while the app is idle and backed off while DEGRADED (see _LAST_PLAY).
Probe target: ep_id 1 = One Piece E1 on the primary provider.
ponytail: single fixed episode; if it ever disappears, the probe logs
DEGRADED forever — swap the ID, don't build a picker."""
def first(pl, base):
l = next(x.strip() for x in pl.splitlines()
if x.strip() and not x.startswith("#"))
return l if l.startswith("http") else base + l
state = None
while True:
if time.time() - _LAST_PLAY > _IDLE_SECS:
@@ -89,23 +85,15 @@ def _cdn_probe():
raise RuntimeError("no master m3u8")
# hianime's CDN 403s without the embed-origin Referer (anidb needs none)
hdrs = upstream.stream_meta(m) or {}
base = m.rsplit("/", 1)[0] + "/"
v = first(upstream._get(m, timeout=10, headers=hdrs).text, base)
# hianime's variant playlists use URIs relative to THEIR OWN path
vbase = v.rsplit("/", 1)[0] + "/"
seg = first(upstream._get(v, timeout=10, headers=hdrs).text, vbase)
t = time.time()
r = upstream._session().get(seg, headers={**hdrs, "Range": "bytes=0-65535"},
impersonate="chrome", timeout=10)
kbps = len(r.content) / 1024 / max(time.time() - t, 0.01)
upstream._get(m, timeout=10, headers=hdrs)
now = "OK"
except Exception:
kbps = 0.0
now = "OK" if kbps >= _CDN_THRESHOLD else "DEGRADED"
now = "DEGRADED"
if now != state:
print(f"[probe] CDN {now} ({kbps:.0f} KB/s)", flush=True)
print(f"[probe] CDN {now}", flush=True)
state = now
_CDN.update(state=now, kbps=round(kbps), ts=time.time())
_CDN["hist"].append({"t": time.time(), "kbps": round(kbps)})
_CDN.update(state=now, kbps=0, ts=time.time())
_CDN["hist"].append({"t": time.time(), "kbps": 0})
del _CDN["hist"][:-20]
_origin_probe()
time.sleep(_DEGRADED_RETRY if now == "DEGRADED" else 180)