完善数据库 SQL 约束与运维治理
This commit is contained in:
@@ -21,6 +21,10 @@ type structuredGormLogger struct {
|
||||
}
|
||||
|
||||
func newGormLogger(logLevel string, logger *zap.Logger) gormLogger.Interface {
|
||||
return newGormLoggerWithThreshold(logLevel, logger, 500*time.Millisecond)
|
||||
}
|
||||
|
||||
func newGormLoggerWithThreshold(logLevel string, logger *zap.Logger, slowThreshold time.Duration) gormLogger.Interface {
|
||||
level := gormLogger.Warn
|
||||
if strings.EqualFold(strings.TrimSpace(logLevel), "debug") {
|
||||
level = gormLogger.Info
|
||||
@@ -28,10 +32,13 @@ func newGormLogger(logLevel string, logger *zap.Logger) gormLogger.Interface {
|
||||
if logger == nil {
|
||||
logger = zap.L()
|
||||
}
|
||||
if slowThreshold <= 0 {
|
||||
slowThreshold = 500 * time.Millisecond
|
||||
}
|
||||
return &structuredGormLogger{
|
||||
logger: logger.With(zap.String("module", "database")),
|
||||
level: level,
|
||||
slowThreshold: 500 * time.Millisecond,
|
||||
slowThreshold: slowThreshold,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,50 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MySQLOptions struct {
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int
|
||||
ConnMaxLifetime time.Duration
|
||||
ConnMaxIdleTime time.Duration
|
||||
SlowQueryThreshold time.Duration
|
||||
}
|
||||
|
||||
func DefaultMySQLOptions() MySQLOptions {
|
||||
return MySQLOptions{
|
||||
MaxOpenConns: 50,
|
||||
MaxIdleConns: 10,
|
||||
ConnMaxLifetime: 30 * time.Minute,
|
||||
ConnMaxIdleTime: 5 * time.Minute,
|
||||
SlowQueryThreshold: 500 * time.Millisecond,
|
||||
}
|
||||
}
|
||||
|
||||
func OpenMySQL(dsn string, logLevel string, appLogger *zap.Logger) (*gorm.DB, error) {
|
||||
return OpenMySQLWithOptions(dsn, logLevel, appLogger, DefaultMySQLOptions())
|
||||
}
|
||||
|
||||
func OpenMySQLWithOptions(dsn string, logLevel string, appLogger *zap.Logger, options MySQLOptions) (*gorm.DB, error) {
|
||||
defaults := DefaultMySQLOptions()
|
||||
if options.MaxOpenConns < 1 {
|
||||
options.MaxOpenConns = defaults.MaxOpenConns
|
||||
}
|
||||
if options.MaxIdleConns < 0 {
|
||||
options.MaxIdleConns = defaults.MaxIdleConns
|
||||
}
|
||||
if options.ConnMaxLifetime <= 0 {
|
||||
options.ConnMaxLifetime = defaults.ConnMaxLifetime
|
||||
}
|
||||
if options.ConnMaxIdleTime < 0 {
|
||||
options.ConnMaxIdleTime = defaults.ConnMaxIdleTime
|
||||
}
|
||||
if options.SlowQueryThreshold <= 0 {
|
||||
options.SlowQueryThreshold = defaults.SlowQueryThreshold
|
||||
}
|
||||
if options.MaxIdleConns > options.MaxOpenConns {
|
||||
options.MaxIdleConns = options.MaxOpenConns
|
||||
}
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: newGormLogger(logLevel, appLogger),
|
||||
Logger: newGormLoggerWithThreshold(logLevel, appLogger, options.SlowQueryThreshold),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -20,11 +61,11 @@ func OpenMySQL(dsn string, logLevel string, appLogger *zap.Logger) (*gorm.DB, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 限制连接池,避免本地压测瞬间打满 MySQL max_connections。
|
||||
sqlDB.SetMaxOpenConns(50)
|
||||
sqlDB.SetMaxIdleConns(10)
|
||||
sqlDB.SetConnMaxLifetime(30 * time.Minute)
|
||||
sqlDB.SetConnMaxIdleTime(5 * time.Minute)
|
||||
// 限制连接池,避免单个实例耗尽 MySQL max_connections。
|
||||
sqlDB.SetMaxOpenConns(options.MaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||
sqlDB.SetConnMaxIdleTime(options.ConnMaxIdleTime)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user