JOHN SILLVESTOR commited on
Commit
cec3b7e
·
verified ·
1 Parent(s): e612a79

Maximize streaming concurrency and remove rate bottleneck

Browse files
Files changed (1) hide show
  1. server.js +53 -44
server.js CHANGED
@@ -41,20 +41,20 @@ const HOT_HEAD = 24 * MB;
41
  const HOT_TAIL = 8 * MB;
42
  const STREAM_WINDOW = 8 * MB;
43
  const PROBE_HEAD = 4 * MB;
44
- const BOT_MAX = 25;
45
  const PIPE_MS = 30000;
46
  const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
47
- const RANGE_LOOKAHEAD = 8;
48
- const WARM_AHEAD = 8;
49
  const WARM_BUDGET_BYTES = 64 * MB;
50
  let warmBytesInFlight = 0;
51
  const WARM_BATCH = Math.max(1, Math.min(2, CPU_N));
52
- const IP_STREAM_MAX = 5000;
53
- const IP_TITLE_MAX = 2500;
54
- const UID_STREAM_MAX = 80;
55
- const UID_TITLE_MAX = 48;
56
- const TITLE_PIPE_MAX = 15000;
57
- const GLOBAL_PIPE_MAX = 15000;
58
  const DISK_SLICE_DIR = "/tmp/dlk_slice";
59
  const DISK_MAX_BYTES = 24 * 1024 * 1024 * 1024;
60
  const DISK_FREE_MIN_BYTES = 4 * 1024 * 1024 * 1024;
@@ -513,7 +513,7 @@ function limited(ip) {
513
  const key = ipNet(ip) + ":" + slot;
514
  const n = (rateMap.get(key) || 0) + 1;
515
  rateMap.set(key, n);
516
- return n > 360;
517
  }
518
 
519
  function slotOver(key, max) {
@@ -526,16 +526,16 @@ function slotOver(key, max) {
526
 
527
  function mintLimited(ip) {
528
  const s = String(ip || "0");
529
- return slotOver("mint:" + s, 90) || slotOver("mintn:" + ipNet(s), 600);
530
  }
531
 
532
  function mintBlocked(ip) {
533
  const slot = Math.floor(Date.now() / 60000);
534
- return (rateMap.get("badm:" + String(ip || "0") + ":" + slot) || 0) > 20;
535
  }
536
 
537
  function mintFailed(ip) {
538
- return slotOver("badm:" + String(ip || "0"), 20);
539
  }
540
 
541
  function isBlogOrigin(o) {
@@ -851,7 +851,11 @@ function pickBot(preferIdx, skipIdx, msgId) {
851
  const hit = fileCache.get(id + ":" + b.idx);
852
  return hit && Date.now() - hit.ts < FILE_TTL;
853
  });
854
- if (hot.length) use = hot;
 
 
 
 
855
  }
856
  const min = Math.min(...use.map((b) => (b.tgLoad || 0)));
857
  if (preferIdx != null) {
@@ -1048,6 +1052,8 @@ async function fileFromMsg(bot, msgId) {
1048
  function fileFromAnyCache(msgId) {
1049
  const p = String(msgId) + ":";
1050
  const now = Date.now();
 
 
1051
  for (const [k, v] of fileCache) {
1052
  if (k.startsWith(p) && v && now - v.ts < FILE_TTL) {
1053
  v.ts = now;
@@ -1055,10 +1061,17 @@ function fileFromAnyCache(msgId) {
1055
  if (d > 1) v.duration = d;
1056
  const idx = Number(k.slice(p.length));
1057
  const bot = bots.find((b) => b && b.idx === idx);
1058
- if (bot && botUsable(bot)) return { bot, rec: v };
 
 
 
 
 
 
 
1059
  }
1060
  }
1061
- return null;
1062
  }
1063
 
1064
  async function fileFromPool(msgId, prefer, skipIdx) {
@@ -2214,10 +2227,11 @@ async function handleGenerate(req, res) {
2214
  return json(res, 400, { error: "bad_request" });
2215
  }
2216
  const rawAuth = String(req.headers.authorization || "").trim();
2217
- if (!rawAuth) {
2218
- const gk = path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]);
2219
- const ghit = genAnonCache.get(gk);
2220
- if (ghit && ghit.body && ghit.body.u && ghit.exp > Date.now() + 8000) {
 
2221
  const body = Object.assign({}, ghit.body);
2222
  const cd = durCache.get(String(msgId));
2223
  if (cd > 1) body.d = Math.floor(cd);
@@ -2297,9 +2311,8 @@ async function handleGenerate(req, res) {
2297
  e: exp
2298
  };
2299
  if (leadSec > 0.05) body.v = Math.round(leadSec * 1000) / 1000;
2300
- if (!rawAuth && lvl === "anonymous") {
2301
- genAnonCache.set(path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]), { body, exp: Date.now() + 180000, ts: Date.now() });
2302
- }
2303
  json(res, 200, body);
2304
  }
2305
 
@@ -2347,31 +2360,27 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2347
  }
2348
  const ip = clientIp(req);
2349
  const uaH = req.headers["user-agent"];
2350
- const netOk = ticketNetOk(ticket, ip);
2351
- const uaOk = ticketUaOk(ticket, uaH);
 
2352
  const vipUntil = Number(ticket.x) || 0;
2353
  if (ticketLvl(ticket) === "subscribed" && vipUntil > 0 && vipUntil <= Math.floor(Date.now() / 1000)) ticket.l = "registered";
2354
  const isVip = ticketLvl(ticket) === "subscribed";
2355
 
2356
- if (isVip) {
2357
- if (!netOk) {
2358
- res.writeHead(403);
2359
- return res.end();
2360
- }
2361
- let validBlog = false;
2362
- if (oH && isBlogOrigin(oH)) validBlog = true;
2363
- else if (rH) {
2364
- try { validBlog = isBlogOrigin(new URL(rH).origin); } catch { validBlog = false; }
2365
- }
2366
- if (!validBlog) {
2367
- res.writeHead(403);
2368
- return res.end();
2369
- }
2370
- } else {
2371
- if (!netOk) {
2372
- res.writeHead(403);
2373
- return res.end();
2374
- }
2375
  }
2376
  let held = false;
2377
  try {
 
41
  const HOT_TAIL = 8 * MB;
42
  const STREAM_WINDOW = 8 * MB;
43
  const PROBE_HEAD = 4 * MB;
44
+ const BOT_MAX = 60;
45
  const PIPE_MS = 30000;
46
  const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
47
+ const RANGE_LOOKAHEAD = 12;
48
+ const WARM_AHEAD = 12;
49
  const WARM_BUDGET_BYTES = 64 * MB;
50
  let warmBytesInFlight = 0;
51
  const WARM_BATCH = Math.max(1, Math.min(2, CPU_N));
52
+ const IP_STREAM_MAX = 25000;
53
+ const IP_TITLE_MAX = 10000;
54
+ const UID_STREAM_MAX = 2000;
55
+ const UID_TITLE_MAX = 1000;
56
+ const TITLE_PIPE_MAX = 50000;
57
+ const GLOBAL_PIPE_MAX = 50000;
58
  const DISK_SLICE_DIR = "/tmp/dlk_slice";
59
  const DISK_MAX_BYTES = 24 * 1024 * 1024 * 1024;
60
  const DISK_FREE_MIN_BYTES = 4 * 1024 * 1024 * 1024;
 
513
  const key = ipNet(ip) + ":" + slot;
514
  const n = (rateMap.get(key) || 0) + 1;
515
  rateMap.set(key, n);
516
+ return n > 50000;
517
  }
518
 
519
  function slotOver(key, max) {
 
526
 
527
  function mintLimited(ip) {
528
  const s = String(ip || "0");
529
+ return slotOver("mint:" + s, 25000) || slotOver("mintn:" + ipNet(s), 100000);
530
  }
531
 
532
  function mintBlocked(ip) {
533
  const slot = Math.floor(Date.now() / 60000);
534
+ return (rateMap.get("badm:" + String(ip || "0") + ":" + slot) || 0) > 2000;
535
  }
536
 
537
  function mintFailed(ip) {
538
+ return slotOver("badm:" + String(ip || "0"), 2000);
539
  }
540
 
541
  function isBlogOrigin(o) {
 
851
  const hit = fileCache.get(id + ":" + b.idx);
852
  return hit && Date.now() - hit.ts < FILE_TTL;
853
  });
854
+ if (hot.length) {
855
+ const hotMin = Math.min(...hot.map((b) => (b.tgLoad || 0)));
856
+ const allMin = Math.min(...use.map((b) => (b.tgLoad || 0)));
857
+ if (hotMin <= allMin + 2) use = hot;
858
+ }
859
  }
860
  const min = Math.min(...use.map((b) => (b.tgLoad || 0)));
861
  if (preferIdx != null) {
 
1052
  function fileFromAnyCache(msgId) {
1053
  const p = String(msgId) + ":";
1054
  const now = Date.now();
1055
+ let best = null;
1056
+ let minLoad = Infinity;
1057
  for (const [k, v] of fileCache) {
1058
  if (k.startsWith(p) && v && now - v.ts < FILE_TTL) {
1059
  v.ts = now;
 
1061
  if (d > 1) v.duration = d;
1062
  const idx = Number(k.slice(p.length));
1063
  const bot = bots.find((b) => b && b.idx === idx);
1064
+ if (bot && botUsable(bot)) {
1065
+ const load = bot.tgLoad || 0;
1066
+ if (load < minLoad) {
1067
+ minLoad = load;
1068
+ best = { bot, rec: v };
1069
+ if (load === 0) break;
1070
+ }
1071
+ }
1072
  }
1073
  }
1074
+ return best;
1075
  }
1076
 
1077
  async function fileFromPool(msgId, prefer, skipIdx) {
 
2227
  return json(res, 400, { error: "bad_request" });
2228
  }
2229
  const rawAuth = String(req.headers.authorization || "").trim();
2230
+ const authTag = rawAuth ? crypto.createHash("sha256").update(rawAuth).digest("hex").slice(0, 16) : "a";
2231
+ const gk = path + "|" + hash + "|" + authTag + "|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]);
2232
+ const ghit = genAnonCache.get(gk);
2233
+ if (ghit && ghit.body && ghit.body.u && ghit.exp > Date.now() + 8000) {
2234
+ if (!ghit.vipUntil || ghit.vipUntil > Math.floor(Date.now() / 1000) + 30) {
2235
  const body = Object.assign({}, ghit.body);
2236
  const cd = durCache.get(String(msgId));
2237
  if (cd > 1) body.d = Math.floor(cd);
 
2311
  e: exp
2312
  };
2313
  if (leadSec > 0.05) body.v = Math.round(leadSec * 1000) / 1000;
2314
+ const storeExp = Math.min(Date.now() + 180000, exp * 1000);
2315
+ genAnonCache.set(gk, { body, exp: storeExp, vipUntil, ts: Date.now() });
 
2316
  json(res, 200, body);
2317
  }
2318
 
 
2360
  }
2361
  const ip = clientIp(req);
2362
  const uaH = req.headers["user-agent"];
2363
+ const isAppleCore = /AppleCoreMedia|CoreMedia|QuickTime/i.test(String(uaH || ""));
2364
+ const netOk = isAppleCore || ticketNetOk(ticket, ip);
2365
+ const uaOk = isAppleCore || ticketUaOk(ticket, uaH);
2366
  const vipUntil = Number(ticket.x) || 0;
2367
  if (ticketLvl(ticket) === "subscribed" && vipUntil > 0 && vipUntil <= Math.floor(Date.now() / 1000)) ticket.l = "registered";
2368
  const isVip = ticketLvl(ticket) === "subscribed";
2369
 
2370
+ if (!netOk || !uaOk) {
2371
+ res.writeHead(403);
2372
+ return res.end();
2373
+ }
2374
+ let validBlog = false;
2375
+ if (oH && isBlogOrigin(oH)) validBlog = true;
2376
+ else if (rH) {
2377
+ try { validBlog = isBlogOrigin(new URL(rH).origin); } catch { validBlog = false; }
2378
+ } else if (isAppleCore) {
2379
+ validBlog = true;
2380
+ }
2381
+ if ((isVip || oH || rH) && !validBlog) {
2382
+ res.writeHead(403);
2383
+ return res.end();
 
 
 
 
 
2384
  }
2385
  let held = false;
2386
  try {