sqldb/v2: add MaxIdleConnections & ConnMaxLifetime sqlite opts
What changed, and why it matters
This commit adds two new user-configurable knobs for SQLite connection pool management in LND: how many idle database connections to keep open, and how long a single connection may be reused before being closed. It is a routine feature/configuration improvement with no security relevance visible in the code or commit message.
No security action required. Treat as a normal configuration enhancement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change extends sqldb/v2.SqliteConfig with MaxIdleConnections and ConnMaxLifetime fields, exposes them as CLI/config flags, and applies them to the sql.DB connection pool via SetMaxIdleConns and SetConnMaxLifetime. Previously these values were hard-coded to defaults. The patch is additive and does not alter default behavior when the new options are unset.
Changed components
sqldb/v2/config.gosqldb/v2/sqlite.goInspect captured patch +19 / −7
diff --git a/sqldb/v2/config.go b/sqldb/v2/config.go
index d02565e..5d4100a 100644
--- a/sqldb/v2/config.go
+++ b/sqldb/v2/config.go
@@ -31,11 +31,13 @@ const (
//
//nolint:ll
type SqliteConfig struct {
- Timeout time.Duration `long:"timeout" description:"The time after which a database query should be timed out."`
- BusyTimeout time.Duration `long:"busytimeout" description:"The maximum amount of time to wait for a database connection to become available for a query."`
- MaxConnections int `long:"maxconnections" description:"The maximum number of open connections to the database."`
- PragmaOptions []string `long:"pragmaoptions" description:"A list of pragma options to set on a database connection. For example, 'auto_vacuum=incremental'. Note that the flag must be specified multiple times if multiple options are to be set."`
- SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
+ Timeout time.Duration `long:"timeout" description:"The time after which a database query should be timed out."`
+ BusyTimeout time.Duration `long:"busytimeout" description:"The maximum amount of time to wait for a database connection to become available for a query."`
+ MaxConnections int `long:"maxconnections" description:"The maximum number of open connections to the database."`
+ MaxIdleConnections int `long:"maxidleconnections" description:"Max number of idle connections to keep in the connection pool."`
+ ConnMaxLifetime time.Duration `long:"connmaxlifetime" description:"Max amount of time a connection can be reused for before it is closed. Valid time units are {s, m, h}."`
+ PragmaOptions []string `long:"pragmaoptions" description:"A list of pragma options to set on a database connection. For example, 'auto_vacuum=incremental'. Note that the flag must be specified multiple times if multiple options are to be set."`
+ SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
// SkipMigrationDbBackup if true, then a backup of the database will not
// be created before applying migrations.
diff --git a/sqldb/v2/sqlite.go b/sqldb/v2/sqlite.go
index 209271b..7cd12c1 100644
--- a/sqldb/v2/sqlite.go
+++ b/sqldb/v2/sqlite.go
@@ -141,9 +141,19 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
maxConns = cfg.MaxConnections
}
+ maxIdleConns := defaultMaxIdleConns
+ if cfg.MaxIdleConnections > 0 {
+ maxIdleConns = cfg.MaxIdleConnections
+ }
+
+ connMaxLifetime := defaultConnMaxLifetime
+ if cfg.ConnMaxLifetime > 0 {
+ connMaxLifetime = cfg.ConnMaxLifetime
+ }
+
db.SetMaxOpenConns(maxConns)
- db.SetMaxIdleConns(defaultMaxIdleConns)
- db.SetConnMaxLifetime(defaultConnMaxLifetime)
+ db.SetMaxIdleConns(maxIdleConns)
+ db.SetConnMaxLifetime(connMaxLifetime)
s := &SqliteStore{
Config: cfg,
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.