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:
3 files changed
+21
-3
No files matched your search
+1
-1
@@ -207,7 +207,7 @@ func (db *DB) Init(ctx context.Context, dbPath string) error {
|
|||||||
sqlbase.Init(db.Postgres.MaxConnections)
|
sqlbase.Init(db.Postgres.MaxConnections)
|
||||||
|
|
||||||
case db.Backend == SqliteBackend:
|
case db.Backend == SqliteBackend:
|
||||||
sqlbase.Init(db.Sqlite.MaxConnections)
|
sqlbase.Init(db.Sqlite.MaxConns())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -32,6 +32,12 @@ type SqliteConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
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
|
// defaultBusyTimeoutMs is the default busy_timeout value in
|
||||||
// milliseconds, used when no BusyTimeout is configured.
|
// milliseconds, used when no BusyTimeout is configured.
|
||||||
defaultBusyTimeoutMs = 5000
|
defaultBusyTimeoutMs = 5000
|
||||||
@@ -47,6 +53,18 @@ func (s *SqliteConfig) busyTimeoutMs() int64 {
|
|||||||
return defaultBusyTimeoutMs
|
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.
|
// Validate checks that the SqliteConfig values are valid.
|
||||||
func (p *SqliteConfig) Validate() error {
|
func (p *SqliteConfig) Validate() error {
|
||||||
if err := p.QueryConfig.Validate(true); err != nil {
|
if err := p.QueryConfig.Validate(true); err != nil {
|
||||||
|
|||||||
+2
-2
@@ -136,8 +136,8 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
|
|||||||
err)
|
err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.SetMaxOpenConns(defaultMaxConns)
|
db.SetMaxOpenConns(cfg.MaxConns())
|
||||||
db.SetMaxIdleConns(defaultMaxConns)
|
db.SetMaxIdleConns(cfg.MaxConns())
|
||||||
db.SetConnMaxLifetime(connIdleLifetime)
|
db.SetConnMaxLifetime(connIdleLifetime)
|
||||||
queries := sqlc.New(db)
|
queries := sqlc.New(db)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user