mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-10 16:53:35 +08:00
- 将 SQL Server 单条空闲连接保留时间由 30 秒延长至 30 分钟 - 保持最大连接数和 30 分钟生命周期轮换策略不变 - Oracle、OceanBase 与其他数据库继续沿用原连接池策略 - 增加空闲窗口与连接复用边界回归测试
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultSQLMaxOpenConns = 4
|
|
defaultSQLMaxIdleConns = 1
|
|
defaultSQLConnMaxLifetime = 30 * time.Minute
|
|
defaultSQLConnMaxIdleTime = 30 * time.Second
|
|
// SQL Server login can be expensive. Keep its single idle connection warm
|
|
// until the normal lifetime rotation instead of expiring it at 30 seconds.
|
|
sqlServerSQLConnMaxIdleTime = defaultSQLConnMaxLifetime
|
|
)
|
|
|
|
func resolveSQLConnectionPoolMaxIdleTime(dbType string) time.Duration {
|
|
if strings.EqualFold(strings.TrimSpace(dbType), "sqlserver") {
|
|
return sqlServerSQLConnMaxIdleTime
|
|
}
|
|
return defaultSQLConnMaxIdleTime
|
|
}
|
|
|
|
func configureSQLConnectionPool(db *sql.DB, dbType string) {
|
|
if db == nil {
|
|
return
|
|
}
|
|
switch strings.ToLower(strings.TrimSpace(dbType)) {
|
|
case "sqlite", "duckdb":
|
|
return
|
|
case "oracle", "oceanbase":
|
|
db.SetMaxOpenConns(defaultSQLMaxOpenConns)
|
|
db.SetMaxIdleConns(defaultSQLMaxIdleConns)
|
|
db.SetConnMaxIdleTime(resolveSQLConnectionPoolMaxIdleTime(dbType))
|
|
db.SetConnMaxLifetime(defaultSQLConnMaxLifetime)
|
|
return
|
|
case "sqlserver":
|
|
db.SetMaxOpenConns(defaultSQLMaxOpenConns)
|
|
db.SetMaxIdleConns(defaultSQLMaxIdleConns)
|
|
db.SetConnMaxIdleTime(resolveSQLConnectionPoolMaxIdleTime(dbType))
|
|
db.SetConnMaxLifetime(defaultSQLConnMaxLifetime)
|
|
return
|
|
}
|
|
db.SetMaxOpenConns(defaultSQLMaxOpenConns)
|
|
db.SetMaxIdleConns(0)
|
|
db.SetConnMaxIdleTime(resolveSQLConnectionPoolMaxIdleTime(dbType))
|
|
db.SetConnMaxLifetime(defaultSQLConnMaxLifetime)
|
|
}
|