Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 14 additions & 1 deletion src/music-hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ export function parseMoved(text: string | undefined): Record<string, string> {
/** NIXAMP_MUSIC_MIRRORS: "<server origin> > <mirror base>" 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<HubCatalogue> } | null = null;
private bases = new Map<string, string>();
/** Each server's last good answers: one slow reply (a busy box) must not empty /music. */
private lastGood = new Map<string, { label?: { at: number; value: LabelView }; guide?: { at: number; value: { stations?: GuideStation[] } } }>();

constructor(private readonly options: MusicHubOptions) {}

Expand Down Expand Up @@ -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<LabelView>(`${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<string, HubStation>();
for (const one of guide?.stations ?? []) {
Expand Down
17 changes: 16 additions & 1 deletion test/music-hub.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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");
});
Loading