DUBLK 2000 Admin commited on
Commit
b3b0247
·
1 Parent(s): b74596d

Improve stream range resilience and bot error failover

Browse files
Files changed (1) hide show
  1. server.js +6 -8
server.js CHANGED
@@ -2032,11 +2032,7 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
2032
  await ready;
2033
  const primed = opened ? null : await primedP.catch(() => null);
2034
  if (!opened && (!primed || !primed.length)) {
2035
- if (!res.headersSent) {
2036
- res.writeHead(502);
2037
- res.end();
2038
- }
2039
- return;
2040
  }
2041
  if (!opened) {
2042
  res.writeHead(status, headers);
@@ -2429,9 +2425,10 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2429
  } catch (e) {
2430
  coolBot(bot, e);
2431
  if (isFileRef(e)) dropFile(msgId, bot);
2432
- if (!res.headersSent && (isFileRef(e) || isFlood(e))) {
2433
- const next = pickBot(undefined, bot.idx, msgId);
2434
- if (next) {
 
2435
  try {
2436
  rec = await fileFromMsg(next, msgId);
2437
  await pipeRange(next, rec, rng.start, rng.end, req, res, status, headers, msgId);
@@ -2439,6 +2436,7 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2439
  } catch (e2) {
2440
  coolBot(next, e2);
2441
  if (isFileRef(e2)) dropFile(msgId, next);
 
2442
  }
2443
  }
2444
  }
 
2032
  await ready;
2033
  const primed = opened ? null : await primedP.catch(() => null);
2034
  if (!opened && (!primed || !primed.length)) {
2035
+ throw new Error("prime_failed");
 
 
 
 
2036
  }
2037
  if (!opened) {
2038
  res.writeHead(status, headers);
 
2425
  } catch (e) {
2426
  coolBot(bot, e);
2427
  if (isFileRef(e)) dropFile(msgId, bot);
2428
+ if (!res.headersSent) {
2429
+ for (let attempt = 0; attempt < 3; attempt++) {
2430
+ const next = pickBot(undefined, bot.idx, msgId);
2431
+ if (!next || next.idx === bot.idx) break;
2432
  try {
2433
  rec = await fileFromMsg(next, msgId);
2434
  await pipeRange(next, rec, rng.start, rng.end, req, res, status, headers, msgId);
 
2436
  } catch (e2) {
2437
  coolBot(next, e2);
2438
  if (isFileRef(e2)) dropFile(msgId, next);
2439
+ bot = next;
2440
  }
2441
  }
2442
  }