Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

perf(storage): stop the file count holding the event loop for the whole store

Yudhi Armyndharis9679168c882f
diff --git a/src/common/storage/storage.service.spec.ts b/src/common/storage/storage.service.spec.ts
index 55536729f..8f048f1aa 100644
--- a/src/common/storage/storage.service.spec.ts
+++ b/src/common/storage/storage.service.spec.ts
@@ -343,4 +343,39 @@ describe('StorageService.createExportStream enumerates the whole store', () => {
expect(listFiles).not.toHaveBeenCalled();
expect(count).toBe(2); // the iterator's items are what was counted, not an unrelated walk
});
+
+ /**
+ * Removing the cap from the enumeration also removed the bound on the SIZE loop underneath it,
+ * which stat'ed every file synchronously. On a large store that holds the event loop for the whole
+ * walk: health checks, webhooks and every in-flight request wait behind a count.
+ *
+ * Measured, not asserted structurally. A single "did something run during the call?" flag is NOT
+ * discriminating here — the enumeration awaits before the stat loop begins, so that flag flips
+ * either way. Counting how many times the loop yields WHILE the call is pending does discriminate:
+ * measured at 1 tick for 2000 files with the synchronous loop, and it rises with the file count
+ * once each stat yields.
+ */
+ it('does not hold the event loop for the whole walk', async () => {
+ const { service, localPath } = makeLocalService();
+ fs.mkdirSync(localPath, { recursive: true });
+ const FILES = 2000;
+ for (let i = 0; i < FILES; i++) {
+ fs.writeFileSync(path.join(localPath, `f${i}.bin`), 'x');
+ }
+
+ let ticks = 0;
+ let finished = false;
+ const tick = (): void => {
+ if (finished) return;
+ ticks += 1;
+ setImmediate(tick);
+ };
+ setImmediate(tick);
+
+ const { count } = await service.getFileCount();
+ finished = true;
+
+ expect(count).toBe(FILES); // the walk really did the work being measured
+ expect(ticks).toBeGreaterThan(100);
+ }, 30000);
});
diff --git a/src/common/storage/storage.service.ts b/src/common/storage/storage.service.ts
index e87894d63..e556a6678 100644
--- a/src/common/storage/storage.service.ts
+++ b/src/common/storage/storage.service.ts
@@ -311,7 +311,8 @@ export class StorageService implements OnModuleDestroy {
if (s3Keys.has(file)) continue;
count += 1;
try {
- sizeBytes += fs.statSync(path.join(this.localPath, file)).size;
+ // Same reason as the local branch below: this walk is uncapped too, so the stat must yield.
+ sizeBytes += (await fs.promises.stat(path.join(this.localPath, file))).size;
} catch (error) {
this.logger.debug(`Failed to stat file: ${file}`, { error: String(error) });
}
@@ -327,7 +328,10 @@ export class StorageService implements OnModuleDestroy {
for (const file of files) {
try {
const fullPath = path.join(this.localPath, file);
- const stats = fs.statSync(fullPath);
+ // Awaited, not statSync: uncapping the walk above also uncapped this loop, and a synchronous
+ // stat per file holds the event loop for the whole store — health checks, webhooks and every
+ // in-flight request wait behind a count. Measured at one event-loop tick for 2000 files.
+ const stats = await fs.promises.stat(fullPath);
sizeBytes += stats.size;
} catch (error) {
this.logger.debug(`Failed to stat file: ${file}`, { error: String(error) });