JOHN SILLVESTOR commited on
Commit
8320f59
·
verified ·
1 Parent(s): f83ae5a

Serve finished byte windows so playback reaches the limit

Browse files
Files changed (1) hide show
  1. server.js +24 -8
server.js CHANGED
@@ -23,12 +23,12 @@ const MB = 1024 * 1024;
23
  const CHUNK_SIZE = 1024 * 1024;
24
  const HOT_HEAD = 16 * MB;
25
  const HOT_TAIL = 16 * MB;
26
- const STREAM_WINDOW = 128 * 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 = 8;
32
  const WARM_AHEAD = 48;
33
  const IP_STREAM_MAX = 5000;
34
  const IP_TITLE_MAX = 2500;
@@ -1212,7 +1212,7 @@ function parseRange(h, size) {
1212
  start = Number(m[1]);
1213
  if (m[2] === "") {
1214
  if (start === 0) end = firstEnd;
1215
- else end = size - 1;
1216
  } else {
1217
  end = Number(m[2]);
1218
  }
@@ -1240,7 +1240,8 @@ function mediaHeaders(type, msgId, size, hash, rng) {
1240
 
1241
  function pipeBudget(start, end) {
1242
  const n = Math.max(1, end - start + 1);
1243
- return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (64 * 1024)) * 1000));
 
1244
  }
1245
 
1246
 
@@ -1895,8 +1896,8 @@ function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
1895
  } catch { }
1896
  jobs.push(off);
1897
  }
1898
- for (let i = 0; i < jobs.length; i += 4) {
1899
- const batch = jobs.slice(i, i + 4);
1900
  await Promise.all(batch.map((off) => fetchSliceChunk(bot, rec, msgId, off).catch(() => null)));
1901
  }
1902
  } catch { }
@@ -2028,19 +2029,33 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
2028
  try { res.socket.setTimeout(0); } catch { }
2029
  }
2030
  if (pipeDead(req, res)) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
2031
  res.writeHead(status, headers);
2032
  if (typeof res.flushHeaders === "function") res.flushHeaders();
 
2033
  const budget = pipeBudget(start, end);
2034
- const stop = () => clientClosed || pipeDead(req, res) || Date.now() - t0 > budget;
2035
  let pos = start;
2036
  const prefix = hotPrefix(msgId, pos, end);
2037
  if (prefix && prefix.length) {
2038
  const ok = res.write(prefix);
 
2039
  pos += prefix.length;
2040
  if (!ok) await waitDrain(req, res);
2041
  }
2042
  if (pos > end || stop()) return;
2043
- kickWarmAhead(bot, rec, msgId, pos, rec.size);
2044
  const gen = rangeChunks(bot, rec, pos, end, stop, msgId);
2045
  let at = pos;
2046
  for await (const buf of gen) {
@@ -2048,6 +2063,7 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
2048
  hotFill(msgId, at, buf, rec.size);
2049
  at += buf.length;
2050
  const ok = res.write(buf);
 
2051
  if (!ok) await waitDrain(req, res);
2052
  }
2053
  } finally {
 
23
  const CHUNK_SIZE = 1024 * 1024;
24
  const HOT_HEAD = 16 * MB;
25
  const HOT_TAIL = 16 * MB;
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 = 12;
32
  const WARM_AHEAD = 48;
33
  const IP_STREAM_MAX = 5000;
34
  const IP_TITLE_MAX = 2500;
 
1212
  start = Number(m[1]);
1213
  if (m[2] === "") {
1214
  if (start === 0) end = firstEnd;
1215
+ else end = Math.min(size - 1, start + STREAM_WINDOW - 1);
1216
  } else {
1217
  end = Number(m[2]);
1218
  }
 
1240
 
1241
  function pipeBudget(start, end) {
1242
  const n = Math.max(1, end - start + 1);
1243
+ const ms = Math.ceil(n / (32 * 1024)) * 1000;
1244
+ return Math.min(PIPE_MAX_MS, Math.max(45000, ms));
1245
  }
1246
 
1247
 
 
1896
  } catch { }
1897
  jobs.push(off);
1898
  }
1899
+ for (let i = 0; i < jobs.length; i += 6) {
1900
+ const batch = jobs.slice(i, i + 6);
1901
  await Promise.all(batch.map((off) => fetchSliceChunk(bot, rec, msgId, off).catch(() => null)));
1902
  }
1903
  } catch { }
 
2029
  try { res.socket.setTimeout(0); } catch { }
2030
  }
2031
  if (pipeDead(req, res)) return;
2032
+ const primedPrefix = hotPrefix(msgId, start, end);
2033
+ if (!(primedPrefix && primedPrefix.length)) {
2034
+ const primed = await fetchSliceChunk(bot, rec, msgId, Math.floor(start / CHUNK_SIZE) * CHUNK_SIZE);
2035
+ if (!primed || !primed.length) {
2036
+ if (!res.headersSent) {
2037
+ res.writeHead(502);
2038
+ res.end();
2039
+ }
2040
+ return;
2041
+ }
2042
+ }
2043
+ kickWarmAhead(bot, rec, msgId, start, rec.size || (end + 1));
2044
+ if (pipeDead(req, res)) return;
2045
  res.writeHead(status, headers);
2046
  if (typeof res.flushHeaders === "function") res.flushHeaders();
2047
+ let lastByte = Date.now();
2048
  const budget = pipeBudget(start, end);
2049
+ const stop = () => clientClosed || pipeDead(req, res) || Date.now() - lastByte > 22000 || Date.now() - t0 > budget;
2050
  let pos = start;
2051
  const prefix = hotPrefix(msgId, pos, end);
2052
  if (prefix && prefix.length) {
2053
  const ok = res.write(prefix);
2054
+ lastByte = Date.now();
2055
  pos += prefix.length;
2056
  if (!ok) await waitDrain(req, res);
2057
  }
2058
  if (pos > end || stop()) return;
 
2059
  const gen = rangeChunks(bot, rec, pos, end, stop, msgId);
2060
  let at = pos;
2061
  for await (const buf of gen) {
 
2063
  hotFill(msgId, at, buf, rec.size);
2064
  at += buf.length;
2065
  const ok = res.write(buf);
2066
+ lastByte = Date.now();
2067
  if (!ok) await waitDrain(req, res);
2068
  }
2069
  } finally {