JOHN SILLVESTOR commited on
Commit
4dd933b
·
verified ·
1 Parent(s): 5ac4d32

Update Node HTTP service

Browse files
Files changed (1) hide show
  1. server.js +73 -31
server.js CHANGED
@@ -51,6 +51,7 @@ const bots = [];
51
  const fileCache = new Map();
52
  const inflightFile = new Map();
53
  const durCache = new Map();
 
54
  const lvlCache = new Map();
55
  const rateMap = new Map();
56
  const ipStreams = new Map();
@@ -345,16 +346,25 @@ function httpVideoType(mime, name) {
345
  return "video/mp4";
346
  }
347
 
348
- function pickBot(preferIdx, skipIdx) {
349
  if (!bots.length) return null;
350
  const pool = bots.filter((b) => botUsable(b) && b.idx !== skipIdx);
351
  if (!pool.length) return null;
352
- const min = Math.min(...pool.map((b) => b.load));
 
 
 
 
 
 
 
 
 
353
  if (preferIdx != null) {
354
- const sticky = pool.find((b) => b.idx === preferIdx && b.load <= min + 1);
355
  if (sticky) return sticky;
356
  }
357
- const even = pool.filter((b) => b.load === min);
358
  const bot = even[rr % even.length];
359
  rr += 1;
360
  return bot;
@@ -402,8 +412,11 @@ async function loadFile(bot, msgId) {
402
  const attrs = doc.attributes || [];
403
  for (const a of attrs) {
404
  if (a && a.className === "DocumentAttributeFilename" && a.fileName) name = a.fileName;
405
- if (a && Number(a.duration) > 0) duration = Number(a.duration);
 
406
  }
 
 
407
  const loc = new Api.InputDocumentFileLocation({
408
  id: doc.id,
409
  accessHash: doc.accessHash,
@@ -422,6 +435,7 @@ async function loadFile(bot, msgId) {
422
  doc,
423
  ts: Date.now()
424
  };
 
425
  fileCache.set(String(msgId) + ":" + bot.idx, rec);
426
  return rec;
427
  }
@@ -429,7 +443,12 @@ async function loadFile(bot, msgId) {
429
  async function fileFromMsg(bot, msgId) {
430
  const key = String(msgId) + ":" + bot.idx;
431
  const hit = fileCache.get(key);
432
- if (hit && Date.now() - hit.ts < FILE_TTL) return hit;
 
 
 
 
 
433
  let p = inflightFile.get(key);
434
  if (p) return p;
435
  p = loadFile(bot, msgId).finally(() => inflightFile.delete(key));
@@ -440,7 +459,7 @@ async function fileFromMsg(bot, msgId) {
440
  async function fileFromPool(msgId, prefer, skipIdx) {
441
  const seen = new Set();
442
  const order = [];
443
- const first = prefer && botUsable(prefer) && prefer.idx !== skipIdx ? prefer : pickBot(prefer && prefer.idx, skipIdx);
444
  if (first) order.push(first);
445
  for (const b of bots) {
446
  if (order.length >= 4) break;
@@ -482,31 +501,54 @@ function durationFromMp4(buf) {
482
  }
483
  }
484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485
  async function fillDuration(bot, rec, msgId) {
486
- const id = String(msgId || rec.id || "");
487
- if (id && durCache.has(id)) {
 
488
  rec.duration = durCache.get(id);
489
- return;
490
  }
491
- if (rec.duration > 0) {
492
- if (id) durCache.set(id, rec.duration);
493
- return;
494
  }
495
- if (!rec.size || !botUsable(bot) || bot.load >= 6) return;
496
- const n = Math.min(rec.size, 1024 * 1024);
497
- const start = rec.size - n;
498
- let acc = Buffer.alloc(0);
499
- try {
500
- for await (const c of rangeChunks(bot, rec, start, rec.size - 1, () => false)) {
501
- acc = Buffer.concat([acc, c]);
502
- rec.duration = durationFromMp4(acc);
503
- if (rec.duration > 0) {
504
- if (id) durCache.set(id, rec.duration);
505
- return;
506
- }
507
- if (acc.length >= n) return;
508
- }
509
- } catch {}
510
  }
511
 
512
  function parseRange(h, size) {
@@ -650,7 +692,7 @@ async function handleGenerate(req, res, url) {
650
  }
651
  let got;
652
  try {
653
- got = await fileFromPool(msgId, pickBot());
654
  } catch {
655
  return busyJson(res);
656
  }
@@ -694,7 +736,7 @@ async function handleStream(req, res, url, msgId) {
694
  }
695
  try {
696
  const prefer = Number.isInteger(ticket.b) ? ticket.b : undefined;
697
- let bot = pickBot(prefer);
698
  if (!bot) {
699
  res.setHeader("Retry-After", "1");
700
  res.writeHead(503);
@@ -743,7 +785,7 @@ async function handleStream(req, res, url, msgId) {
743
  coolBot(bot, e);
744
  if (isFileRef(e)) dropFile(msgId, bot);
745
  if (!res.headersSent && (isFileRef(e) || isFlood(e))) {
746
- const next = pickBot(undefined, bot.idx);
747
  if (next) {
748
  try {
749
  rec = await fileFromMsg(next, msgId);
 
51
  const fileCache = new Map();
52
  const inflightFile = new Map();
53
  const durCache = new Map();
54
+ const inflightDur = new Map();
55
  const lvlCache = new Map();
56
  const rateMap = new Map();
57
  const ipStreams = new Map();
 
346
  return "video/mp4";
347
  }
348
 
349
+ function pickBot(preferIdx, skipIdx, msgId) {
350
  if (!bots.length) return null;
351
  const pool = bots.filter((b) => botUsable(b) && b.idx !== skipIdx);
352
  if (!pool.length) return null;
353
+ let use = pool;
354
+ if (msgId != null) {
355
+ const id = String(msgId);
356
+ const hot = pool.filter((b) => {
357
+ const hit = fileCache.get(id + ":" + b.idx);
358
+ return hit && Date.now() - hit.ts < FILE_TTL;
359
+ });
360
+ if (hot.length) use = hot;
361
+ }
362
+ const min = Math.min(...use.map((b) => b.load));
363
  if (preferIdx != null) {
364
+ const sticky = use.find((b) => b.idx === preferIdx && b.load <= min + 1);
365
  if (sticky) return sticky;
366
  }
367
+ const even = use.filter((b) => b.load === min);
368
  const bot = even[rr % even.length];
369
  rr += 1;
370
  return bot;
 
412
  const attrs = doc.attributes || [];
413
  for (const a of attrs) {
414
  if (a && a.className === "DocumentAttributeFilename" && a.fileName) name = a.fileName;
415
+ const n = Number(a && a.duration);
416
+ if (Number.isFinite(n) && n > duration) duration = n;
417
  }
418
+ const cached = durCache.get(String(msgId));
419
+ if (cached > 1) duration = cached;
420
  const loc = new Api.InputDocumentFileLocation({
421
  id: doc.id,
422
  accessHash: doc.accessHash,
 
435
  doc,
436
  ts: Date.now()
437
  };
438
+ if (duration > 1) durCache.set(String(msgId), duration);
439
  fileCache.set(String(msgId) + ":" + bot.idx, rec);
440
  return rec;
441
  }
 
443
  async function fileFromMsg(bot, msgId) {
444
  const key = String(msgId) + ":" + bot.idx;
445
  const hit = fileCache.get(key);
446
+ if (hit && Date.now() - hit.ts < FILE_TTL) {
447
+ hit.ts = Date.now();
448
+ const d = durCache.get(String(msgId));
449
+ if (d > 1) hit.duration = d;
450
+ return hit;
451
+ }
452
  let p = inflightFile.get(key);
453
  if (p) return p;
454
  p = loadFile(bot, msgId).finally(() => inflightFile.delete(key));
 
459
  async function fileFromPool(msgId, prefer, skipIdx) {
460
  const seen = new Set();
461
  const order = [];
462
+ const first = prefer && botUsable(prefer) && prefer.idx !== skipIdx ? prefer : pickBot(prefer && prefer.idx, skipIdx, msgId);
463
  if (first) order.push(first);
464
  for (const b of bots) {
465
  if (order.length >= 4) break;
 
501
  }
502
  }
503
 
504
+ async function peekDuration(bot, rec) {
505
+ if (!bot || !rec || !rec.size) return 0;
506
+ const take = Math.min(rec.size, 262144);
507
+ async function readSpan(start, end) {
508
+ let acc = Buffer.alloc(0);
509
+ for await (const c of rangeChunks(bot, rec, start, end, () => acc.length >= take)) {
510
+ acc = Buffer.concat([acc, c]);
511
+ const d = durationFromMp4(acc);
512
+ if (d > 1) return d;
513
+ if (acc.length >= take) break;
514
+ }
515
+ return durationFromMp4(acc);
516
+ }
517
+ bot.load++;
518
+ try {
519
+ let d = await readSpan(rec.size - take, rec.size - 1);
520
+ if (d > 1) return d;
521
+ return await readSpan(0, take - 1);
522
+ } catch {
523
+ return 0;
524
+ } finally {
525
+ bot.load = Math.max(0, bot.load - 1);
526
+ }
527
+ }
528
+
529
  async function fillDuration(bot, rec, msgId) {
530
+ const id = String(msgId || "");
531
+ if (!id) return 0;
532
+ if (durCache.has(id)) {
533
  rec.duration = durCache.get(id);
534
+ return rec.duration;
535
  }
536
+ if (rec.duration > 1) {
537
+ durCache.set(id, rec.duration);
538
+ return rec.duration;
539
  }
540
+ let p = inflightDur.get(id);
541
+ if (!p) {
542
+ const worker = pickBot(bot && bot.idx, undefined, msgId) || bot;
543
+ p = peekDuration(worker, rec).finally(() => inflightDur.delete(id));
544
+ inflightDur.set(id, p);
545
+ }
546
+ const d = await p;
547
+ if (d > 1) {
548
+ rec.duration = d;
549
+ durCache.set(id, d);
550
+ }
551
+ return d;
 
 
 
552
  }
553
 
554
  function parseRange(h, size) {
 
692
  }
693
  let got;
694
  try {
695
+ got = await fileFromPool(msgId, pickBot(undefined, undefined, msgId));
696
  } catch {
697
  return busyJson(res);
698
  }
 
736
  }
737
  try {
738
  const prefer = Number.isInteger(ticket.b) ? ticket.b : undefined;
739
+ let bot = pickBot(prefer, undefined, msgId);
740
  if (!bot) {
741
  res.setHeader("Retry-After", "1");
742
  res.writeHead(503);
 
785
  coolBot(bot, e);
786
  if (isFileRef(e)) dropFile(msgId, bot);
787
  if (!res.headersSent && (isFileRef(e) || isFlood(e))) {
788
+ const next = pickBot(undefined, bot.idx, msgId);
789
  if (next) {
790
  try {
791
  rec = await fileFromMsg(next, msgId);