fix(playback): stop walking the quality ladder once -460 hits

-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.
This commit is contained in:
jiayuLian committed 2026-11-09 20:13:39 +08:00
1 parent d8a8574323
commit d2c44ed256
2 files changed
+41 -6

No files matched your search

+33 -6
View File
@@ -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 () => {
+8
View File
@@ -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)