From d87d93b11490042afca7ae4abf902d048144daad Mon Sep 17 00:00:00 2001 From: ziggie Date: Wed, 11 Feb 2026 13:01:53 +0100 Subject: [PATCH] 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. --- lncfg/db.go | 2 +- sqldb/config.go | 18 ++++++++++++++++++ sqldb/sqlite.go | 4 ++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lncfg/db.go b/lncfg/db.go index 5bd4d2e19..2dec1bd63 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -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 diff --git a/sqldb/config.go b/sqldb/config.go index ebf386f54..a5e8122ab 100644 --- a/sqldb/config.go +++ b/sqldb/config.go @@ -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 { diff --git a/sqldb/sqlite.go b/sqldb/sqlite.go index ede2999ad..1ed26810d 100644 --- a/sqldb/sqlite.go +++ b/sqldb/sqlite.go @@ -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)