Skip to content

Commit 26bdd83

Browse files
authored
docs: LRU eviction resets budgets; createSlowQueryDetector mutates sinks (#25)
- README + budget.md: eviction clears per-requestId counters; same id can violate again after re-insertion - README: document sinks array mutation when LoggerSink is appended - GitHub Pages: short footnote mirroring the above Fixes #17 Fixes #18
1 parent 7551d46 commit 26bdd83

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Why not benchmark only against DB in Docker?
186186
- **`wrapTaggedTemplate`** — tagged template `(strings, ...values) => Promise<unknown>` (e.g. **postgres.js** `sql`, same literal shape as Prisma `$queryRaw`).
187187
- **`extractQueryInfo`** — build `$1…$n` SQL + params from a `TemplateStringsArray` if you wire a custom executor.
188188

189+
**`createSlowQueryDetector`** appends a default **`LoggerSink`** with **`Array#push`** when your `sinks` array does not already include one, so the array you pass is **mutated**. Pass an array you own, or copy first (`sinks: [...existing]`), if immutability matters.
190+
189191
### postgres.js (tagged template + request scope)
190192

191193
Use **`runWithDbContext`** so each HTTP request (or job) gets a stable **`requestId`**; **`createSlowQueryDetector`** defaults `contextProvider` to **`getDbContext()`**, so you usually do **not** pass `contextProvider` unless you merge ALS with your own source.
@@ -249,6 +251,7 @@ Set `requestBudget.maxQueries` and/or `requestBudget.maxTotalDurationMs` to catc
249251

250252
- **Successful and failed** `executeQuery` completions both increment the budget (every round-trip attempt counts).
251253
- The first time a limit is exceeded for a `requestId`, sinks receive one **`db.request.budget`** event (`LoggerSink`**warn**). A second violation for the same id is only possible after that id falls out of the LRU (e.g. many concurrent requests with unique ids).
254+
- **LRU eviction resets counters:** when a `requestId` is evicted, its budget state is dropped. If that same id string appears again later, totals start from zero and another **`db.request.budget`** can fire for a new burst.
252255
- **`requestBudget.maxTrackedRequests`** bounds memory (default **5000**); non-finite or `< 1` values are normalized.
253256

254257
**Custom sinks:** `IEventSink.handle` receives **`DetectorEvent`** (`QueryEvent | RequestBudgetViolationEvent`). Branch on `event.event === "db.request.budget"` before assuming `sql` / `subtype` exist.

docs/budget.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Behavior (see [README § Request budgets](../README.md#request-budgets-per-reque
2222

2323
1. Budgets apply only when **`requestId`** is set on context (e.g. via `runWithDbContext`). With **`createSlowQueryDetector`**, the default `contextProvider` reads that context; custom detectors must supply one (see [README](../README.md#request-budgets-per-requestid)).
2424
2. Each query completion increments **count** and adds **duration** to that `requestId`.
25-
3. The **first** time either limit is exceeded, sinks receive **one** `RequestBudgetViolationEvent` (`event: "db.request.budget"`). Further violations for the same id only happen after that id is evicted from the LRU map.
25+
3. The **first** time either limit is exceeded, sinks receive **one** `RequestBudgetViolationEvent` (`event: "db.request.budget"`). Further violations for the same id only happen after that id is evicted from the LRU map. **Eviction removes the entry:** the same `requestId` string seen again later gets **fresh counters**, so another `db.request.budget` is possible after a subsequent burst.
2626
4. **`LoggerSink`** logs budget events at **warn** level (see `LoggerSink` in source).
2727

2828
This design targets **query storms**: many fast queries that never cross a single-query `warnThresholdMs`, but still hurt latency, CPU, and pool usage.

docs/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,10 @@ <h3>Explain the worst</h3>
646646
</div>
647647
</section>
648648

649+
<p class="footnote wrap">
650+
Request budgets: LRU eviction resets per-<code>requestId</code> counters (same id string can violate again after re-insertion).
651+
<code>createSlowQueryDetector</code> may <code>push</code> onto your <code>sinks</code> array — see README.
652+
</p>
649653
<p class="footnote wrap">
650654
MIT licensed ·
651655
<a href="https://github.com/oleg-koval/slow-query-detector">oleg-koval/slow-query-detector</a>

0 commit comments

Comments
 (0)