Spaces:
Running
Running
JOHN SILLVESTOR commited on
Optimize stream startup latency and multi-bot chunk throughput
Browse files
server.js
CHANGED
|
@@ -44,8 +44,8 @@ const PROBE_HEAD = 8 * MB;
|
|
| 44 |
const BOT_MAX = 100;
|
| 45 |
const PIPE_MS = 30000;
|
| 46 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 47 |
-
const RANGE_LOOKAHEAD =
|
| 48 |
-
const WARM_AHEAD =
|
| 49 |
const WARM_BUDGET_BYTES = 128 * MB;
|
| 50 |
let warmBytesInFlight = 0;
|
| 51 |
const WARM_BATCH = Math.max(2, Math.min(4, CPU_N * 2));
|
|
@@ -1484,7 +1484,7 @@ function waitHotHead(msgId, min, ms) {
|
|
| 1484 |
async function warmHot(bot, rec, msgId) {
|
| 1485 |
const id = String(msgId);
|
| 1486 |
const hit = hotMap.get(id);
|
| 1487 |
-
const headNeed = Math.min(rec.size, CHUNK_SIZE);
|
| 1488 |
if (hit && hit.head && hit.head.length >= headNeed && (durCache.get(id) > 1 || hit.tail)) return;
|
| 1489 |
if (inflightHot.has(id)) return inflightHot.get(id);
|
| 1490 |
let finish;
|
|
@@ -1856,8 +1856,8 @@ async function fetchSliceChunk(bot, rec, msgId, aligned, onPiece) {
|
|
| 1856 |
if (onPiece) onPiece(recheck, aligned);
|
| 1857 |
return recheck;
|
| 1858 |
}
|
| 1859 |
-
let downloadBot = bot;
|
| 1860 |
-
if (!downloadBot || !botUsable(downloadBot)
|
| 1861 |
downloadBot = pickBot(undefined, undefined, msgId) || downloadBot;
|
| 1862 |
}
|
| 1863 |
if (!downloadBot || !downloadBot.client) throw new Error("nobot");
|
|
@@ -2213,6 +2213,7 @@ async function handleGenerate(req, res) {
|
|
| 2213 |
return json(res, 400, { error: "hash" });
|
| 2214 |
}
|
| 2215 |
warmHot(bot, rec, msgId);
|
|
|
|
| 2216 |
const hid = String(msgId);
|
| 2217 |
if (durCache.get(hid) > 1) rec.duration = durCache.get(hid);
|
| 2218 |
else {
|
|
@@ -2350,6 +2351,18 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2350 |
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId, req.headers["user-agent"]));
|
| 2351 |
return;
|
| 2352 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2353 |
if (await rangeSlicesReady(msgId, rngHot.start, rngHot.end)) {
|
| 2354 |
const diskOk = await pipeDiskRange(msgId, rngHot.start, rngHot.end, req, res, headersHot, sizeHint);
|
| 2355 |
if (diskOk || res.headersSent) return;
|
|
|
|
| 44 |
const BOT_MAX = 100;
|
| 45 |
const PIPE_MS = 30000;
|
| 46 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 47 |
+
const RANGE_LOOKAHEAD = 4;
|
| 48 |
+
const WARM_AHEAD = 4;
|
| 49 |
const WARM_BUDGET_BYTES = 128 * MB;
|
| 50 |
let warmBytesInFlight = 0;
|
| 51 |
const WARM_BATCH = Math.max(2, Math.min(4, CPU_N * 2));
|
|
|
|
| 1484 |
async function warmHot(bot, rec, msgId) {
|
| 1485 |
const id = String(msgId);
|
| 1486 |
const hit = hotMap.get(id);
|
| 1487 |
+
const headNeed = Math.min(rec.size, CHUNK_SIZE * 2);
|
| 1488 |
if (hit && hit.head && hit.head.length >= headNeed && (durCache.get(id) > 1 || hit.tail)) return;
|
| 1489 |
if (inflightHot.has(id)) return inflightHot.get(id);
|
| 1490 |
let finish;
|
|
|
|
| 1856 |
if (onPiece) onPiece(recheck, aligned);
|
| 1857 |
return recheck;
|
| 1858 |
}
|
| 1859 |
+
let downloadBot = pickBot(bot && bot.idx, undefined, msgId) || bot;
|
| 1860 |
+
if (!downloadBot || !botUsable(downloadBot)) {
|
| 1861 |
downloadBot = pickBot(undefined, undefined, msgId) || downloadBot;
|
| 1862 |
}
|
| 1863 |
if (!downloadBot || !downloadBot.client) throw new Error("nobot");
|
|
|
|
| 2213 |
return json(res, 400, { error: "hash" });
|
| 2214 |
}
|
| 2215 |
warmHot(bot, rec, msgId);
|
| 2216 |
+
await waitHotHead(msgId, Math.min(rec.size, CHUNK_SIZE), 350);
|
| 2217 |
const hid = String(msgId);
|
| 2218 |
if (durCache.get(hid) > 1) rec.duration = durCache.get(hid);
|
| 2219 |
else {
|
|
|
|
| 2351 |
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId, req.headers["user-agent"]));
|
| 2352 |
return;
|
| 2353 |
}
|
| 2354 |
+
if (hit && hit.length > 0 && rngHot.start === 0 && (!req.headers.range || req.headers.range === "bytes=0-")) {
|
| 2355 |
+
const serveLen = Math.min(hit.length, needHot);
|
| 2356 |
+
const fastRng = { start: 0, end: serveLen - 1, partial: true };
|
| 2357 |
+
const fastHeaders = mediaHeaders(typeHint, msgId, sizeHint, ticket.h, fastRng);
|
| 2358 |
+
if (res.socket) {
|
| 2359 |
+
try { res.socket.setNoDelay(true); } catch { }
|
| 2360 |
+
}
|
| 2361 |
+
res.writeHead(206, fastHeaders);
|
| 2362 |
+
rememberElstLead(msgId, hit);
|
| 2363 |
+
res.end(patchAvSync(hit.subarray(0, serveLen), 0, msgId, req.headers["user-agent"]));
|
| 2364 |
+
return;
|
| 2365 |
+
}
|
| 2366 |
if (await rangeSlicesReady(msgId, rngHot.start, rngHot.end)) {
|
| 2367 |
const diskOk = await pipeDiskRange(msgId, rngHot.start, rngHot.end, req, res, headersHot, sizeHint);
|
| 2368 |
if (diskOk || res.headersSent) return;
|