fix(playback): retry and downgrade quality before reporting 无法播放

Two failure modes look identical to the user — 「无法播放(区域/版权限制)」 — but
need different handling, and both were hitting the same code path:

1. **Transient.** NetEase intermittently answers `url: null` for a song that
   resolves fine a moment later. Repeating the SAME request fixes it, but the
   user had to click play repeatedly to be that retry.

2. **Quality gap.** Measured against a real library: a track can be playable at
   192k ("higher") while BOTH 320k ("exhigh") and 128k ("standard") return
   `url: null`. So the ladder is not continuous and "just drop to the cheapest"
   would still fail — every level below the current one has to be tried.

`resolveAndPlay` now resolves through `bot/url-retry.ts`:

- Phase 1: `1 + config.playUrlRetries` attempts (default 2, clamped 0..5) at the
  configured quality, 250ms linear backoff between them.
- Phase 2: one attempt per lower level from `provider.getFallbackQualities()`
  (NetEase: exhigh → higher → standard), 120ms apart — switching level is a
  different request, not a re-poke of the same one.
- Cancel (stop / disconnect) is checked before every attempt, so the budget
  never retries into a dead connection.
- A thrown error is retried exactly like an empty result: a sidecar timeout
  looks identical to the user.
- Failure is logged once per track, not once per attempt.

Every queue path funnels into `resolveAndPlay` (web click, chat !play,
auto-advance, FM), so playlists and albums get the same budget for free — that
is where "click the playlist four times before it starts" came from.

`config.playUrlRetries` is exposed on GET/POST `/api/bot/settings`. A config
without the field falls back to 2 — retries are ON by default because the whole
point is to be invisible. Set it to 0 to restore the previous fail-fast path.
This commit is contained in:
jiayuLian committed 2026-11-08 17:30:02 +08:00
1 parent 17008ae52e
commit c2771ab9a7
8 files changed
+566 -5

No files matched your search

+32 -5
View File
@@ -7,8 +7,9 @@ import {
type TS3VoiceSendFailure,
} from "../ts-protocol/client.js";
import { AudioPlayer } from "../audio/player.js";
import { resolveUrlWithRetry } from "./url-retry.js";
import { PlayQueue, PlayMode, type QueuedSong } from "../audio/queue.js";
import type { MusicProvider, Platform, Song } from "../music/provider.js";
import type { MusicProvider, Platform, Song, SongUrlResult } from "../music/provider.js";
import {
parseCommand,
canRunCommand,
@@ -1022,8 +1023,7 @@ export class BotInstance extends EventEmitter {
}
/** Resolve URL for a song and start playing it. Skips to next if URL fails. */
async resolveAndPlay(song: QueuedSong): Promise<boolean> {
if (!this.connected) {
async resolveAndPlay(song: QueuedSong): Promise<boolean> { if (!this.connected) {
this.logger.warn({ songId: song.id, name: song.name }, "resolveAndPlay called on disconnected bot — skipping");
return false;
}
@@ -1045,9 +1045,36 @@ export class BotInstance extends EventEmitter {
song.id = detail.id;
}
}
const result = await provider.getSongUrl(song.id);
// Retries live in resolveUrlWithRetry so the whole queue path (web click,
// chat !play, auto-advance, FM) benefits from the same budget, and the
// failure is logged once there instead of once per attempt.
// Retries happen here (not in the provider) so every queue path — web
// click, chat !play, auto-advance, FM — shares the same budget. The
// policy lives in url-retry.ts so it is unit tested without a bot; a
// config without the field means 0 retries, i.e. the previous behaviour.
const result = await resolveUrlWithRetry({
retries: this.config?.playUrlRetries,
resolve: (quality) => provider.getSongUrl(song.id, quality),
// NetEase's quality ladder is not continuous: a track can play at 192k
// while both 320k and 128k return null, so every lower level is tried.
// Providers without a quality knob return [] and keep one attempt.
fallbacks: provider.getFallbackQualities?.() ?? [],
// Stop / disconnect must abort the budget: retrying into a dead
// connection only delays the queue.
isCancelled: () => !this.connected,
onAttemptFailed: ({ attempt, total, quality, error }) => {
const fields = { songId: song.id, name: song.name, attempt, total, quality };
if (error) {
this.logger.warn({ ...fields, err: error }, "URL resolve threw — retrying");
} else if (attempt < total) {
this.logger.debug(fields, "URL resolve returned no URL — retrying");
} else {
// Once per track, after the budget is spent — not once per attempt.
this.logger.warn(fields, "No URL available after retries, skipping");
}
},
});
if (!result?.url) {
this.logger.warn({ songId: song.id, name: song.name }, "No URL available, skipping");
return false;
}
// Re-check connection state AFTER the network round-trip — the URL