mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 16:26:38 +00:00
[management] update db connection lifecycle configuration (#4740)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
@@ -273,15 +274,21 @@ func configureConnectionPool(db *gorm.DB, storeEngine types.Engine) (*gorm.DB, e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if storeEngine == types.SqliteStoreEngine {
|
conns, err := strconv.Atoi(os.Getenv(sqlMaxOpenConnsEnv))
|
||||||
sqlDB.SetMaxOpenConns(1)
|
if err != nil {
|
||||||
} else {
|
conns = runtime.NumCPU()
|
||||||
conns, err := strconv.Atoi(os.Getenv(sqlMaxOpenConnsEnv))
|
|
||||||
if err != nil {
|
|
||||||
conns = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
sqlDB.SetMaxOpenConns(conns)
|
|
||||||
}
|
}
|
||||||
|
if storeEngine == types.SqliteStoreEngine {
|
||||||
|
conns = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlDB.SetMaxOpenConns(conns)
|
||||||
|
sqlDB.SetMaxIdleConns(conns)
|
||||||
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||||
|
sqlDB.SetConnMaxIdleTime(3 * time.Minute)
|
||||||
|
|
||||||
|
log.Infof("Set max open db connections to %d, max idle to %d, max lifetime to %v, max idle time to %v",
|
||||||
|
conns, conns, time.Hour, 3*time.Minute)
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,8 +89,12 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine types.Engine, met
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql.SetMaxOpenConns(conns)
|
sql.SetMaxOpenConns(conns)
|
||||||
|
sql.SetMaxIdleConns(conns)
|
||||||
|
sql.SetConnMaxLifetime(time.Hour)
|
||||||
|
sql.SetConnMaxIdleTime(3 * time.Minute)
|
||||||
|
|
||||||
log.WithContext(ctx).Infof("Set max open db connections to %d", conns)
|
log.WithContext(ctx).Infof("Set max open db connections to %d, max idle to %d, max lifetime to %v, max idle time to %v",
|
||||||
|
conns, conns, time.Hour, 3*time.Minute)
|
||||||
|
|
||||||
if skipMigration {
|
if skipMigration {
|
||||||
log.WithContext(ctx).Infof("skipping migration")
|
log.WithContext(ctx).Infof("skipping migration")
|
||||||
|
|||||||
Reference in New Issue
Block a user