Spaces:
Running
Running
JOHN SILLVESTOR commited on
Fit stream cache to 2 vCPU and 16 GB RAM
Browse files- Dockerfile +3 -3
- server.js +14 -9
Dockerfile
CHANGED
|
@@ -10,8 +10,8 @@ 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=
|
| 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))"
|
| 17 |
-
CMD ["node","--max-old-space-size=
|
|
|
|
| 10 |
ENV HOME=/home/node
|
| 11 |
ENV NODE_ENV=production
|
| 12 |
ENV PORT=7860
|
| 13 |
+
ENV UV_THREADPOOL_SIZE=4
|
| 14 |
+
ENV NODE_OPTIONS=--max-old-space-size=4096
|
| 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))"
|
| 17 |
+
CMD ["node","--max-old-space-size=4096","server.js"]
|
server.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
const http = require("http");
|
| 4 |
const fs = require("fs");
|
|
|
|
| 5 |
const path = require("path");
|
| 6 |
const crypto = require("crypto");
|
| 7 |
const { spawn } = require("child_process");
|
|
@@ -20,16 +21,19 @@ const HASH_LEN = 6;
|
|
| 20 |
const TOKEN_TTL = 21600;
|
| 21 |
const MIN_ALIGN = 4096;
|
| 22 |
const MB = 1024 * 1024;
|
|
|
|
|
|
|
| 23 |
const CHUNK_SIZE = 1024 * 1024;
|
| 24 |
-
const HOT_HEAD =
|
| 25 |
-
const HOT_TAIL =
|
| 26 |
const STREAM_WINDOW = 8 * MB;
|
| 27 |
const PROBE_HEAD = 4 * MB;
|
| 28 |
const BOT_MAX = 25;
|
| 29 |
const PIPE_MS = 30000;
|
| 30 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 31 |
-
const RANGE_LOOKAHEAD =
|
| 32 |
-
const WARM_AHEAD =
|
|
|
|
| 33 |
const IP_STREAM_MAX = 5000;
|
| 34 |
const IP_TITLE_MAX = 2500;
|
| 35 |
const UID_STREAM_MAX = 80;
|
|
@@ -44,7 +48,7 @@ const HLS_IP_MAX = 24;
|
|
| 44 |
const HLS_BUCKET = 240;
|
| 45 |
const hlsSegCount = new Map();
|
| 46 |
const DISK_MAX_BYTES = 32 * 1024 * 1024 * 1024;
|
| 47 |
-
const HOT_BYTES_MAX =
|
| 48 |
const BODY_MAX = 262144;
|
| 49 |
const FILE_TTL = 20 * 60 * 1000;
|
| 50 |
const LVL_TTL = 60000;
|
|
@@ -1724,7 +1728,7 @@ function buildHlsUrl(host, msgId, hash, lvl) {
|
|
| 1724 |
+ "&s=" + encodeURIComponent(sig);
|
| 1725 |
}
|
| 1726 |
|
| 1727 |
-
const SLICE_MEM_MAX =
|
| 1728 |
const sliceMemCache = new Map();
|
| 1729 |
|
| 1730 |
function getSliceMem(msgId, offset) {
|
|
@@ -1743,7 +1747,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 |
-
const
|
|
|
|
| 1747 |
while (sliceMemCache.size > memMax) {
|
| 1748 |
const oldest = sliceMemCache.keys().next().value;
|
| 1749 |
sliceMemCache.delete(oldest);
|
|
@@ -2028,8 +2033,8 @@ function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
|
| 2028 |
} catch { }
|
| 2029 |
jobs.push(off);
|
| 2030 |
}
|
| 2031 |
-
for (let i = 0; i < jobs.length; i +=
|
| 2032 |
-
const batch = jobs.slice(i, i +
|
| 2033 |
await Promise.all(batch.map((off) => fetchSliceChunk(bot, rec, msgId, off).catch(() => null)));
|
| 2034 |
}
|
| 2035 |
} catch { }
|
|
|
|
| 2 |
|
| 3 |
const http = require("http");
|
| 4 |
const fs = require("fs");
|
| 5 |
+
const os = require("os");
|
| 6 |
const path = require("path");
|
| 7 |
const crypto = require("crypto");
|
| 8 |
const { spawn } = require("child_process");
|
|
|
|
| 21 |
const TOKEN_TTL = 21600;
|
| 22 |
const MIN_ALIGN = 4096;
|
| 23 |
const MB = 1024 * 1024;
|
| 24 |
+
const CPU_N = Math.max(1, (os.cpus() || []).length || 2);
|
| 25 |
+
const RAM_BYTES = os.totalmem() || (16 * 1024 * MB);
|
| 26 |
const CHUNK_SIZE = 1024 * 1024;
|
| 27 |
+
const HOT_HEAD = 24 * MB;
|
| 28 |
+
const HOT_TAIL = 8 * MB;
|
| 29 |
const STREAM_WINDOW = 8 * MB;
|
| 30 |
const PROBE_HEAD = 4 * MB;
|
| 31 |
const BOT_MAX = 25;
|
| 32 |
const PIPE_MS = 30000;
|
| 33 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 34 |
+
const RANGE_LOOKAHEAD = 8;
|
| 35 |
+
const WARM_AHEAD = 32;
|
| 36 |
+
const WARM_BATCH = Math.max(1, Math.min(2, CPU_N));
|
| 37 |
const IP_STREAM_MAX = 5000;
|
| 38 |
const IP_TITLE_MAX = 2500;
|
| 39 |
const UID_STREAM_MAX = 80;
|
|
|
|
| 48 |
const HLS_BUCKET = 240;
|
| 49 |
const hlsSegCount = new Map();
|
| 50 |
const DISK_MAX_BYTES = 32 * 1024 * 1024 * 1024;
|
| 51 |
+
const HOT_BYTES_MAX = Math.min(1536 * MB, Math.max(512 * MB, Math.floor(RAM_BYTES * 0.1)));
|
| 52 |
const BODY_MAX = 262144;
|
| 53 |
const FILE_TTL = 20 * 60 * 1000;
|
| 54 |
const LVL_TTL = 60000;
|
|
|
|
| 1728 |
+ "&s=" + encodeURIComponent(sig);
|
| 1729 |
}
|
| 1730 |
|
| 1731 |
+
const SLICE_MEM_MAX = Math.max(1024, Math.min(6144, Math.floor((RAM_BYTES * 0.38) / MB)));
|
| 1732 |
const sliceMemCache = new Map();
|
| 1733 |
|
| 1734 |
function getSliceMem(msgId, offset) {
|
|
|
|
| 1747 |
const k = String(msgId) + ":" + String(offset);
|
| 1748 |
if (sliceMemCache.has(k)) sliceMemCache.delete(k);
|
| 1749 |
sliceMemCache.set(k, buf);
|
| 1750 |
+
const heapCut = Math.floor(RAM_BYTES * 0.22);
|
| 1751 |
+
const memMax = (process.memoryUsage().heapUsed > heapCut) ? Math.max(256, Math.floor(SLICE_MEM_MAX / 4)) : SLICE_MEM_MAX;
|
| 1752 |
while (sliceMemCache.size > memMax) {
|
| 1753 |
const oldest = sliceMemCache.keys().next().value;
|
| 1754 |
sliceMemCache.delete(oldest);
|
|
|
|
| 2033 |
} catch { }
|
| 2034 |
jobs.push(off);
|
| 2035 |
}
|
| 2036 |
+
for (let i = 0; i < jobs.length; i += WARM_BATCH) {
|
| 2037 |
+
const batch = jobs.slice(i, i + WARM_BATCH);
|
| 2038 |
await Promise.all(batch.map((off) => fetchSliceChunk(bot, rec, msgId, off).catch(() => null)));
|
| 2039 |
}
|
| 2040 |
} catch { }
|