Spaces:
Running
Running
JOHN SILLVESTOR commited on
perf: high concurrency 10k users streaming optimization
Browse files- Dockerfile +1 -1
- server.js +42 -33
Dockerfile
CHANGED
|
@@ -10,7 +10,7 @@ USER node
|
|
| 10 |
ENV HOME=/home/node
|
| 11 |
ENV NODE_ENV=production
|
| 12 |
ENV PORT=7860
|
| 13 |
-
ENV UV_THREADPOOL_SIZE=
|
| 14 |
ENV NODE_OPTIONS=--max-old-space-size=6144
|
| 15 |
EXPOSE 7860
|
| 16 |
HEALTHCHECK --interval=45s --timeout=5s --start-period=50s --retries=3 CMD node -e "fetch('http://127.0.0.1:7860/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
|
|
|
|
| 10 |
ENV HOME=/home/node
|
| 11 |
ENV NODE_ENV=production
|
| 12 |
ENV PORT=7860
|
| 13 |
+
ENV UV_THREADPOOL_SIZE=64
|
| 14 |
ENV NODE_OPTIONS=--max-old-space-size=6144
|
| 15 |
EXPOSE 7860
|
| 16 |
HEALTHCHECK --interval=45s --timeout=5s --start-period=50s --retries=3 CMD node -e "fetch('http://127.0.0.1:7860/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
|
server.js
CHANGED
|
@@ -25,15 +25,15 @@ const HOT_HEAD = 12 * MB;
|
|
| 25 |
const HOT_TAIL = 12 * MB;
|
| 26 |
const STREAM_WINDOW = 32 * MB;
|
| 27 |
const PROBE_HEAD = 1 * MB;
|
| 28 |
-
const BOT_MAX =
|
| 29 |
const PIPE_MS = 15000;
|
| 30 |
const PIPE_MAX_MS = 10 * 60 * 1000;
|
| 31 |
-
const IP_STREAM_MAX =
|
| 32 |
-
const IP_TITLE_MAX =
|
| 33 |
-
const UID_STREAM_MAX =
|
| 34 |
-
const UID_TITLE_MAX =
|
| 35 |
-
const TITLE_PIPE_MAX =
|
| 36 |
-
const GLOBAL_PIPE_MAX =
|
| 37 |
const DISK_SLICE_DIR = "/tmp/dlk_slice";
|
| 38 |
const HLS_DIR = "/tmp/dlk_hls";
|
| 39 |
const HLS_SRC_DIR = "/tmp/dlk_src";
|
|
@@ -296,7 +296,7 @@ function botUsable(b) {
|
|
| 296 |
if (!b || !b.ready) return false;
|
| 297 |
if (b.coolUntil && Date.now() < b.coolUntil) return false;
|
| 298 |
if (b.client && b.client.connected === false) return false;
|
| 299 |
-
return b.
|
| 300 |
}
|
| 301 |
|
| 302 |
function pipeDead(req, res) {
|
|
@@ -747,23 +747,28 @@ function httpVideoType(mime, name) {
|
|
| 747 |
|
| 748 |
function pickBot(preferIdx, skipIdx, msgId) {
|
| 749 |
if (!bots.length) return null;
|
| 750 |
-
const
|
| 751 |
-
if (!
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
if (msgId != null) {
|
| 754 |
const id = String(msgId);
|
| 755 |
-
const hot =
|
| 756 |
const hit = fileCache.get(id + ":" + b.idx);
|
| 757 |
return hit && Date.now() - hit.ts < FILE_TTL;
|
| 758 |
});
|
| 759 |
if (hot.length) use = hot;
|
| 760 |
}
|
| 761 |
-
const min = Math.min(...use.map((b) => b.
|
| 762 |
if (preferIdx != null) {
|
| 763 |
-
const sticky = use.find((b) => b.idx === preferIdx && b.
|
| 764 |
if (sticky) return sticky;
|
| 765 |
}
|
| 766 |
-
const even = use.filter((b) => b.
|
| 767 |
const bot = even[rr % even.length];
|
| 768 |
rr += 1;
|
| 769 |
return bot;
|
|
@@ -1722,7 +1727,7 @@ function buildHlsUrl(host, msgId, hash, lvl) {
|
|
| 1722 |
+ "&s=" + encodeURIComponent(sig);
|
| 1723 |
}
|
| 1724 |
|
| 1725 |
-
const SLICE_MEM_MAX =
|
| 1726 |
const sliceMemCache = new Map();
|
| 1727 |
|
| 1728 |
function getSliceMem(msgId, offset) {
|
|
@@ -1820,21 +1825,27 @@ async function fetchSliceChunk(bot, rec, msgId, aligned) {
|
|
| 1820 |
return chunkFlight.do(flightKey, async () => {
|
| 1821 |
const recheck = await readDiskSlice(msgId, aligned);
|
| 1822 |
if (recheck && recheck.length) return recheck;
|
| 1823 |
-
const
|
| 1824 |
-
|
| 1825 |
-
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1836 |
}
|
| 1837 |
-
return full;
|
| 1838 |
});
|
| 1839 |
}
|
| 1840 |
|
|
@@ -1905,7 +1916,6 @@ async function* rangeChunks(bot, rec, start, end, stop, msgId) {
|
|
| 1905 |
}
|
| 1906 |
|
| 1907 |
async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId) {
|
| 1908 |
-
bot.load++;
|
| 1909 |
const t0 = Date.now();
|
| 1910 |
let clientClosed = false;
|
| 1911 |
const onDead = () => { clientClosed = true; };
|
|
@@ -1941,7 +1951,6 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 1941 |
} finally {
|
| 1942 |
req.off("close", onDead);
|
| 1943 |
res.off("close", onDead);
|
| 1944 |
-
bot.load = Math.max(0, bot.load - 1);
|
| 1945 |
if (!res.writableEnded && !res.destroyed) {
|
| 1946 |
try { res.end(); } catch { }
|
| 1947 |
}
|
|
@@ -2747,7 +2756,7 @@ async function main() {
|
|
| 2747 |
requestTimeout: 0,
|
| 2748 |
connectionsCheckingInterval: 10000
|
| 2749 |
}, onReq);
|
| 2750 |
-
srv.maxConnections =
|
| 2751 |
srv.timeout = 0;
|
| 2752 |
srv.on("clientError", (err, sock) => {
|
| 2753 |
try {
|
|
|
|
| 25 |
const HOT_TAIL = 12 * MB;
|
| 26 |
const STREAM_WINDOW = 32 * MB;
|
| 27 |
const PROBE_HEAD = 1 * MB;
|
| 28 |
+
const BOT_MAX = 25;
|
| 29 |
const PIPE_MS = 15000;
|
| 30 |
const PIPE_MAX_MS = 10 * 60 * 1000;
|
| 31 |
+
const IP_STREAM_MAX = 5000;
|
| 32 |
+
const IP_TITLE_MAX = 2500;
|
| 33 |
+
const UID_STREAM_MAX = 24;
|
| 34 |
+
const UID_TITLE_MAX = 12;
|
| 35 |
+
const TITLE_PIPE_MAX = 15000;
|
| 36 |
+
const GLOBAL_PIPE_MAX = 15000;
|
| 37 |
const DISK_SLICE_DIR = "/tmp/dlk_slice";
|
| 38 |
const HLS_DIR = "/tmp/dlk_hls";
|
| 39 |
const HLS_SRC_DIR = "/tmp/dlk_src";
|
|
|
|
| 296 |
if (!b || !b.ready) return false;
|
| 297 |
if (b.coolUntil && Date.now() < b.coolUntil) return false;
|
| 298 |
if (b.client && b.client.connected === false) return false;
|
| 299 |
+
return (b.tgLoad || 0) < BOT_MAX;
|
| 300 |
}
|
| 301 |
|
| 302 |
function pipeDead(req, res) {
|
|
|
|
| 747 |
|
| 748 |
function pickBot(preferIdx, skipIdx, msgId) {
|
| 749 |
if (!bots.length) return null;
|
| 750 |
+
const readyBots = bots.filter((b) => b && b.ready && (!b.coolUntil || Date.now() >= b.coolUntil) && (b.client && b.client.connected !== false) && b.idx !== skipIdx);
|
| 751 |
+
if (!readyBots.length) {
|
| 752 |
+
const connected = bots.filter((b) => b && b.client && b.client.connected !== false && b.idx !== skipIdx);
|
| 753 |
+
if (!connected.length) return null;
|
| 754 |
+
return connected[rr++ % connected.length];
|
| 755 |
+
}
|
| 756 |
+
const pool = readyBots.filter((b) => (b.tgLoad || 0) < BOT_MAX);
|
| 757 |
+
let use = pool.length ? pool : readyBots;
|
| 758 |
if (msgId != null) {
|
| 759 |
const id = String(msgId);
|
| 760 |
+
const hot = use.filter((b) => {
|
| 761 |
const hit = fileCache.get(id + ":" + b.idx);
|
| 762 |
return hit && Date.now() - hit.ts < FILE_TTL;
|
| 763 |
});
|
| 764 |
if (hot.length) use = hot;
|
| 765 |
}
|
| 766 |
+
const min = Math.min(...use.map((b) => (b.tgLoad || 0)));
|
| 767 |
if (preferIdx != null) {
|
| 768 |
+
const sticky = use.find((b) => b.idx === preferIdx && (b.tgLoad || 0) <= min + 1);
|
| 769 |
if (sticky) return sticky;
|
| 770 |
}
|
| 771 |
+
const even = use.filter((b) => (b.tgLoad || 0) === min);
|
| 772 |
const bot = even[rr % even.length];
|
| 773 |
rr += 1;
|
| 774 |
return bot;
|
|
|
|
| 1727 |
+ "&s=" + encodeURIComponent(sig);
|
| 1728 |
}
|
| 1729 |
|
| 1730 |
+
const SLICE_MEM_MAX = 1536;
|
| 1731 |
const sliceMemCache = new Map();
|
| 1732 |
|
| 1733 |
function getSliceMem(msgId, offset) {
|
|
|
|
| 1825 |
return chunkFlight.do(flightKey, async () => {
|
| 1826 |
const recheck = await readDiskSlice(msgId, aligned);
|
| 1827 |
if (recheck && recheck.length) return recheck;
|
| 1828 |
+
const downloadBot = (bot && botUsable(bot)) ? bot : (pickBot(undefined, undefined, msgId) || bot);
|
| 1829 |
+
downloadBot.tgLoad = (downloadBot.tgLoad || 0) + 1;
|
| 1830 |
+
try {
|
| 1831 |
+
const it = downloadBot.client.iterDownload(rec.loc || rec.doc, {
|
| 1832 |
+
offset: aligned,
|
| 1833 |
+
limit: CHUNK_SIZE,
|
| 1834 |
+
requestSize: CHUNK_SIZE,
|
| 1835 |
+
dcId: rec.dcId
|
| 1836 |
+
});
|
| 1837 |
+
const parts = [];
|
| 1838 |
+
for await (const chunk of it) {
|
| 1839 |
+
parts.push(Buffer.from(chunk));
|
| 1840 |
+
}
|
| 1841 |
+
const full = Buffer.concat(parts);
|
| 1842 |
+
if (full.length) {
|
| 1843 |
+
writeDiskSlice(msgId, aligned, full).catch(() => { });
|
| 1844 |
+
}
|
| 1845 |
+
return full;
|
| 1846 |
+
} finally {
|
| 1847 |
+
downloadBot.tgLoad = Math.max(0, (downloadBot.tgLoad || 0) - 1);
|
| 1848 |
}
|
|
|
|
| 1849 |
});
|
| 1850 |
}
|
| 1851 |
|
|
|
|
| 1916 |
}
|
| 1917 |
|
| 1918 |
async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId) {
|
|
|
|
| 1919 |
const t0 = Date.now();
|
| 1920 |
let clientClosed = false;
|
| 1921 |
const onDead = () => { clientClosed = true; };
|
|
|
|
| 1951 |
} finally {
|
| 1952 |
req.off("close", onDead);
|
| 1953 |
res.off("close", onDead);
|
|
|
|
| 1954 |
if (!res.writableEnded && !res.destroyed) {
|
| 1955 |
try { res.end(); } catch { }
|
| 1956 |
}
|
|
|
|
| 2756 |
requestTimeout: 0,
|
| 2757 |
connectionsCheckingInterval: 10000
|
| 2758 |
}, onReq);
|
| 2759 |
+
srv.maxConnections = 30000;
|
| 2760 |
srv.timeout = 0;
|
| 2761 |
srv.on("clientError", (err, sock) => {
|
| 2762 |
try {
|