From d2c44ed256e066125c808cbc66be342b765cd235 Mon Sep 17 00:00:00 2001 From: jiayuLian <87891011+jiayuLian@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:13:39 +0800 Subject: [PATCH] fix(playback): stop walking the quality ladder once -460 hits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -460 is NetEase refusing the caller's IP, not a property of the track, so asking for a lower quality cannot succeed — it only adds another RISK_CONTROL_BACKOFF_MS of silence before the same error surfaces. With the default budget (2 retries + 2 NetEase fallback levels) a blocked host spent ~17s before the user saw anything, which is worse than the instant failure this replaced. Bail out as soon as the phase changes instead. --- src/bot/url-retry.test.ts | 39 +++++++++++++++++++++++++++++++++------ src/bot/url-retry.ts | 8 ++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/bot/url-retry.test.ts b/src/bot/url-retry.test.ts index 3193e76..6ab20ea 100644 --- a/src/bot/url-retry.test.ts +++ b/src/bot/url-retry.test.ts @@ -341,20 +341,47 @@ describe("resolveUrlWithRetry — risk control backoff", () => { for (const d of delays) expect(d).toBeGreaterThan(DEFAULT_URL_RETRY_BACKOFF_MS); }); - it("does not fall back to the fast level-switch delay while risk controlled", async () => { + it("gives up instead of stepping down the ladder while risk controlled", async () => { + let attempts = 0; const { sleep, delays } = recordingSleep(); - await resolveUrlWithRetry({ - retries: 0, + const result = await resolveUrlWithRetry({ + retries: 1, resolve: async () => { + attempts += 1; throw new RiskControlError("网络环境风险 (-460)"); }, fallbacks: ["higher", "standard"], sleep, }); - // -460 ignores which quality we ask for, so 120ms would just burn attempts. - expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS, RISK_CONTROL_BACKOFF_MS]); - expect(delays).not.toContain(QUALITY_SWITCH_BACKOFF_MS); + expect(result).toBeNull(); + // 1 + retries attempts at the configured quality, then stop. -460 ignores + // which level we ask for, so each ladder step would only add another + // RISK_CONTROL_BACKOFF_MS of silence before the same failure surfaces. + expect(attempts).toBe(2); + expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS]); + }); + + it("still walks the ladder when the risk control clears on a retry", async () => { + // First attempt blocked by the IP, second one just has no URL — the cause + // changed, so a lower level is worth trying again. + let attempts = 0; + const result = await resolveUrlWithRetry({ + retries: 1, + resolve: async (quality) => { + attempts += 1; + if (attempts === 1) throw new RiskControlError("网络环境风险 (-460)"); + if (quality === "standard") return ok("https://cdn.example/128k.mp3"); + return null; + }, + fallbacks: ["higher", "standard"], + sleep: async () => {}, + }); + + expect(result?.url).toBe("https://cdn.example/128k.mp3"); + // 2 same-quality attempts, then both ladder steps — the second attempt was a + // plain miss, not -460, so the ladder keeps its normal cost. + expect(attempts).toBe(4); }); it("recovers as soon as the window passes, and stops spending", async () => { diff --git a/src/bot/url-retry.ts b/src/bot/url-retry.ts index 717e181..dd105e3 100644 --- a/src/bot/url-retry.ts +++ b/src/bot/url-retry.ts @@ -130,6 +130,14 @@ export async function resolveUrlWithRetry( const step = plan[i]; if (opts.isCancelled?.()) return null; + // -460 is an IP-level refusal, not a property of this track: the quality we + // ask for is irrelevant to it, so stepping down the ladder cannot succeed + // and each extra step costs another RISK_CONTROL_BACKOFF_MS. With the + // default budget that turned a blocked host into 17s of silence before the + // failure message — worse than the instant error it replaced. Give up as + // soon as the phase changes rather than when the plan runs out. + if (step.fallback && riskControlled) break; + if (i > 0) { const wait = riskControlled ? RISK_CONTROL_BACKOFF_MS * Math.max(1, sameQualityAttempt)