Files
Burrowdev aa455f52d0 perf: save only changed accounts, cache missing assets, lighter logging and backup
Ping on prod jumped to seconds because node's main thread was busy most
of every second.

- accounts.save(account, ...) names the changed accounts, which are
  written within a second; the rest are looked at a few at a time
  (10 ms a save for those in play, 5 ms for the others). One save under
  prod-like load: 289 ms before, 16 ms after.
- an asset the CDN does not have gets a cacheable 404 and is logged once
- the proxy's own connections no longer count against the web cap, which
  had node reset requests nginx had already let in
- a repeated client error is written once in ten minutes; the panel's
  log buffer trims in batches
- the daily backup uses sqlite's online backup off the main thread
2026-10-17 11:57:40 +03:00

37 lines
1.3 KiB
JavaScript

"use strict";
// keeps the most recent lines the server printed, for the admin panel's log
// view. requiring this module is what starts the capture.
const MAX_LINES = 5000;
const MAX_LINE_LENGTH = 2000;
const TRIM_BATCH = 1000;
const lines = []; // {id, time, text}
let nextId = 1;
const capture = original => (...args) => {
original(...args);
const text = args.map(arg => (typeof arg === "string" ? arg : String(arg && arg.stack ? arg.stack : JSON.stringify(arg)))).join(" ");
for (const line of text.split("\n")) {
lines.push({id: nextId++, time: Date.now(), text: line.slice(0, MAX_LINE_LENGTH)});
}
// trimmed a batch at a time: trimming a line at a time moved all the
// others along for every line printed.
if (lines.length > MAX_LINES + TRIM_BATCH) {
lines.splice(0, lines.length - MAX_LINES);
}
};
console.log = capture(console.log.bind(console));
console.error = capture(console.error.bind(console));
// lines newer than id `after` whose text contains `filter` (any case), newest
// `limit` of them, oldest first.
const recent = (after = 0, filter = "", limit = 500) => {
const wanted = filter.toLowerCase();
return lines.filter(line => line.id > after && (!wanted || line.text.toLowerCase().includes(wanted))).slice(-limit);
};
module.exports = {recent, count: () => nextId - 1};