fix: set db connection keepalive options (#22357)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2025-04-03 19:52:29 +08:00
committed by GitHub
co-authored by Qiu Jian
parent 4edc483209
commit 54c4e78470
2 changed files with 12 additions and 0 deletions
+10
View File
@@ -94,6 +94,16 @@ func InitDB(options *common_options.DBOptions) {
}
sqlchemy.SetDBWithNameBackend(dbConn, sqlchemy.DefaultDB, backend)
if options.DbMaxWaitTimeoutSeconds <= 300 {
options.DbMaxWaitTimeoutSeconds = 3600
}
// ConnMaxLifetime is the maximum amount of time a connection may be reused.
// mysql default max_waitimeout is 28800 seconds, 1 hour should be enough
// but if user set a customized mysql max_waittimeout, the value should be adjusted accordingly
dbConn.SetConnMaxLifetime(time.Duration(options.DbMaxWaitTimeoutSeconds) * time.Second)
// ConnMaxIdleTime should be half of ConnMaxLifetime
dbConn.SetConnMaxIdleTime(time.Duration(options.DbMaxWaitTimeoutSeconds/2) * time.Second)
dialect, sqlStr, err = options.GetClickhouseConnStr()
if err == nil {
// connect to clickcloud
+2
View File
@@ -165,6 +165,8 @@ type DBOptions struct {
Clickhouse string `help:"Connection string for click house"`
DbMaxWaitTimeoutSeconds int `help:"max wait timeout for db connection, default 1 hour" default:"3600"`
OpsLogWithClickhouse bool `help:"store operation logs with clickhouse" default:"false"`
EnableDBChecksumTables bool `help:"Enable DB tables with record checksum for consistency"`
DBChecksumSkipInit bool `help:"Skip DB tables with record checksum calculation when init" default:"false"`