Skip to content

一分钟搭建影视站,支持Docker等部署方式

feat(live): 测活取消 400 上限,支持万级频道全量测活,版本 2.10.0

bestZwei27648f682245
diff --git a/package-lock.json b/package-lock.json
index 599e448..600dad8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "libretv",
- "version": "2.9.6",
+ "version": "2.10.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "libretv",
- "version": "2.9.6",
+ "version": "2.10.0",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@tanstack/react-query": "^5.59.0",
diff --git a/package.json b/package.json
index df7f111..5c8d2aa 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "libretv",
- "version": "2.9.6",
+ "version": "2.10.0",
"private": true,
"description": "免费在线视频搜索与观看平台(Next.js 重构版)",
"license": "AGPL-3.0-or-later",
diff --git a/src/app/api/live/probe/route.ts b/src/app/api/live/probe/route.ts
index 01a0d3e..4c908fa 100644
--- a/src/app/api/live/probe/route.ts
+++ b/src/app/api/live/probe/route.ts
@@ -22,7 +22,9 @@ export const dynamic = 'force-dynamic';
*
* 性能与准确性要点:
* - 每个频道有整体时间预算(URL_BUDGET_MS),三级探测共享,避免逐级 5s 叠加成长尾;
- * - 结果带短 TTL 缓存(成功 10min / 失败 2min),多用户重复测活不再重复打上游;
+ * - 结果带短 TTL 缓存(成功 30min / 失败 2min),多用户重复测活不再重复打上游;
+ * - 全局按 host 限并发(单 host 在途 ≤3):同一运营商的频道常共用 host,
+ * 批量测活不限流会以 16×N 的并发冲击同一弱源,被打挂甚至封服务器 IP(越测越红);
* - 请求带源站 Referer,消除「源校验 Referer 导致的可播却测不通」假阴性;
* - 校验 m3u8 的 #EXTM3U 前缀与直链的 content-type,剔除 200 的 HTML/JSON 错误页;
* - master playlist 解析 CODECS 一并返回,前端据此提示 H.265 等浏览器无法解码的情况;
@@ -53,10 +55,14 @@ const THROUGHPUT_BYTES = 128 * 1024;
const THROUGHPUT_BUDGET_MS = 2500;
/** 吞吐采样所需的最低剩余预算:预算不足时跳过采样,避免拖长探测长尾 */
const THROUGHPUT_MIN_REMAINING_MS = 1500;
-/** 结果缓存:成功结果稳定,失败可能是瞬断,分开设置 */
-const CACHE_TTL_OK_MS = 10 * 60 * 1000;
+/**
+ * 结果缓存:成功结果稳定,失败可能是瞬断,分开设置。
+ * 成功 30min:客户端结果本身有 6h TTL,这里覆盖多用户/多轮补测场景,避免重复打上游;
+ * 容量需 ≥ 典型全量频道数:几千频道的列表一轮测完若把缓存挤爆(逐出最旧),重测时会全部穿透。
+ */
+const CACHE_TTL_OK_MS = 30 * 60 * 1000;
const CACHE_TTL_FAIL_MS = 2 * 60 * 1000;
-const CACHE_MAX_ENTRIES = 2000;
+const CACHE_MAX_ENTRIES = 20_000;
const UA =
process.env.USER_AGENT ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
@@ -384,6 +390,59 @@ async function probeOne(url: string): Promise<ProbeOutcome> {
}
}
+/**
+ * 全局按 host 限并发:IPTV 列表里同一运营商的几十个频道常共用一个 host,
+ * 批量测活若不限流,会以「客户端多批次 × 服务端 CONCURRENCY」的并发冲击同一源,
+ * 弱源服务器会被打挂甚至封掉本服务 IP(表现为越测越红)。
+ * 同一 host 最多 PER_HOST_CONCURRENCY 个在途探测,跨请求共享(模块级闸门),
+ * 不同 host 互不影响;名额满时 FIFO 排队,空闲闸门及时清理避免 Map 无界增长。
+ */
+const PER_HOST_CONCURRENCY = 3;
+
+const hostGates = new Map<string, { active: number; waiters: (() => void)[] }>();
+
+function hostOf(url: string): string {
+ try {
+ return new URL(url).host;
+ } catch {
+ return url;
+ }
+}
+
+/** 取一个该 host 的在途名额,返回释放函数;名额满时排队等待 */
+function acquireHostSlot(host: string): Promise<() => void> {
+ let gate = hostGates.get(host);
+ if (!gate) {
+ gate = { active: 0, waiters: [] };
+ hostGates.set(host, gate);
+ }
+ const g = gate;
+ return new Promise((resolve) => {
+ const take = () => {
+ g.active++;
+ resolve(() => {
+ g.active--;
+ // 唤醒的等待者同步接管名额(active-- 后立即 ++),不留被新请求插队的竞态窗口
+ const next = g.waiters.shift();
+ if (next) next();
+ else if (g.active === 0) hostGates.delete(host);
+ });
+ };
+ if (g.active < PER_HOST_CONCURRENCY) take();
+ else g.waiters.push(take);
+ });
+}
+
+/** 在 host 闸门内探测单个频道(probeOne 内部已兜底,不会抛出) */
+async function probeWithHostLimit(url: string): Promise<ProbeOutcome> {
+ const release = await acquireHostSlot(hostOf(url));
+ try {
+ return await probeOne(url);
+ } finally {
+ release();
+ }
+}
+
const NDJSON_HEADERS = {
'Content-Type': 'application/x-ndjson; charset=utf-8',
'Cache-Control': 'no-store, no-transform',
@@ -431,7 +490,7 @@ function ndjsonResponse(urls: string[], reqSignal: AbortSignal): Response {
while (cursor < pending.length) {
if (closed || reqSignal.aborted) return;
const url = pending[cursor++];
- const outcome = await probeOne(url);
+ const outcome = await probeWithHostLimit(url);
if (closed || reqSignal.aborted) return;
cacheSet(url, outcome);
send(outcome);
@@ -518,7 +577,7 @@ export async function POST(req: Request) {
const worker = async () => {
while (cursor < pending.length) {
const url = pending[cursor++];
- const outcome = await probeOne(url);
+ const outcome = await probeWithHostLimit(url);
cacheSet(url, outcome);
outcomes.set(url, outcome);
}
diff --git a/src/components/live-channel-list.tsx b/src/components/live-channel-list.tsx
index 0d7fac7..33bd974 100644
--- a/src/components/live-channel-list.tsx
+++ b/src/components/live-channel-list.tsx
@@ -29,6 +29,16 @@ import type { LiveChannel } from '@/lib/types';
const SEARCH_DEBOUNCE_MS = 200;
+/** 测活进度文案:按已完成均速估算剩余时间(长尾在途期间由每秒 tick 重渲染保持走动) */
+function formatProbeProgress(p: { done: number; total: number; startedAt: number }): string {
+ const base = `${p.done}/${p.total}`;
+ const remaining = p.total - p.done;
+ if (remaining <= 0) return base;
+ if (p.done <= 0) return `${base} · 估算中…`;
+ const remainMin = Math.ceil(((Date.now() - p.startedAt) / p.done) * remaining / 60000);
+ return `${base} · 预计剩余 ${Math.max(1, remainMin)} 分钟`;
+}
+
export interface LiveChannelItem extends LiveChannel {
/** 来源订阅的 EPG 地址(用于节目单查询) */
epg?: string;
@@ -72,7 +82,23 @@ export function LiveChannelList({ channels, groups, currentUrl, onSelect, onFilt
/** 键盘光标(filtered 的下标),与"正在播放"高亮独立 */
const [cursor, setCursor] = useState(-1);
const listRef = useRef<HTMLDivElement>(null);
- const { results: probeResults, progress: probeProgress, probe, clear: clearProbe, isProbing, hint: probeHint } = useLiveProbe();
+ const {
+ results: probeResults,
+ progress: probeProgress,
+ probe,
+ cancel: cancelProbe,
+ clear: clearProbe,
+ isProbing,
+ hint: probeHint,
+ } = useLiveProbe();
+
+ // 测活 ETA 每秒刷新:探测长尾(在途请求未返回)期间进度不推进,剩余时间也要走动
+ const [, setProbeTick] = useState(0);
+ useEffect(() => {
+ if (!isProbing) return;
+ const timer = setInterval(() => setProbeTick((n) => n + 1), 1000);
+ return () => clearInterval(timer);
+ }, [isProbing]);
const favSet = useMemo(() => new Set(liveFavorites), [liveFavorites]);
const recentOrder = useMemo(() => new Map(liveRecent.map((r, i) => [r.url, i] as const)), [liveRecent]);
@@ -246,7 +272,7 @@ export function LiveChannelList({ channels, groups, currentUrl, onSelect, onFilt
className="btn-ghost !py-1 !px-2 text-xs"
disabled={isProbing || filtered.length === 0}
onClick={() => void probe(filtered)}
- title="探测当前列表频道是否可播(分片级校验,每批 50 条并发,结果 6 小时内有效)"
+ title="探测当前列表频道是否可播(分片级校验;量大时自动分批排队跑完,结果 6 小时内有效)"
>
⚡ 测活
</button>
@@ -264,11 +290,20 @@ export function LiveChannelList({ channels, groups, currentUrl, onSelect, onFilt
</button>
)}
{isProbing && probeProgress && (
- <span className="text-[10px] text-faint">
- 探测中 {probeProgress.done}/{probeProgress.total}
- </span>
+ <>
+ <span className="text-[10px] text-faint whitespace-nowrap">
+ 探测中 {formatProbeProgress(probeProgress)}
+ </span>
+ <button
+ className="btn-ghost !py-1 !px-2 text-xs"
+ onClick={cancelProbe}
+ title="中止本次测活,已完成的结果会保留"
+ >
+ 取消
+ </button>
+ </>
)}
- {probeHint && !isProbing && (
+ {probeHint && (
<span className="text-[10px] text-faint">{probeHint}</span>
)}
{probeResults.size > 0 && !isProbing && (
diff --git a/src/components/use-live-probe.ts b/src/components/use-live-probe.ts
index 47945c9..fad5bf5 100644
--- a/src/components/use-live-probe.ts
+++ b/src/components/use-live-probe.ts
@@ -9,7 +9,9 @@ import { LIVE_PROBE_TTL_MS, useAppStore, type LiveProbeEntry } from '@/lib/store
* - 结果持久化到 store(liveProbeResults),6 小时内直接复用(LIVE_PROBE_TTL_MS);
* - 触发探测时只补测缺失或已过期的频道,全部仍有效则提示跳过;
* - 服务端以 NDJSON 流式逐条返回,结果边测边写入列表(节流合并,避免高频 re-render);
- * - 每批 CHUNK_SIZE 条并行 CHUNK_CONCURRENCY 批,服务端批内并发 16;
+ * - 每批 CHUNK_SIZE 条并行 CHUNK_CONCURRENCY 批,服务端批内并发 16、单 host 在途 ≤3(按源排队,不冲垮弱源);
+ * - 目标数无上限(仅 MAX_TARGETS 保险阀防病态列表):几千频道自动分批排队跑完,
+ * 进度带 ETA,可随时取消(保留已完成的结果);
* - 重新测活/清空时 abort 在途请求并作废上一轮(runId 比对)。
*/
@@ -28,13 +30,17 @@ export interface ProbeResult {
const CHUNK_SIZE = 50;
/** 同时在途的批次数:与 CHUNK_SIZE 相乘即最大并发探测规模 */
const CHUNK_CONCURRENCY = 2;
-const MAX_TARGETS = 400;
+/**
+ * 单轮测活目标数上限(保险阀):正常几千个频道分批排队都能跑完,不设人为瓶颈;
+ * 只防病态超大列表(如数十万行的脏 M3U)拖死浏览器与服务端,超出部分提示后丢弃。
+ */
+const MAX_TARGETS = 20_000;
/** 流式结果的写回节流间隔:把高频到达的结果合并成一次 store 更新 */
const FLUSH_INTERVAL_MS = 200;
export function useLiveProbe() {
const cache = useAppStore((s) => s.liveProbeResults);
- const [progress, setProgress] = useState<{ done: number; total: number } | null>(null);
+ const [progress, setProgress] = useState<{ done: number; total: number; startedAt: number } | null>(null);
const [hint, setHint] = useState('');
const runIdRef = useRef(0);
const hintTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -96,10 +102,23 @@ export function useLiveProbe() {
setProgress(null);
}, []);
+ /** 中止在途测活:已完成的结果保留,未完成的频道保持待测状态(下次测活会补上) */
+ const cancel = useCallback(() => {
+ if (!abortRef.current) return;
+ runIdRef.current++;
+ abortRef.current.abort();
+ abortRef.current = null;
+ setProgress(null);
+ }, []);
+
const probe = useCallback(
async (targets: { url: string }[]) => {
- const urls = [...new Set(targets.map((t) => t.url))].slice(0, MAX_TARGETS);
- if (urls.length === 0) return;
+ const allUrls = [...new Set(targets.map((t) => t.url))];
+ if (allUrls.length === 0) return;
+ if (allUrls.length > MAX_TARGETS) {
+ showHint(`频道数超过 ${MAX_TARGETS},仅测前 ${MAX_TARGETS} 个,请精简订阅源`);
+ }
+ const urls = allUrls.slice(0, MAX_TARGETS);
// 6 小时内已测过的直接复用,仅补测缺失或过期的频道
const now = Date.now();
@@ -123,7 +142,9 @@ export function useLiveProbe() {
for (let i = 0; i < stale.length; i += CHUNK_SIZE) {
chunks.push(stale.slice(i, i + CHUNK_SIZE));
}
- setProgress({ done: 0, total: stale.length });
+ // startedAt 供 UI 按已完成均速估算剩余时间
+ const startedAt = Date.now();
+ setProgress({ done: 0, total: stale.length, startedAt });
let done = 0;
let buffer: Record<string, LiveProbeEntry> = {};
@@ -140,7 +161,7 @@ export function useLiveProbe() {
buffer = {};
useAppStore.getState().setLiveProbeResults(entries);
}
- setProgress({ done: Math.min(done, stale.length), total: stale.length });
+ setProgress({ done: Math.min(done, stale.length), total: stale.length, startedAt });
};
const scheduleFlush = () => {
if (!flushTimer) flushTimer = setTimeout(flush, FLUSH_INTERVAL_MS);
@@ -196,5 +217,5 @@ export function useLiveProbe() {
[showHint]
);
- return { results, progress, probe, clear, isProbing: progress !== null, hint };
+ return { results, progress, probe, cancel, clear, isProbing: progress !== null, hint };
}