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

+19
View File
@@ -75,6 +75,8 @@ export function createBotRouter(
localAudioEnabled: config.localAudioEnabled,
savedQueuesEnabled: config.savedQueuesEnabled,
playKeepsQueue: config.playKeepsQueue,
// Silent re-resolve budget for a track whose URL came back empty.
playUrlRetries: config.playUrlRetries ?? 0,
adminGroups: config.adminGroups ?? [],
guestMode: config.guestMode,
spotify: maskedSpotify(),
@@ -94,6 +96,7 @@ export function createBotRouter(
voiceDucking,
guestMode,
adminGroups,
playUrlRetries,
} = req.body;
const hasIdle = idleTimeoutMinutes !== undefined;
@@ -104,10 +107,24 @@ export function createBotRouter(
const hasAutoPause = typeof autoPauseOnEmpty === "boolean";
const hasLocalAudioEnabled = typeof localAudioEnabled === "boolean";
// Retry budget is an integer 0..5; out-of-range is a client bug, not
// something to silently clamp, so it 400s like the other numeric fields.
const hasPlayUrlRetries = playUrlRetries !== undefined;
if (
hasPlayUrlRetries &&
(typeof playUrlRetries !== "number" ||
!Number.isInteger(playUrlRetries) ||
playUrlRetries < 0 ||
playUrlRetries > 5)
) {
res.status(400).json({ error: "playUrlRetries must be an integer 0-5" });
return;
}
if (hasIdle) config.idleTimeoutMinutes = idleTimeoutMinutes;
if (hasAutoPause) config.autoPauseOnEmpty = autoPauseOnEmpty;
if (hasLocalAudioEnabled) config.localAudioEnabled = localAudioEnabled;
if (hasPlayUrlRetries) config.playUrlRetries = playUrlRetries;
// Voice ducking is a partial settings block. Merge only known, strictly
// valid fields so malformed JSON cannot replace the object or inject NaN /
@@ -283,6 +300,8 @@ export function createBotRouter(
localAudioEnabled: config.localAudioEnabled,
savedQueuesEnabled: config.savedQueuesEnabled,
playKeepsQueue: config.playKeepsQueue,
// Silent re-resolve budget for a track whose URL came back empty.
playUrlRetries: config.playUrlRetries ?? 0,
adminGroups: config.adminGroups ?? [],
guestMode: config.guestMode,
spotify: maskedSpotify(),