Skip to content

Commit c9bd021

Browse files
committed
added connection pooling
1 parent 2f31a7f commit c9bd021

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/common/database.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gorm.io/driver/sqlite"
99
"gorm.io/gorm"
1010
"path/filepath"
11+
"time"
1112
)
1213

1314
func InitDB() (*gorm.DB, *models.BroadcastChannel) {
@@ -25,6 +26,16 @@ func InitDB() (*gorm.DB, *models.BroadcastChannel) {
2526
log.Fatal().Err(err).Msg("failed to connect to database")
2627
}
2728

29+
if EnablePostgres.GetBool() {
30+
sqlDB, err := db.DB()
31+
if err != nil {
32+
log.Fatal().Err(err).Msg("failed to connect to database")
33+
}
34+
sqlDB.SetMaxIdleConns(10) // SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
35+
sqlDB.SetMaxOpenConns(100) // SetMaxOpenConns sets the maximum number of open connections to the database.
36+
sqlDB.SetConnMaxLifetime(time.Hour) // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
37+
}
38+
2839
err = db.AutoMigrate(&models.Job{})
2940
if err != nil {
3041
log.Fatal().Err(err).Msgf("failed to migrate database")

0 commit comments

Comments
 (0)