Spaces:
Running
Running
JOHN SILLVESTOR commited on
Speed optimization: 1080p stream pipeline
Browse files
server.js
CHANGED
|
@@ -18,18 +18,17 @@ const HASH_LEN = 6;
|
|
| 18 |
const TOKEN_TTL = 1200;
|
| 19 |
const MIN_ALIGN = 4096;
|
| 20 |
const MB = 1024 * 1024;
|
| 21 |
-
const
|
| 22 |
-
const
|
| 23 |
-
const HOT_HEAD = 2 * MB;
|
| 24 |
const HOT_TAIL = 2 * MB;
|
| 25 |
-
const STREAM_WINDOW =
|
| 26 |
const BOT_MAX = 12;
|
| 27 |
const PIPE_MS = 45000;
|
| 28 |
const PIPE_MAX_MS = 30 * 60 * 1000;
|
| 29 |
const IP_STREAM_MAX = 5;
|
| 30 |
const UID_STREAM_MAX = 3;
|
| 31 |
const GLOBAL_PIPE_MAX = 96;
|
| 32 |
-
const HOT_BYTES_MAX =
|
| 33 |
const BODY_MAX = 262144;
|
| 34 |
const FILE_TTL = 20 * 60 * 1000;
|
| 35 |
const LVL_TTL = 60000;
|
|
@@ -1199,14 +1198,6 @@ function pipeBudget(start, end) {
|
|
| 1199 |
return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
|
| 1200 |
}
|
| 1201 |
|
| 1202 |
-
function pickGetSize(room, want, first) {
|
| 1203 |
-
const cap = first ? Math.min(want, FIRST_GET) : want;
|
| 1204 |
-
const max = Math.min(room, cap, GET_SIZES[0]);
|
| 1205 |
-
for (let i = 0; i < GET_SIZES.length; i++) {
|
| 1206 |
-
if (GET_SIZES[i] <= max) return GET_SIZES[i];
|
| 1207 |
-
}
|
| 1208 |
-
return MIN_ALIGN;
|
| 1209 |
-
}
|
| 1210 |
|
| 1211 |
function hotEntry(msgId, size) {
|
| 1212 |
const id = String(msgId);
|
|
@@ -1339,46 +1330,43 @@ async function warmHot(bot, rec, msgId) {
|
|
| 1339 |
}
|
| 1340 |
|
| 1341 |
async function* rangeChunks(bot, rec, start, end, stop) {
|
| 1342 |
-
let
|
| 1343 |
-
|
| 1344 |
-
while (pos <= end) {
|
| 1345 |
if (stop && stop()) break;
|
| 1346 |
-
const aligned = Math.floor(
|
| 1347 |
-
let skip =
|
| 1348 |
-
const
|
| 1349 |
-
const room = Math.max(MIN_ALIGN, pageEnd - aligned);
|
| 1350 |
-
const remain = end - pos + 1;
|
| 1351 |
-
const reqSize = pickGetSize(room, skip + remain, first);
|
| 1352 |
-
first = false;
|
| 1353 |
-
const take = Math.min(remain, reqSize - skip);
|
| 1354 |
-
if (take <= 0) break;
|
| 1355 |
const it = bot.client.iterDownload(rec.loc || rec.doc, {
|
| 1356 |
offset: aligned,
|
| 1357 |
-
limit: skip +
|
| 1358 |
-
requestSize:
|
| 1359 |
dcId: rec.dcId
|
| 1360 |
});
|
| 1361 |
-
let
|
| 1362 |
-
|
| 1363 |
-
|
| 1364 |
-
|
| 1365 |
-
|
| 1366 |
-
if (
|
| 1367 |
-
|
| 1368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1369 |
}
|
| 1370 |
-
|
| 1371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1372 |
}
|
| 1373 |
-
|
| 1374 |
-
|
| 1375 |
-
if (!buf.length) continue;
|
| 1376 |
-
sent += buf.length;
|
| 1377 |
-
yield buf;
|
| 1378 |
-
if (sent >= take) break;
|
| 1379 |
}
|
| 1380 |
-
if (
|
| 1381 |
-
pos += sent;
|
| 1382 |
}
|
| 1383 |
}
|
| 1384 |
|
|
@@ -1555,7 +1543,7 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1555 |
return res.end();
|
| 1556 |
}
|
| 1557 |
if (rngHot.start === 0 && !hotHeadLen(msgId)) {
|
| 1558 |
-
await waitHotHead(msgId,
|
| 1559 |
}
|
| 1560 |
const hit = hotPrefix(msgId, rngHot.start, rngHot.end);
|
| 1561 |
if (hit && hit.length) {
|
|
|
|
| 18 |
const TOKEN_TTL = 1200;
|
| 19 |
const MIN_ALIGN = 4096;
|
| 20 |
const MB = 1024 * 1024;
|
| 21 |
+
const CHUNK_SIZE = 512 * 1024;
|
| 22 |
+
const HOT_HEAD = 6 * MB;
|
|
|
|
| 23 |
const HOT_TAIL = 2 * MB;
|
| 24 |
+
const STREAM_WINDOW = 24 * MB;
|
| 25 |
const BOT_MAX = 12;
|
| 26 |
const PIPE_MS = 45000;
|
| 27 |
const PIPE_MAX_MS = 30 * 60 * 1000;
|
| 28 |
const IP_STREAM_MAX = 5;
|
| 29 |
const UID_STREAM_MAX = 3;
|
| 30 |
const GLOBAL_PIPE_MAX = 96;
|
| 31 |
+
const HOT_BYTES_MAX = 2048 * MB;
|
| 32 |
const BODY_MAX = 262144;
|
| 33 |
const FILE_TTL = 20 * 60 * 1000;
|
| 34 |
const LVL_TTL = 60000;
|
|
|
|
| 1198 |
return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
|
| 1199 |
}
|
| 1200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1201 |
|
| 1202 |
function hotEntry(msgId, size) {
|
| 1203 |
const id = String(msgId);
|
|
|
|
| 1330 |
}
|
| 1331 |
|
| 1332 |
async function* rangeChunks(bot, rec, start, end, stop) {
|
| 1333 |
+
let at = start;
|
| 1334 |
+
while (at <= end) {
|
|
|
|
| 1335 |
if (stop && stop()) break;
|
| 1336 |
+
const aligned = Math.floor(at / CHUNK_SIZE) * CHUNK_SIZE;
|
| 1337 |
+
let skip = at - aligned;
|
| 1338 |
+
const need = end - at + 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1339 |
const it = bot.client.iterDownload(rec.loc || rec.doc, {
|
| 1340 |
offset: aligned,
|
| 1341 |
+
limit: skip + need,
|
| 1342 |
+
requestSize: CHUNK_SIZE,
|
| 1343 |
dcId: rec.dcId
|
| 1344 |
});
|
| 1345 |
+
let got = 0;
|
| 1346 |
+
try {
|
| 1347 |
+
for await (const chunk of it) {
|
| 1348 |
+
if (stop && stop()) break;
|
| 1349 |
+
let buf = Buffer.from(chunk);
|
| 1350 |
+
if (skip > 0) {
|
| 1351 |
+
if (buf.length <= skip) {
|
| 1352 |
+
skip -= buf.length;
|
| 1353 |
+
continue;
|
| 1354 |
+
}
|
| 1355 |
+
buf = buf.subarray(skip);
|
| 1356 |
+
skip = 0;
|
| 1357 |
}
|
| 1358 |
+
const left = end - at + 1;
|
| 1359 |
+
if (buf.length > left) buf = buf.subarray(0, left);
|
| 1360 |
+
if (!buf.length) continue;
|
| 1361 |
+
at += buf.length;
|
| 1362 |
+
got += buf.length;
|
| 1363 |
+
yield buf;
|
| 1364 |
+
if (at > end || (stop && stop())) break;
|
| 1365 |
}
|
| 1366 |
+
} catch {
|
| 1367 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1368 |
}
|
| 1369 |
+
if (got <= 0) break;
|
|
|
|
| 1370 |
}
|
| 1371 |
}
|
| 1372 |
|
|
|
|
| 1543 |
return res.end();
|
| 1544 |
}
|
| 1545 |
if (rngHot.start === 0 && !hotHeadLen(msgId)) {
|
| 1546 |
+
await waitHotHead(msgId, 131072, 400);
|
| 1547 |
}
|
| 1548 |
const hit = hotPrefix(msgId, rngHot.start, rngHot.end);
|
| 1549 |
if (hit && hit.length) {
|