Spaces:
Running
Running
JOHN SILLVESTOR commited on
Multi-user parallel slice streaming and memory optimization
Browse files
server.js
CHANGED
|
@@ -1743,7 +1743,8 @@ function setSliceMem(msgId, offset, buf) {
|
|
| 1743 |
const k = String(msgId) + ":" + String(offset);
|
| 1744 |
if (sliceMemCache.has(k)) sliceMemCache.delete(k);
|
| 1745 |
sliceMemCache.set(k, buf);
|
| 1746 |
-
|
|
|
|
| 1747 |
const oldest = sliceMemCache.keys().next().value;
|
| 1748 |
sliceMemCache.delete(oldest);
|
| 1749 |
}
|
|
@@ -2009,9 +2010,10 @@ async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
|
| 2009 |
const warmRun = new Set();
|
| 2010 |
function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
| 2011 |
const id = String(msgId);
|
| 2012 |
-
if (!bot || !rec || !id || warmRun.has(id)) return;
|
| 2013 |
-
warmRun.add(id);
|
| 2014 |
const start = Math.floor(Math.max(0, fromOff) / CHUNK_SIZE) * CHUNK_SIZE;
|
|
|
|
|
|
|
|
|
|
| 2015 |
const cap = Number(fileSize) || 0;
|
| 2016 |
(async () => {
|
| 2017 |
try {
|
|
@@ -2032,7 +2034,7 @@ function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
|
| 2032 |
}
|
| 2033 |
} catch { }
|
| 2034 |
finally {
|
| 2035 |
-
warmRun.delete(
|
| 2036 |
}
|
| 2037 |
})();
|
| 2038 |
}
|
|
@@ -2044,7 +2046,11 @@ async function fetchSliceChunk(bot, rec, msgId, aligned) {
|
|
| 2044 |
return chunkFlight.do(flightKey, async () => {
|
| 2045 |
const recheck = await readDiskSlice(msgId, aligned);
|
| 2046 |
if (recheck && recheck.length) return recheck;
|
| 2047 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2048 |
downloadBot.tgLoad = (downloadBot.tgLoad || 0) + 1;
|
| 2049 |
try {
|
| 2050 |
const it = downloadBot.client.iterDownload(rec.loc || rec.doc, {
|
|
@@ -2062,6 +2068,12 @@ async function fetchSliceChunk(bot, rec, msgId, aligned) {
|
|
| 2062 |
writeDiskSlice(msgId, aligned, full).catch(() => { });
|
| 2063 |
}
|
| 2064 |
return full;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2065 |
} finally {
|
| 2066 |
downloadBot.tgLoad = Math.max(0, (downloadBot.tgLoad || 0) - 1);
|
| 2067 |
}
|
|
@@ -2379,6 +2391,10 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2379 |
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId, uaH));
|
| 2380 |
return;
|
| 2381 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2382 |
let pooled = null;
|
| 2383 |
try {
|
| 2384 |
pooled = await fileFromPool(msgId, pickBot(undefined, undefined, msgId));
|
|
@@ -2396,10 +2412,6 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2396 |
}
|
| 2397 |
}
|
| 2398 |
}
|
| 2399 |
-
if (await rangeSlicesReady(msgId, rngHot.start, rngHot.end)) {
|
| 2400 |
-
const diskOk = await pipeDiskRange(msgId, rngHot.start, rngHot.end, req, res, headersHot, sizeHint);
|
| 2401 |
-
if (diskOk || res.headersSent) return;
|
| 2402 |
-
}
|
| 2403 |
}
|
| 2404 |
const prefer = Number.isInteger(ticket.b) ? ticket.b : undefined;
|
| 2405 |
let bot = pickBot(prefer, undefined, msgId);
|
|
|
|
| 1743 |
const k = String(msgId) + ":" + String(offset);
|
| 1744 |
if (sliceMemCache.has(k)) sliceMemCache.delete(k);
|
| 1745 |
sliceMemCache.set(k, buf);
|
| 1746 |
+
const memMax = (process.memoryUsage().heapUsed > 4500 * MB) ? 1024 : SLICE_MEM_MAX;
|
| 1747 |
+
while (sliceMemCache.size > memMax) {
|
| 1748 |
const oldest = sliceMemCache.keys().next().value;
|
| 1749 |
sliceMemCache.delete(oldest);
|
| 1750 |
}
|
|
|
|
| 2010 |
const warmRun = new Set();
|
| 2011 |
function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
| 2012 |
const id = String(msgId);
|
|
|
|
|
|
|
| 2013 |
const start = Math.floor(Math.max(0, fromOff) / CHUNK_SIZE) * CHUNK_SIZE;
|
| 2014 |
+
const warmKey = id + ":" + Math.floor(start / (16 * MB));
|
| 2015 |
+
if (!bot || !rec || !id || warmRun.has(warmKey) || warmRun.size >= 36) return;
|
| 2016 |
+
warmRun.add(warmKey);
|
| 2017 |
const cap = Number(fileSize) || 0;
|
| 2018 |
(async () => {
|
| 2019 |
try {
|
|
|
|
| 2034 |
}
|
| 2035 |
} catch { }
|
| 2036 |
finally {
|
| 2037 |
+
warmRun.delete(warmKey);
|
| 2038 |
}
|
| 2039 |
})();
|
| 2040 |
}
|
|
|
|
| 2046 |
return chunkFlight.do(flightKey, async () => {
|
| 2047 |
const recheck = await readDiskSlice(msgId, aligned);
|
| 2048 |
if (recheck && recheck.length) return recheck;
|
| 2049 |
+
let downloadBot = bot;
|
| 2050 |
+
if (!downloadBot || !botUsable(downloadBot) || (downloadBot.tgLoad || 0) > 0) {
|
| 2051 |
+
downloadBot = pickBot(undefined, undefined, msgId) || downloadBot;
|
| 2052 |
+
}
|
| 2053 |
+
if (!downloadBot || !downloadBot.client) throw new Error("nobot");
|
| 2054 |
downloadBot.tgLoad = (downloadBot.tgLoad || 0) + 1;
|
| 2055 |
try {
|
| 2056 |
const it = downloadBot.client.iterDownload(rec.loc || rec.doc, {
|
|
|
|
| 2068 |
writeDiskSlice(msgId, aligned, full).catch(() => { });
|
| 2069 |
}
|
| 2070 |
return full;
|
| 2071 |
+
} catch (err) {
|
| 2072 |
+
coolBot(downloadBot, err);
|
| 2073 |
+
if (isFileRef(err)) {
|
| 2074 |
+
dropFile(msgId, downloadBot);
|
| 2075 |
+
}
|
| 2076 |
+
throw err;
|
| 2077 |
} finally {
|
| 2078 |
downloadBot.tgLoad = Math.max(0, (downloadBot.tgLoad || 0) - 1);
|
| 2079 |
}
|
|
|
|
| 2391 |
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId, uaH));
|
| 2392 |
return;
|
| 2393 |
}
|
| 2394 |
+
if (await rangeSlicesReady(msgId, rngHot.start, rngHot.end)) {
|
| 2395 |
+
const diskOk = await pipeDiskRange(msgId, rngHot.start, rngHot.end, req, res, headersHot, sizeHint);
|
| 2396 |
+
if (diskOk || res.headersSent) return;
|
| 2397 |
+
}
|
| 2398 |
let pooled = null;
|
| 2399 |
try {
|
| 2400 |
pooled = await fileFromPool(msgId, pickBot(undefined, undefined, msgId));
|
|
|
|
| 2412 |
}
|
| 2413 |
}
|
| 2414 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2415 |
}
|
| 2416 |
const prefer = Number.isInteger(ticket.b) ? ticket.b : undefined;
|
| 2417 |
let bot = pickBot(prefer, undefined, msgId);
|