Skip to content

Commit 353ec3c

Browse files
update db connection lifecycle configuration
1 parent a2313a5 commit 353ec3c

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

management/server/activity/store/sql_store.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"runtime"
99
"strconv"
10+
"time"
1011

1112
log "github.com/sirupsen/logrus"
1213
"gorm.io/driver/postgres"
@@ -273,15 +274,21 @@ func configureConnectionPool(db *gorm.DB, storeEngine types.Engine) (*gorm.DB, e
273274
return nil, err
274275
}
275276

277+
conns, err := strconv.Atoi(os.Getenv(sqlMaxOpenConnsEnv))
278+
if err != nil {
279+
conns = runtime.NumCPU()
280+
}
276281
if storeEngine == types.SqliteStoreEngine {
277-
sqlDB.SetMaxOpenConns(1)
278-
} else {
279-
conns, err := strconv.Atoi(os.Getenv(sqlMaxOpenConnsEnv))
280-
if err != nil {
281-
conns = runtime.NumCPU()
282-
}
283-
sqlDB.SetMaxOpenConns(conns)
282+
conns = 1
284283
}
285284

285+
sqlDB.SetMaxOpenConns(conns)
286+
sqlDB.SetMaxIdleConns(conns)
287+
sqlDB.SetConnMaxLifetime(time.Hour)
288+
sqlDB.SetConnMaxIdleTime(3 * time.Minute)
289+
290+
log.Infof("Set max open db connections to %d, max idle to %d, max lifetime to %v, max idle time to %v",
291+
conns, conns, time.Hour, 3*time.Minute)
292+
286293
return db, nil
287294
}

management/server/store/sql_store.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine types.Engine, met
8989
}
9090

9191
sql.SetMaxOpenConns(conns)
92+
sql.SetMaxIdleConns(conns)
93+
sql.SetConnMaxLifetime(time.Hour)
94+
sql.SetConnMaxIdleTime(3 * time.Minute)
9295

93-
log.WithContext(ctx).Infof("Set max open db connections to %d", conns)
96+
log.WithContext(ctx).Infof("Set max open db connections to %d, max idle to %d, max lifetime to %v, max idle time to %v",
97+
conns, conns, time.Hour, 3*time.Minute)
9498

9599
if skipMigration {
96100
log.WithContext(ctx).Infof("skipping migration")

0 commit comments

Comments
 (0)