diff options
| author | bndw <ben@bdw.to> | 2026-02-14 09:44:55 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-14 09:44:55 -0800 |
| commit | fe3708eaf495613cc6e2340b821795f25811d6ed (patch) | |
| tree | 061f4b1c2938919ab38e0bc840c34d7c35bad42f /internal/storage | |
| parent | 688548d4ac3293449a88913275f886fd2e103cdf (diff) | |
fix: optimize SQLite connection pooling for single-writer architecture
Remove misleading max_connections config option and properly configure
SQLite connection pooling in the storage layer.
Changes:
- Set MaxOpenConns(1) for optimal SQLite performance
- Set MaxIdleConns(1) to keep connection alive
- Set ConnMaxLifetime(0) to never close connection
- Remove max_connections and max_lifetime from DatabaseConfig
- Update docs to clarify SQLite's single-writer architecture
Rationale:
SQLite is an embedded database with a single-writer lock. Multiple
connections cause lock contention and reduce performance. WAL mode
allows concurrent reads from the same connection, making connection
pooling unnecessary and counterproductive.
This change makes the configuration clearer and ensures optimal
SQLite performance by using a single long-lived connection.
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/storage.go | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/internal/storage/storage.go b/internal/storage/storage.go index d00d7bf..9ef9956 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go | |||
| @@ -22,6 +22,12 @@ func New(dbPath string) (*Storage, error) { | |||
| 22 | return nil, fmt.Errorf("failed to open database: %w", err) | 22 | return nil, fmt.Errorf("failed to open database: %w", err) |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | // Configure connection pool for SQLite | ||
| 26 | // SQLite works best with a single connection due to single-writer lock | ||
| 27 | db.SetMaxOpenConns(1) // Single connection (SQLite is single-writer) | ||
| 28 | db.SetMaxIdleConns(1) // Keep connection alive | ||
| 29 | db.SetConnMaxLifetime(0) // Never close the connection | ||
| 30 | |||
| 25 | // Configure SQLite for optimal performance | 31 | // Configure SQLite for optimal performance |
| 26 | pragmas := []string{ | 32 | pragmas := []string{ |
| 27 | "PRAGMA journal_mode=WAL", // Write-Ahead Logging for concurrency | 33 | "PRAGMA journal_mode=WAL", // Write-Ahead Logging for concurrency |
