Merge pull request #1118 from ayi21sui/fix/sqlite-conn-leak
fix: SQLite connection leak causing unbounded memory growth
This commit is contained in:
@@ -25,6 +25,11 @@ func MigrateDb() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if sqlDB, e := db.DB(); e == nil {
|
||||||
|
_ = sqlDB.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
tx := db.Begin()
|
tx := db.Begin()
|
||||||
defer func() {
|
defer func() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ func GetDb(exclude string) ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if sqlDB, e := backupDb.DB(); e == nil {
|
||||||
|
_ = sqlDB.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
defer os.Remove(dbPath)
|
defer os.Remove(dbPath)
|
||||||
|
|
||||||
err = backupDb.AutoMigrate(
|
err = backupDb.AutoMigrate(
|
||||||
@@ -218,7 +223,9 @@ func ImportDB(file multipart.File) error {
|
|||||||
return common.NewErrorf("Error checking db: %v", err)
|
return common.NewErrorf("Error checking db: %v", err)
|
||||||
}
|
}
|
||||||
newDb_db, _ := newDb.DB()
|
newDb_db, _ := newDb.DB()
|
||||||
|
if newDb_db != nil {
|
||||||
newDb_db.Close()
|
newDb_db.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Backup the current database for fallback
|
// Backup the current database for fallback
|
||||||
fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath())
|
fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath())
|
||||||
|
|||||||
+6
-2
@@ -55,7 +55,10 @@ func OpenDB(dbPath string) error {
|
|||||||
if strings.Contains(dbPath, "?") {
|
if strings.Contains(dbPath, "?") {
|
||||||
sep = "&"
|
sep = "&"
|
||||||
}
|
}
|
||||||
dsn := dbPath + sep + "_busy_timeout=10000&_journal_mode=WAL"
|
// _cache_size=-200 caps each connection's page cache at ~200 KiB
|
||||||
|
// (default is ~2 MiB), reducing memory amplification if a connection
|
||||||
|
// escapes the pool.
|
||||||
|
dsn := dbPath + sep + "_busy_timeout=10000&_journal_mode=WAL&_cache_size=-200"
|
||||||
db, err = gorm.Open(sqlite.Open(dsn), c)
|
db, err = gorm.Open(sqlite.Open(dsn), c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -66,8 +69,9 @@ func OpenDB(dbPath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sqlDB.SetMaxOpenConns(25)
|
sqlDB.SetMaxOpenConns(25)
|
||||||
sqlDB.SetMaxIdleConns(5)
|
sqlDB.SetMaxIdleConns(2)
|
||||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||||
|
sqlDB.SetConnMaxIdleTime(5 * time.Minute)
|
||||||
|
|
||||||
if config.IsDebug() {
|
if config.IsDebug() {
|
||||||
db = db.Debug()
|
db = db.Debug()
|
||||||
|
|||||||
+2
-1
@@ -83,7 +83,8 @@ func (s *StatsService) SaveStats(enableTraffic bool) error {
|
|||||||
if !enableTraffic {
|
if !enableTraffic {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return tx.Create(&stats).Error
|
err = tx.Create(&stats).Error
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsService) GetStats(resource string, tag string, limit int) ([]model.Stats, error) {
|
func (s *StatsService) GetStats(resource string, tag string, limit int) ([]model.Stats, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user