Files
pearcord-platform/poll-db-read-budget.js
T
Raven ScottandCursor 96f5893aad Complete Phase 409 platform perf, integrity, and mesh hardening.
Add adaptive poll helpers, discovery frame dedupe, poll DB read budget,
view integrity (draft prune, pin fallback, cross-ref audit, prefs checksum),
mesh quiet mode, topic merge counters, and discovery local-only guard.

Co-authored-by: Cursor <[email protected]>
2026-06-02 16:41:37 -04:00

41 lines
1016 B
JavaScript

'use strict'
const DEFAULT_BUDGET_PER_SEC = Math.max(
5,
Number(process.env.PEARCORD_POLL_DB_READ_BUDGET_PER_SEC) || 120
)
const WINDOW_MS = 1000
function createPollDbReadBudget (budgetPerSec = DEFAULT_BUDGET_PER_SEC) {
let windowStart = Date.now()
let count = 0
let warned = 0
let lastWarnAt = 0
return {
record (n = 1, { log } = {}) {
const now = Date.now()
if (now - windowStart >= WINDOW_MS) {
windowStart = now
count = 0
}
count += Math.max(0, Number(n) || 0)
if (count > budgetPerSec && now - lastWarnAt > WINDOW_MS) {
warned += 1
lastWarnAt = now
log?.warn?.('poll-path db read budget exceeded', {
count,
budgetPerSec,
windowMs: WINDOW_MS
})
}
return { count, budgetPerSec, exceeded: count > budgetPerSec }
},
stats () {
return { count, budgetPerSec, warned, windowStart }
}
}
}
module.exports = { createPollDbReadBudget, DEFAULT_BUDGET_PER_SEC }