Enable safe concurrent SQLite reads and writes - #3
Merged
Merged
Conversation
Reconfigure the SQLite backend so multiple goroutines can read and write without hitting SQLITE_BUSY or busy-snapshot errors: - Build a URI DSN that applies journal_mode=WAL, busy_timeout=5000, foreign_keys=1, and synchronous=NORMAL on every pooled connection (the previous one-shot db.Exec only configured one pool member). - Force _txlock=immediate so every BeginTx acquires the writer lock up front, eliminating busy-snapshot in the read-then-write transactions used for collision resolution and file ops. - Bound the connection pool from CPU count; pin :memory: paths to a single connection since each new conn opens a separate in-memory DB. Add concurrent test coverage for the repository (per-conn pragma check, distinct concurrent writes, mixed reader/writer load, concurrent read-then-write transactions, expiration sweeper racing with writes) and the service layer (concurrent collision resolution producing unique resolved words, distinct concurrent writes via StoreEntry). Run go test with -race in CI and add a dedicated concurrency stress job that runs the concurrent suites with -count=5. Document the SQLite concurrency configuration and the steps to switch an existing deployment from PostgreSQL to SQLite.
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
NewSQLiteRepo) to applyjournal_mode=WAL,busy_timeout=5000,foreign_keys=1,synchronous=NORMAL, and_txlock=immediateon every pooled connection via a URI DSN — fixing instantSQLITE_BUSY, busy-snapshot in read-then-write transactions, and the latent FK-only-on-one-conn bug.:memory:paths to a single connection.StoreEntryon the same word — all succeed with unique resolved words; 32×10 distinct concurrent writes).go test -racein CI and add a dedicated concurrency-stress job (-count=5).Test plan
go vet ./...go build ./...go test -race -timeout 5m ./...go test -race -count=5 -run='Concurrent|PragmasOnEveryPoolConnection' ./internal/repository/...go test -race -count=5 -run='Concurrent' ./internal/service/...WORDSTORE_DATABASE_DRIVER=sqliteboot creates./data/word-store.db, runs migrations, and serves an MCP request.