From f3df4cd56f84d9fc71bcf4c2cd2a0f816bca54bb Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Sat, 14 Nov 2026 12:50:47 +0000 Subject: [PATCH] fix(music): a busy server keeps its last catalogue, so one slow reply no longer empties nixamp.com/music (0.80.7) The hub reads each server's /api/v1/label and guide with a 6 s timeout and dropped a server that missed it. server2 (the only AI label) at load 58 after the 0.80.6 rollout missed it, and /music showed nothing. Each server now keeps its last good answers for up to 30 minutes. Co-Authored-By: Claude Opus 5.5 --- package.json | 2 +- src/music-hub.ts | 15 ++++++++++++++- test/music-hub.test.ts | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ce95730..7273781 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nixamp", - "version": "0.80.6", + "version": "0.80.7", "description": "It really whips the terminal's ass. A Winamp-shaped audio player for your terminal.", "license": "MIT", "type": "module", diff --git a/src/music-hub.ts b/src/music-hub.ts index ae0ff3b..ac3f160 100644 --- a/src/music-hub.ts +++ b/src/music-hub.ts @@ -73,9 +73,14 @@ export function parseMoved(text: string | undefined): Record { /** NIXAMP_MUSIC_MIRRORS: " > " pairs, comma separated (same shape as NIXAMP_MUSIC_MOVED). */ export const parseMirrors = parseMoved; +/** How long a server's last catalogue stands in for it while it does not answer. */ +export const STALE_MS = 30 * 60_000; + export class MusicHub { private cache: { at: number; value: Promise } | null = null; private bases = new Map(); + /** Each server's last good answers: one slow reply (a busy box) must not empty /music. */ + private lastGood = new Map(); constructor(private readonly options: MusicHubOptions) {} @@ -162,10 +167,18 @@ export class MusicHub { await Promise.all(peers.map(async (base) => { const id = serverId(base); this.bases.set(id, base); - const [label, guide] = await Promise.all([ + const [freshLabel, freshGuide] = await Promise.all([ this.read(`${base}/api/v1/label`), this.read<{ stations?: GuideStation[] }>(`${base}/api/v1/stations/guide?local=1`), ]); + // A server that timed out (6 s) keeps what it said last, for up to STALE_MS. + const kept = this.lastGood.get(base) ?? {}; + const now = this.now(); + if (freshLabel) kept.label = { at: now, value: freshLabel }; + if (freshGuide) kept.guide = { at: now, value: freshGuide }; + this.lastGood.set(base, kept); + const label = freshLabel ?? (kept.label && now - kept.label.at < STALE_MS ? kept.label.value : null); + const guide = freshGuide ?? (kept.guide && now - kept.guide.at < STALE_MS ? kept.guide.value : null); // A station is heard through its own server; the page's token is its listen key. const byId = new Map(); for (const one of guide?.stations ?? []) { diff --git a/test/music-hub.test.ts b/test/music-hub.test.ts index dd0832c..eeb9112 100644 --- a/test/music-hub.test.ts +++ b/test/music-hub.test.ts @@ -1,6 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { artistFollowId, handleMusicRoutes, isMusicPath, MusicHub, musicPageFor, parseMirrors, parseMoved, serverId } from "../src/music-hub.ts"; +import { artistFollowId, handleMusicRoutes, isMusicPath, MusicHub, musicPageFor, parseMirrors, parseMoved, serverId, STALE_MS } from "../src/music-hub.ts"; import { buyLink } from "../src/label-routes.ts"; const S1 = "https://server1.example.com:4321"; @@ -148,3 +148,18 @@ test("files come from the server's mirror (a c0mpute site) first, by the label's globalThis.fetch = real; } }); + +test("a server that stops answering keeps its last catalogue for a while, so /music is not emptied by one slow reply", async () => { + let now = 0; + let down = false; + const ok = fetcher([]); + const hub = new MusicHub({ peers: () => [S1], now: () => now, fetcher: (async (url: string) => down ? new Response("busy", { status: 503 }) : ok(url)) as typeof fetch }); + assert.equal((await hub.catalogue()).genres.length, 1); + down = true; + now += 61_000; + const stale = await hub.catalogue(); + assert.deepEqual(stale.genres.map((one) => one.genre), ["Punk"], "kept while the server is slow"); + assert.equal(stale.stations[0]?.id, "punkstatio"); + now += STALE_MS; + assert.equal((await hub.catalogue()).genres.length, 0, "and dropped once it has been gone too long"); +});