JOHN SILLVESTOR commited on
Commit
1c4e059
·
verified ·
1 Parent(s): d1f2f6b

faster first-byte token and hot-head warm

Browse files
Files changed (1) hide show
  1. server.js +33 -8
server.js CHANGED
@@ -926,6 +926,26 @@ function hotFill(msgId, start, buf, size) {
926
  }
927
  }
928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
929
  async function warmHot(bot, rec, msgId) {
930
  const id = String(msgId);
931
  const hit = hotMap.get(id);
@@ -1084,14 +1104,17 @@ async function handleGenerate(req, res, url) {
1084
  const msgId = Number(m[1]);
1085
  let uid = null;
1086
  let lvl = "anonymous";
1087
- try {
1088
- const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
1089
- if (idp) {
1090
- uid = idp.sub;
1091
- lvl = await accessLevel(uid);
 
 
 
 
 
1092
  }
1093
- } catch {
1094
- lvl = uid ? "registered" : "anonymous";
1095
  }
1096
  let got;
1097
  try {
@@ -1102,9 +1125,11 @@ async function handleGenerate(req, res, url) {
1102
  const bot = got.bot;
1103
  const rec = got.rec;
1104
  if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
 
 
 
1105
  const cachedDur = durCache.get(String(msgId));
1106
  if (cachedDur > 1) rec.duration = cachedDur;
1107
- await withTime(warmHot(bot, rec, msgId), 800, 0);
1108
  const playMime = httpVideoType(rec.mime, rec.name);
1109
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
1110
  const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip) });
 
926
  }
927
  }
928
 
929
+ function hotHeadLen(msgId) {
930
+ const h = hotMap.get(String(msgId));
931
+ return h && h.head ? h.head.length : 0;
932
+ }
933
+
934
+ function waitHotHead(msgId, min, ms) {
935
+ const id = String(msgId);
936
+ const need = Math.max(1, min | 0);
937
+ const budget = Math.max(1, ms | 0);
938
+ const t0 = Date.now();
939
+ return new Promise((resolve) => {
940
+ const tick = () => {
941
+ const n = hotHeadLen(id);
942
+ if (n >= need || Date.now() - t0 >= budget) return resolve(n);
943
+ setTimeout(tick, 12);
944
+ };
945
+ tick();
946
+ });
947
+ }
948
+
949
  async function warmHot(bot, rec, msgId) {
950
  const id = String(msgId);
951
  const hit = hotMap.get(id);
 
1104
  const msgId = Number(m[1]);
1105
  let uid = null;
1106
  let lvl = "anonymous";
1107
+ const rawAuth = String(req.headers.authorization || "").trim();
1108
+ if (rawAuth) {
1109
+ try {
1110
+ const idp = await withTime(verifyIdToken(rawAuth), 4000, null);
1111
+ if (idp) {
1112
+ uid = idp.sub;
1113
+ lvl = await accessLevel(uid);
1114
+ }
1115
+ } catch {
1116
+ lvl = uid ? "registered" : "anonymous";
1117
  }
 
 
1118
  }
1119
  let got;
1120
  try {
 
1125
  const bot = got.bot;
1126
  const rec = got.rec;
1127
  if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
1128
+ warmHot(bot, rec, msgId);
1129
+ const wantHead = Math.min(262144, rec.size || 0);
1130
+ if (wantHead > 0) await waitHotHead(msgId, wantHead, 200);
1131
  const cachedDur = durCache.get(String(msgId));
1132
  if (cachedDur > 1) rec.duration = cachedDur;
 
1133
  const playMime = httpVideoType(rec.mime, rec.name);
1134
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
1135
  const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip) });