fix(monitor): serialize DB writes through a single drained writer #99
Reference in New Issue
Block a user
Delete Branch "fix/db-writer"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
Every check spawned
go e.db.Save*(...)with the error discarded — a fire-and-forget goroutine per log line, check, state change, and alert-health update:SaveLogran a full-table prune DELETE on every insert;SaveChecka COUNT + conditional prune on every check — the hot path amplified each write into several statements.Close()→ writes to a closing DB, silently swallowed.state_changeswas never pruned → unbounded growth.Fix
dbWritevalues (log / check / state-change / alert-health). Writes enqueue non-blocking; a saturated queue drops and notes it in the in-memory log instead of blocking the check loop. Write errors are logged, not discarded.SaveLog/SaveCheckbecome plain INSERTs.PruneLogs/PruneCheckHistory/PruneStateChangesrun on a 10-min timer inside the writer (keep-newest-N-per-site via a window function).state_changesis now bounded.Engine.Stop()cancels the engine context, then waits for the writer to drain every buffered write before returning.maincalls it before the deferred storeClose(), so no write races a closed DB.busy_timeout=5000+synchronous=NORMAL(+ WAL) applied through the DSN so every pooled connection inherits them (a post-open PRAGMA only touches one connection).:memory:test DBs left as-is.Tests
Writer drains on
Stop,Stopis idempotent, and the prune queries keep newest-N per site / N logs on real SQLite. Full suite green under-race; vet clean. Smoke-tested the real binary: WAL files created, clean SIGTERM shutdown (no hang).Phase 2 of the fresh-eyes first-cut backlog (builds on #98).
8f479635f9to8b39d4c1a1