Optimize SQLite3 access. Mainly separate read access from write access.

Based on tips from https://archive.is/Xfjh6#selection-257.0-278.0
This commit is contained in:
Deluan 2024-05-21 17:19:41 -04:00
parent 68f03d0167
commit 55bff343cd
18 changed files with 156 additions and 64 deletions

View file

@ -4,6 +4,7 @@ import (
"database/sql"
"embed"
"fmt"
"runtime"
"github.com/mattn/go-sqlite3"
"github.com/navidrome/navidrome/conf"
@ -24,8 +25,36 @@ var embedMigrations embed.FS
const migrationsFolder = "migrations"
func Db() *sql.DB {
return singleton.GetInstance(func() *sql.DB {
type DB interface {
ReadDB() *sql.DB
WriteDB() *sql.DB
Close()
}
type db struct {
readDB *sql.DB
writeDB *sql.DB
}
func (d *db) ReadDB() *sql.DB {
return d.readDB
}
func (d *db) WriteDB() *sql.DB {
return d.writeDB
}
func (d *db) Close() {
if err := d.readDB.Close(); err != nil {
log.Error("Error closing read DB", err)
}
if err := d.writeDB.Close(); err != nil {
log.Error("Error closing write DB", err)
}
}
func Db() DB {
return singleton.GetInstance(func() *db {
sql.Register(Driver+"_custom", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
return conn.RegisterFunc("SEEDEDRAND", hasher.HashFunc(), false)
@ -38,21 +67,32 @@ func Db() *sql.DB {
conf.Server.DbPath = Path
}
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
instance, err := sql.Open(Driver+"_custom", Path)
rdb, err := sql.Open(Driver+"_custom", Path)
if err != nil {
panic(err)
}
return instance
rdb.SetMaxOpenConns(max(4, runtime.NumCPU()))
wdb, err := sql.Open(Driver+"_custom", Path)
if err != nil {
panic(err)
}
wdb.SetMaxOpenConns(1)
return &db{
readDB: rdb,
writeDB: wdb,
}
})
}
func Close() error {
func Close() {
log.Info("Closing Database")
return Db().Close()
Db().Close()
}
func Init() func() {
db := Db()
db := Db().WriteDB()
// Disable foreign_keys to allow re-creating tables in migrations
_, err := db.Exec("PRAGMA foreign_keys=off")
@ -82,11 +122,7 @@ func Init() func() {
log.Fatal("Failed to apply new migrations", err)
}
return func() {
if err := Close(); err != nil {
log.Error("Error closing DB", err)
}
}
return Close
}
type statusLogger struct{ numPending int }