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

+27
View File
@@ -137,6 +137,19 @@ export interface BotConfig {
* keeps today's clear-and-play behavior.
*/
playKeepsQueue: boolean;
/**
* Extra attempts to re-resolve a playable URL before a track is reported as
* unplayable.
*
* NetEase intermittently answers `url: null` for a song that resolves fine a
* second later (upstream risk-control / edge jitter), which users experienced
* as "click once → 无法播放(区域/版权限制), click again → works". Retrying
* silently inside one request turns that into a normal play.
*
* 0 disables retries (fail fast, pre-existing behaviour). Clamped to 0..5 so
* a hand-edited config cannot turn every failed track into a long stall.
*/
playUrlRetries: number;
// Public base URL used when generating share links (e.g. the bot专属链接).
// Leave empty to use the browser's current origin. Example:
// "https://music.example.com" or "http://1.2.3.4:3000"
@@ -192,6 +205,9 @@ export function getDefaultConfig(): BotConfig {
localAudioEnabled: true,
savedQueuesEnabled: false,
playKeepsQueue: false,
// Two extra attempts: enough to ride out a transient NetEase hiccup without
// making a genuinely unlicensed track feel stuck (~1s of backoff total).
playUrlRetries: 2,
publicUrl: "",
trustProxy: false,
guestMode: {
@@ -395,6 +411,16 @@ export function loadConfig(path: string): BotConfig {
const savedQueuesEnabled = partial.savedQueuesEnabled === true;
const playKeepsQueue = partial.playKeepsQueue === true;
// Play-URL retry budget. Anything non-numeric (legacy config, `"3"`,
// `null`) falls back to the default instead of disabling the retry, because
// "no retries" is the surprising outcome here — the feature exists to hide
// upstream flakiness from users.
const rawRetries = partial.playUrlRetries;
const playUrlRetries =
typeof rawRetries === "number" && Number.isFinite(rawRetries)
? Math.min(5, Math.max(0, Math.trunc(rawRetries)))
: defaults.playUrlRetries;
// Voice ducking is opt-in and the retained-volume percentage is consumed
// directly by the audio path. Only a plain-object block with correctly
// typed, finite and in-range fields may override the safe defaults.
@@ -457,6 +483,7 @@ export function loadConfig(path: string): BotConfig {
enabledProviders,
savedQueuesEnabled,
playKeepsQueue,
playUrlRetries,
voiceDucking,
defaultPlatform: defaultPlatformPref,
};