Use single connection in pool to avoid simultaneous write lock issues

This commit is contained in:
DaneEveritt 2022-07-24 16:26:52 -04:00
parent 251f91a08e
commit 21cf66b2b4
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -7,7 +7,9 @@ import (
"github.com/pterodactyl/wings/internal/models" "github.com/pterodactyl/wings/internal/models"
"github.com/pterodactyl/wings/system" "github.com/pterodactyl/wings/system"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
"path/filepath" "path/filepath"
"time"
) )
var o system.AtomicBool var o system.AtomicBool
@ -20,11 +22,19 @@ func Initialize() error {
panic("database: attempt to initialize more than once during application lifecycle") panic("database: attempt to initialize more than once during application lifecycle")
} }
p := filepath.Join(config.Get().System.RootDirectory, "wings.db") p := filepath.Join(config.Get().System.RootDirectory, "wings.db")
instance, err := gorm.Open(sqlite.Open(p), &gorm.Config{}) instance, err := gorm.Open(sqlite.Open(p), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil { if err != nil {
return errors.Wrap(err, "database: could not open database file") return errors.Wrap(err, "database: could not open database file")
} }
db = instance db = instance
if sql, err := db.DB(); err != nil {
return errors.WithStack(err)
} else {
sql.SetMaxOpenConns(1)
sql.SetConnMaxLifetime(time.Hour)
}
if err := db.AutoMigrate(&models.Activity{}); err != nil { if err := db.AutoMigrate(&models.Activity{}); err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }