tune(playback): spend two requests on -460, not three with a 7.5s tail

The risk-control branch grew its delay per attempt (2.5s, then 7.5s) on the
assumption that a blocked IP unlocks on a timer. Measured behaviour says
otherwise: six back-to-back 128k requests from a blocked host answered
-460, -460, -460, -460, 200 — it lets a few through after a while, it does not
unlock at a fixed moment. So the third attempt mostly bought silence: five
requests and ~4s worst case for the same "still blocked".

Cap the whole risk-controlled budget at two requests (one retry after 2.5s)
and skip the quality ladder in that state, which -460 ignores anyway. Worst
case drops to 2.5s, and when it still fails the message now names the wait
(「约 5 秒后再试」) instead of 「请稍后重试」 — a spinner cannot be acted on, a
number can.
This commit is contained in:
jiayuLian committed 2026-11-11 22:44:15 +08:00
1 parent 178935a63d
commit 97ee05f1ef
3 files changed
+56 -35

No files matched your search

+21 -20
View File
@@ -325,27 +325,27 @@ describe("resolveUrlWithRetry — quality fallback", () => {
// window and the whole budget is wasted. These tests pin the longer wait.
// ---------------------------------------------------------------------------
describe("resolveUrlWithRetry — risk control backoff", () => {
it("grows the wait from RISK_CONTROL_BACKOFF_MS instead of 250ms", async () => {
it("retries once after RISK_CONTROL_BACKOFF_MS, then stops", async () => {
const { sleep, delays } = recordingSleep();
const result = await resolveUrlWithRetry({
retries: 2,
resolve: async () => {
throw new RiskControlError("网络环境风险 (-460)");
},
sleep,
const resolve = vi.fn(async () => {
throw new RiskControlError("网络环境风险 (-460)");
});
const result = await resolveUrlWithRetry({ retries: 2, resolve, sleep });
expect(result).toBeNull();
expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS, RISK_CONTROL_BACKOFF_MS * 2]);
// Every one of them is longer than a plain transient retry would have been.
for (const d of delays) expect(d).toBeGreaterThan(DEFAULT_URL_RETRY_BACKOFF_MS);
// One request plus exactly one retry. The old shape waited 2.5s and then
// 7.5s, spending three requests to reach the same "still blocked".
expect(resolve).toHaveBeenCalledTimes(2);
expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS]);
// Still far longer than a plain transient retry would have waited.
expect(delays[0]).toBeGreaterThan(DEFAULT_URL_RETRY_BACKOFF_MS);
});
it("gives up instead of stepping down the ladder while risk controlled", async () => {
it("caps the budget at two requests regardless of retries, and skips the ladder", async () => {
let attempts = 0;
const { sleep, delays } = recordingSleep();
const result = await resolveUrlWithRetry({
retries: 1,
retries: 5,
resolve: async () => {
attempts += 1;
throw new RiskControlError("网络环境风险 (-460)");
@@ -355,9 +355,9 @@ describe("resolveUrlWithRetry — risk control backoff", () => {
});
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.
// -460 ignores which level we ask for, so neither the extra configured
// retries nor the two ladder steps are worth sending: each one only adds
// silence before the same failure surfaces.
expect(attempts).toBe(2);
expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS]);
});
@@ -380,26 +380,27 @@ describe("resolveUrlWithRetry — risk control backoff", () => {
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.
// plain miss, not -460, so the cap no longer applies and the ladder keeps
// its normal cost.
expect(attempts).toBe(4);
});
it("recovers as soon as the window passes, and stops spending", async () => {
it("recovers on the single retry and stops spending", async () => {
let attempts = 0;
const { sleep, delays } = recordingSleep();
const result = await resolveUrlWithRetry({
retries: 3,
resolve: async () => {
attempts += 1;
if (attempts < 3) throw new RiskControlError("网络环境风险 (-460)");
if (attempts < 2) throw new RiskControlError("网络环境风险 (-460)");
return ok("https://cdn.example/after-window.mp3");
},
sleep,
});
expect(result?.url).toBe("https://cdn.example/after-window.mp3");
expect(attempts).toBe(3);
expect(delays).toHaveLength(2);
expect(attempts).toBe(2);
expect(delays).toEqual([RISK_CONTROL_BACKOFF_MS]);
});
it("still uses the short delay for a plain error", async () => {
+31 -14
View File
@@ -37,21 +37,32 @@ export const DEFAULT_URL_RETRY_BACKOFF_MS = 250;
export const QUALITY_SWITCH_BACKOFF_MS = 120;
/**
* Delay after a -460 (NetEase IP risk control) failure.
* Delay before the single retry that follows a -460 (NetEase IP risk control).
*
* This is the one failure mode where the delay is NOT cosmetic: the upstream
* has decided the caller's address looks risky and will keep answering 400 for
* a few seconds no matter what we ask. Re-poking after 250ms just burns the
* budget.
* budget inside the same window.
*
* Sized against the manual workaround users reported: the first click fails, the
* toast sits there for ~2-3s, and the very next click succeeds. So the window is
* a couple of seconds, not a couple hundred milliseconds. 2500ms × attempt puts
* the second attempt at 2.5s and the third at ~7.5s, which is reliably outside
* it; anything shorter just spends the retry budget inside the window.
* Sized against what the window actually looks like: a live host answered
* `-460, -460, -460, -460, 200` on six consecutive 128k requests with no
* waiting at all, i.e. it is "let a few of these through, then stop caring"
* rather than a hard timer. So 2.5s is a good bet for one retry, and waiting
* longer is not: it buys nothing the user cannot do themselves.
*/
export const RISK_CONTROL_BACKOFF_MS = 2500;
/**
* Hard ceiling on requests sent while the IP is risk controlled (1 + 1 retry).
*
* The previous shape grew the delay per attempt (2.5s, then 7.5s), which meant
* up to 10s of a spinner doing nothing before the same error appeared. Measured
* behaviour says a blocked address either clears within a couple of seconds or
* stays blocked; the third and later attempts only add silence. Spending two
* requests and then telling the user to wait ~5s is both faster and honest.
*/
export const MAX_RISK_CONTROL_REQUESTS = 2;
/**
* Normalise a config value into a usable retry count.
* Anything non-numeric (legacy config, `"3"`, `null`) means "no retries", which
@@ -100,7 +111,9 @@ export interface ResolveUrlWithRetryOptions {
*
* Request count: `(1 + retries)` at the configured quality, then one per
* fallback level. With the defaults (2 retries, exhigh, NetEase's two lower
* levels) that is at most 5 requests and ~1.1s of waiting.
* levels) that is at most 5 requests and ~1.1s of waiting — or 2 requests and
* 2.5s when the IP is risk controlled, which is the one case where waiting
* more cannot help.
*/
export async function resolveUrlWithRetry(
opts: ResolveUrlWithRetryOptions,
@@ -120,6 +133,10 @@ export async function resolveUrlWithRetry(
// Set when the previous attempt failed with -460. Risk control ignores the
// quality we ask for, so a level switch does not get to use its short delay.
let riskControlled = false;
// Requests actually sent. Only consulted while risk controlled, where the
// budget is capped at MAX_RISK_CONTROL_REQUESTS instead of the configured
// retry count.
let requestsMade = 0;
// The last failure, kept so the caller can explain the outcome. A `null`
// result (no throw) counts too — it is the providers that have not adopted
// SongUrlUnavailable yet.
@@ -132,15 +149,14 @@ export async function resolveUrlWithRetry(
// -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;
// and each extra step only adds silence. Stop after one retry instead of
// when the plan runs out — the caller reports "try again in a few seconds",
// which is something the user can act on, unlike a longer spinner.
if (riskControlled && (step.fallback || requestsMade >= MAX_RISK_CONTROL_REQUESTS)) break;
if (i > 0) {
const wait = riskControlled
? RISK_CONTROL_BACKOFF_MS * Math.max(1, sameQualityAttempt)
? RISK_CONTROL_BACKOFF_MS
: step.fallback
? QUALITY_SWITCH_BACKOFF_MS
: backoffMs * sameQualityAttempt;
@@ -150,6 +166,7 @@ export async function resolveUrlWithRetry(
}
let error: unknown;
requestsMade += 1;
try {
const result = await opts.resolve(step.quality);
if (result?.url) return result;
+4 -1
View File
@@ -119,7 +119,10 @@ export function riskControlFailure(
platform,
songId,
code: "risk_control",
message: "被音源风控拦截(-460),请稍后重试",
// 「约 5 秒」是有依据的:实测同一 IP 连续请求一般是 -460 几次之后放行,
// 重试预算也已经只留一次(RISK_CONTROL_BACKOFF_MS)。给一个具体的等待值,
// 比「请稍后重试」更能让人知道该干嘛。
message: "被网易云风控拦截(-460),约 5 秒后再试",
upstream: detail ? { detail } : undefined,
};
}