mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 11:22:51 +08:00
fix(playback): back off properly when NetEase risk control answers -460
Measured directly against the sidecar on a cloud host, same song, same minute:
GET /song/url/v1?id=1827600686&level=exhigh → 400
{"code":-460,"message":"检测到您的网络环境存在风险,请稍后再试"}
GET …&cookie=<account cookie> → 200, 320k url
So the "无法播放(区域/版权限制)" everyone was chasing is NetEase rejecting
the host's IP, not a copyright or membership problem. It arrives as a 4xx,
which axios reports as ERR_BAD_REQUEST — indistinguishable from a malformed
request without reading the body — and that is why it looked like a dead end in
the logs.
The previous commit retried everything on a flat 250ms backoff. For -460 that
is useless: the risk window lasts a few seconds, so three re-pokes inside 750ms
all land in it and the budget is spent without ever giving the upstream time to
relax. It also spent the 120ms level-switch delay on it, but -460 ignores which
quality we ask for.
Now:
- `netease.getSongUrl` recognises 400 + `code: -460` and rethrows
`RiskControlError` (provider.ts), so the retry layer can tell "wait this out"
from "this track will never play".
- `resolveUrlWithRetry` waits `RISK_CONTROL_BACKOFF_MS * attempt` (800ms,
1600ms, …) instead of 250ms, and never uses the fast level-switch delay while
risk controlled.
- The failure log now says what actually happened
(`hit NetEase risk control (-460) — backing off`) instead of dumping an axios
error that reads like a broken request.
Everything else keeps the old timing: a plain thrown error still retries at
250ms, a genuine 400 (`code: 400`) is not reclassified, and a 200 with `url:null`
still falls through to the quality ladder.
11 new tests: -460 → RiskControlError, genuine 400 untouched, HTML 400
untouched, message preserved, exponential wait, no 120ms switch under risk
control, recovery stops spending, cancellation during a wait.
This commit is contained in:
1 parent
c2771ab9a7
commit
e9995c1e30
6 files changed
+253
-5
No files matched your search
+21
-1
@@ -1,3 +1,4 @@
|
||||
import { RiskControlError } from "../music/provider.js";
|
||||
import type { SongUrlResult } from "../music/provider.js";
|
||||
|
||||
/**
|
||||
@@ -35,6 +36,17 @@ export const DEFAULT_URL_RETRY_BACKOFF_MS = 250;
|
||||
*/
|
||||
export const QUALITY_SWITCH_BACKOFF_MS = 120;
|
||||
|
||||
/**
|
||||
* Delay after a -460 (NetEase IP risk control) failure.
|
||||
*
|
||||
* 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. 800ms × attempt (800, 1600, …) covers roughly the window users
|
||||
* reported clicking through by hand, without stalling the queue for long.
|
||||
*/
|
||||
export const RISK_CONTROL_BACKOFF_MS = 800;
|
||||
|
||||
/**
|
||||
* Normalise a config value into a usable retry count.
|
||||
* Anything non-numeric (legacy config, `"3"`, `null`) means "no retries", which
|
||||
@@ -91,13 +103,20 @@ export async function resolveUrlWithRetry(
|
||||
for (const quality of opts.fallbacks ?? []) plan.push({ quality, fallback: true });
|
||||
|
||||
let sameQualityAttempt = 0;
|
||||
// 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;
|
||||
|
||||
for (let i = 0; i < plan.length; i++) {
|
||||
const step = plan[i];
|
||||
if (opts.isCancelled?.()) return null;
|
||||
|
||||
if (i > 0) {
|
||||
const wait = step.fallback ? QUALITY_SWITCH_BACKOFF_MS : backoffMs * sameQualityAttempt;
|
||||
const wait = riskControlled
|
||||
? RISK_CONTROL_BACKOFF_MS * Math.max(1, sameQualityAttempt)
|
||||
: step.fallback
|
||||
? QUALITY_SWITCH_BACKOFF_MS
|
||||
: backoffMs * sameQualityAttempt;
|
||||
if (wait > 0) await sleep(wait);
|
||||
// The user may have stopped or switched tracks while we waited.
|
||||
if (opts.isCancelled?.()) return null;
|
||||
@@ -110,6 +129,7 @@ export async function resolveUrlWithRetry(
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
riskControlled = error instanceof RiskControlError;
|
||||
opts.onAttemptFailed?.({ attempt: i + 1, total: plan.length, quality: step.quality, error });
|
||||
// Only same-quality attempts grow the backoff; a level switch resets it.
|
||||
sameQualityAttempt = step.fallback ? 0 : sameQualityAttempt + 1;
|
||||
|
||||
Reference in new issue
Block a user