From 2dd146183b1c1ce270d790fe70ca800da4bc5c36 Mon Sep 17 00:00:00 2001 From: Kieran Klukas Date: Thu, 11 Jun 2026 14:10:12 -0400 Subject: [PATCH] feat: defer analytics flush to avoid blocking request responses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the write buffer fills during high load, the SQLite transaction now runs on the next tick instead of inline. This prevents analytics writes from adding latency to the current request's response path. 💘 Generated with Crush Assisted-by: Crush:qwen3.7-max --- src/lib/analytics-queries.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/analytics-queries.ts b/src/lib/analytics-queries.ts index bb4a2a8..bab3174 100644 --- a/src/lib/analytics-queries.ts +++ b/src/lib/analytics-queries.ts @@ -196,7 +196,12 @@ export class AnalyticsQueryService { }); if (this.writeBuffer.length >= this.MAX_BUFFER_SIZE) { - this.flushWriteBuffer(); + // Defer flush so it doesn't block the current request's response + if (!this.flushTimer) { + this.flushTimer = setTimeout(() => { + this.flushWriteBuffer(); + }, 0); + } } else if (!this.flushTimer) { this.flushTimer = setTimeout(() => { this.flushWriteBuffer(); -- 2.51.2