JOHN SILLVESTOR commited on
Commit
7b5d2cc
·
verified ·
1 Parent(s): 8b23522

Update Node HTTP service

Browse files
Files changed (1) hide show
  1. server.js +22 -11
server.js CHANGED
@@ -18,7 +18,7 @@ const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
  const HTTP_WINDOW = MB;
20
  const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
21
- const BOT_MAX = 12;
22
  const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
23
  const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
24
  const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
@@ -130,7 +130,7 @@ function limited(ip) {
130
  if (!k.endsWith(":" + slot) && !k.endsWith(":" + (slot - 1))) rateMap.delete(k);
131
  }
132
  }
133
- return n > 180;
134
  }
135
 
136
  async function googleCerts() {
@@ -223,14 +223,14 @@ function httpVideoType(mime, name) {
223
 
224
  function pickBot(preferIdx) {
225
  if (!bots.length) return null;
226
- if (preferIdx != null) {
227
- const sticky = bots.find((b) => b.idx === preferIdx && b.ready && b.load < BOT_MAX);
228
- if (sticky) return sticky;
229
- }
230
  const live = bots.filter((b) => b.ready && b.load < BOT_MAX);
231
  const pool = live.length ? live : bots.filter((b) => b.ready);
232
  if (!pool.length) return null;
233
  const min = Math.min(...pool.map((b) => b.load));
 
 
 
 
234
  const even = pool.filter((b) => b.load === min);
235
  const bot = even[rr % even.length];
236
  rr += 1;
@@ -244,8 +244,8 @@ async function startBot(token, idx) {
244
  retryDelay: 1500,
245
  autoReconnect: true,
246
  useWSS: true,
247
- maxConcurrentDownloads: 8,
248
- downloadPool: { poolSize: 4, workers: 8, requestDeadlineMs: 20000 }
249
  });
250
  client.setLogLevel("error");
251
  await client.start({ botAuthToken: token });
@@ -266,7 +266,7 @@ function dropFile(msgId, bot) {
266
  async function fileFromMsg(bot, msgId) {
267
  const key = String(msgId) + ":" + bot.idx;
268
  const hit = fileCache.get(key);
269
- if (hit && Date.now() - hit.ts < 8 * 60 * 1000) return hit;
270
  const msgs = await bot.client.getMessages(CHANNEL_ID, { ids: msgId });
271
  const msg = msgs && msgs[0];
272
  if (!msg || msg.deleted) throw new Error("missing");
@@ -426,6 +426,10 @@ async function* rangeChunks(bot, rec, start, end) {
426
  async function pipeRange(bot, rec, start, end, res, status, headers) {
427
  bot.load++;
428
  try {
 
 
 
 
429
  res.writeHead(status, headers);
430
  if (typeof res.flushHeaders === "function") res.flushHeaders();
431
  const gen = rangeChunks(bot, rec, start, end);
@@ -683,7 +687,8 @@ function handleHealth(res) {
683
  ok: true,
684
  role: ROLE,
685
  bots: bots.filter((b) => b.ready).length,
686
- load: bots.reduce((n, b) => n + b.load, 0)
 
687
  });
688
  }
689
 
@@ -753,7 +758,13 @@ async function main() {
753
  process.stderr.write("no bots online\n");
754
  process.exit(1);
755
  }
756
- http.createServer(onReq).listen(PORT, "0.0.0.0");
 
 
 
 
 
 
757
  }
758
 
759
  main().catch((e) => {
 
18
  const MB = 1024 * 1024;
19
  const HTTP_WINDOW = MB;
20
  const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
21
+ const BOT_MAX = 24;
22
  const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
23
  const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
24
  const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
 
130
  if (!k.endsWith(":" + slot) && !k.endsWith(":" + (slot - 1))) rateMap.delete(k);
131
  }
132
  }
133
+ return n > 800;
134
  }
135
 
136
  async function googleCerts() {
 
223
 
224
  function pickBot(preferIdx) {
225
  if (!bots.length) return null;
 
 
 
 
226
  const live = bots.filter((b) => b.ready && b.load < BOT_MAX);
227
  const pool = live.length ? live : bots.filter((b) => b.ready);
228
  if (!pool.length) return null;
229
  const min = Math.min(...pool.map((b) => b.load));
230
+ if (preferIdx != null) {
231
+ const sticky = pool.find((b) => b.idx === preferIdx && b.load <= min + 1);
232
+ if (sticky) return sticky;
233
+ }
234
  const even = pool.filter((b) => b.load === min);
235
  const bot = even[rr % even.length];
236
  rr += 1;
 
244
  retryDelay: 1500,
245
  autoReconnect: true,
246
  useWSS: true,
247
+ maxConcurrentDownloads: 16,
248
+ downloadPool: { poolSize: 6, workers: 8, requestDeadlineMs: 25000 }
249
  });
250
  client.setLogLevel("error");
251
  await client.start({ botAuthToken: token });
 
266
  async function fileFromMsg(bot, msgId) {
267
  const key = String(msgId) + ":" + bot.idx;
268
  const hit = fileCache.get(key);
269
+ if (hit && Date.now() - hit.ts < 15 * 60 * 1000) return hit;
270
  const msgs = await bot.client.getMessages(CHANNEL_ID, { ids: msgId });
271
  const msg = msgs && msgs[0];
272
  if (!msg || msg.deleted) throw new Error("missing");
 
426
  async function pipeRange(bot, rec, start, end, res, status, headers) {
427
  bot.load++;
428
  try {
429
+ if (res.socket) {
430
+ res.socket.setNoDelay(true);
431
+ res.socket.setKeepAlive(true, 15000);
432
+ }
433
  res.writeHead(status, headers);
434
  if (typeof res.flushHeaders === "function") res.flushHeaders();
435
  const gen = rangeChunks(bot, rec, start, end);
 
687
  ok: true,
688
  role: ROLE,
689
  bots: bots.filter((b) => b.ready).length,
690
+ load: bots.reduce((n, b) => n + b.load, 0),
691
+ cap: bots.filter((b) => b.ready).length * BOT_MAX
692
  });
693
  }
694
 
 
758
  process.stderr.write("no bots online\n");
759
  process.exit(1);
760
  }
761
+ const srv = http.createServer({
762
+ keepAlive: true,
763
+ keepAliveTimeout: 65000,
764
+ headersTimeout: 70000,
765
+ requestTimeout: 0
766
+ }, onReq);
767
+ srv.listen(PORT, "0.0.0.0");
768
  }
769
 
770
  main().catch((e) => {