sqldb+lncfg: use SQLite-appropriate default for max connections

Both the sqldb and kvdb SQLite layers were not using a sensible default
for MaxConnections. The sqldb store used defaultMaxConns (25) which is
meant for Postgres, and the kvdb path passed 0 (unlimited) to
sqlbase.Init when unconfigured.

Add a MaxConns() method on SqliteConfig that returns the configured
value or defaults to 2, appropriate for SQLite's single-writer model.
Use it in both sqldb/sqlite.go and lncfg/db.go so both layers share
the same default.
This commit is contained in:
ziggie committed 2026-02-11 13:15:56 +01:00
1 parent a7d1a30cf2
commit d87d93b114
3 files changed
+21 -3

No files matched your search

+1 -1
View File
@@ -207,7 +207,7 @@ func (db *DB) Init(ctx context.Context, dbPath string) error {
sqlbase.Init(db.Postgres.MaxConnections)
case db.Backend == SqliteBackend:
sqlbase.Init(db.Sqlite.MaxConnections)
sqlbase.Init(db.Sqlite.MaxConns())
}
return nil
+18
View File
@@ -32,6 +32,12 @@ type SqliteConfig struct {
}
const (
// defaultSqliteMaxConns is the default number of maximum open
// connections for SQLite. SQLite only supports a single writer, so a
// low default reduces contention on the busy_timeout and limits
// resource usage, especially on mobile.
defaultSqliteMaxConns = 2
// defaultBusyTimeoutMs is the default busy_timeout value in
// milliseconds, used when no BusyTimeout is configured.
defaultBusyTimeoutMs = 5000
@@ -47,6 +53,18 @@ func (s *SqliteConfig) busyTimeoutMs() int64 {
return defaultBusyTimeoutMs
}
// MaxConns returns the effective maximum number of open connections. If
// MaxConnections is not set, it returns a default of 2. This low default is
// chosen because SQLite only supports a single writer, which helps reduce
// contention and resource usage.
func (s *SqliteConfig) MaxConns() int {
if s.MaxConnections > 0 {
return s.MaxConnections
}
return defaultSqliteMaxConns
}
// Validate checks that the SqliteConfig values are valid.
func (p *SqliteConfig) Validate() error {
if err := p.QueryConfig.Validate(true); err != nil {
+2 -2
View File
@@ -136,8 +136,8 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
err)
}
db.SetMaxOpenConns(defaultMaxConns)
db.SetMaxIdleConns(defaultMaxConns)
db.SetMaxOpenConns(cfg.MaxConns())
db.SetMaxIdleConns(cfg.MaxConns())
db.SetConnMaxLifetime(connIdleLifetime)
queries := sqlc.New(db)