Resources card: CPU, Memory and Network on top, More/Less for Swap, Disk and a bandwidth test

The test downloads random bytes from the VPS for 5 s and shows the fastest half second. It's refused
while players are connected, since it saturates the uplink.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
neil 2026-10-26 20:22:28 -07:00
parent a963dc1bf4
commit 169a6154ab
No known key found for this signature in database
4 changed files with 112 additions and 4 deletions

View file

@ -419,6 +419,38 @@ def get_stats():
}
# ---------- bandwidth test ----------
# The browser downloads random bytes for BANDWIDTH_TEST_S and measures the rate, so the result is the
# VPS -> browser path (the VPS's upload, what players receive), capped by the viewer's own connection.
# One test at a time, and none while anyone is playing: a test saturates the uplink and would lag them.
BANDWIDTH_TEST_S = 5
_bw_chunk = os.urandom(1024 * 1024) # random, so a compressing proxy can't shrink it
_bw_lock = threading.Lock()
@app.get("/api/bandwidth-test", dependencies=[Depends(require_auth)])
def bandwidth_test():
with stats_lock:
online = sum(len(s.get("players") or []) for s in server_stats.values())
if online:
raise HTTPException(409, f"{online} player{'s are' if online != 1 else ' is'} connected. The test would "
"use all of the VPS's bandwidth and lag them, so it's disabled until everyone has left.")
if not _bw_lock.acquire(blocking=False):
raise HTTPException(409, "A bandwidth test is already running.")
def stream():
try:
end = time.monotonic() + BANDWIDTH_TEST_S + 1 # the browser stops at BANDWIDTH_TEST_S
while time.monotonic() < end:
yield _bw_chunk
finally:
_bw_lock.release()
return StreamingResponse(stream(), media_type="application/octet-stream",
headers={"Cache-Control": "no-store", "Content-Encoding": "identity"})
# ---------- servers ----------
def _server_info(c):