feat(storage): SQLite read-pool to alleviate writer contention#88
Open
feat(storage): SQLite read-pool to alleviate writer contention#88
Conversation
SQLite hardcodes MaxOpen=1 (correct: SQLite has a single-writer model), but
the Go driver pool serializes ALL operations through that one connection.
API/MCP read handlers were queueing behind in-flight writes/retention/VACUUM,
producing slow tail latency under load.
This change opens a separate *gorm.DB against the same file with MaxOpen=N
(default 4, range [1,32]) and PRAGMA query_only=ON. Read-only Repository
methods route through a new r.reader() accessor; writes (BatchCreate*,
Update*, Purge*, VacuumDB, FTS5/DDL) stay on r.db. WAL mode is already
enabled, so each read connection has its own snapshot and reads do not
block writers.
- New: internal/storage/factory.go: NewSQLiteReadPool(dsn, maxOpen).
- internal/storage/repository.go: readPool field, reader() accessor,
sqliteReadPoolSizeFromEnv() parser, NewRepository wiring (SQLite-only,
graceful fallback on factory failure), Close() now closes both pools.
- internal/storage/{log,trace,metrics,graph,archive}_repo.go: every
read-only path routed through reader(); writes unchanged.
- New: internal/storage/sqlite_read_pool_test.go: env parsing, query_only
INSERT-rejection, reader() identity fallback, concurrent reads + writes
with p99 latency assertion (<200ms; observed 22.85ms).
- CLAUDE.md: DB_SQLITE_READ_POOL_SIZE documented.
Non-SQLite drivers ignore the setting; reader() falls back to the writer
when readPool is nil. NewRepositoryFromDB does not open a read pool — its
doc now notes this so test authors aren't surprised by SQLite contention.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
*gorm.DBagainst the SQLite file withMaxOpen=N(default 4, range [1,32]) andPRAGMA query_only=ON. Read handlers route through a newr.reader()accessor; writes stay onr.db(which keepsMaxOpen=1per SQLite's single-writer model).DB_SQLITE_READ_POOL_SIZE(default 4;0/off/false/nodisables and falls back to writer). Non-SQLite drivers ignore the flag.Changes
internal/storage/factory.go::NewSQLiteReadPool(dsn, maxOpen)factory.internal/storage/repository.go:readPoolfield +reader()accessor, env parser,NewRepositorywiring (SQLite-only, graceful fallback on factory failure),Close()closes both pools (read-pool errors logged viaslog.Warn).internal/storage/{log,trace,metrics,graph,archive}_repo.go: every read-only path routed throughreader(); writes (BatchCreate*, Update*, Purge*, Vacuum, FTS5/DDL) unchanged.internal/storage/sqlite_read_pool_test.go: env parsing,query_only=ONINSERT-rejection,reader()identity fallback, concurrent reads + writes with p99 latency assertion.CLAUDE.md:DB_SQLITE_READ_POOL_SIZEdocumented.Test plan
🤖 Generated with Claude Code