Add patch: ap_followers.countDocuments() → estimatedDocumentCount()
Deploy Indiekit Server / deploy (push) Successful in 1m44s

syndicator.js uses countDocuments() with no filter for a log statement
("Sending X to N followers"). This runs a full aggregate scan — slow
under load (2.7s seen in profiling). estimatedDocumentCount() reads
collection metadata: O(1), no lock contention, accurate enough for logging.
This commit is contained in:
Sven
2026-05-03 12:24:45 +02:00
parent fae9e62af5
commit a04a8976e4
@@ -0,0 +1,52 @@
/**
* Patch: replace ap_followers.countDocuments() with estimatedDocumentCount()
* in syndicator.js log statement.
*
* countDocuments() runs an aggregate({$match:{}, $group:{n:$sum:1}}) scan.
* For a logging call this is unnecessary — estimatedDocumentCount() reads
* collection metadata (O(1), no lock contention).
*/
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-activitypub/lib/syndicator.js",
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-activitypub/lib/syndicator.js",
];
const MARKER = "// [patch] followers-count-estimate";
const OLD = ` // Count followers for logging
const followerCount =
await plugin._collections.ap_followers.countDocuments();`;
const NEW = ` // Count followers for logging
const followerCount =
await plugin._collections.ap_followers.estimatedDocumentCount(); ${MARKER}`;
async function exists(p) {
try { await access(p); return true; } catch { return false; }
}
let patched = false;
for (const filePath of candidates) {
if (!(await exists(filePath))) continue;
const src = await readFile(filePath, "utf8");
if (src.includes(MARKER)) {
console.log(`[postinstall] patch-ap-followers-count-estimate: already applied in ${filePath}`);
patched = true;
break;
}
if (!src.includes(OLD)) {
console.log(`[postinstall] patch-ap-followers-count-estimate: target snippet not found in ${filePath}`);
continue;
}
await writeFile(filePath, src.replace(OLD, NEW), "utf8");
console.log(`[postinstall] patch-ap-followers-count-estimate: applied to ${filePath}`);
patched = true;
break;
}
if (!patched) {
console.log("[postinstall] patch-ap-followers-count-estimate: no target file found");
}