完善数据库 SQL 约束与运维治理
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -29,6 +30,7 @@ type Config struct {
|
||||
Realname RealnameConfig
|
||||
Log LogConfig
|
||||
RateLimit RateLimitConfig
|
||||
Database DatabaseConfig
|
||||
BackupStatusFile string
|
||||
}
|
||||
|
||||
@@ -70,6 +72,14 @@ type RateLimitConfig struct {
|
||||
RequestsPerMinute int
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int
|
||||
ConnMaxLifetime time.Duration
|
||||
ConnMaxIdleTime time.Duration
|
||||
SlowQueryThreshold time.Duration
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
AppEnv: getEnv("APP_ENV", "development"),
|
||||
@@ -131,6 +141,13 @@ func Load() Config {
|
||||
Enabled: getEnvBool("RATE_LIMIT_ENABLED", true),
|
||||
RequestsPerMinute: getEnvInt("RATE_LIMIT_REQUESTS_PER_MINUTE", 300),
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
MaxOpenConns: getEnvIntMin("DATABASE_MAX_OPEN_CONNS", 50, 1),
|
||||
MaxIdleConns: getEnvIntMin("DATABASE_MAX_IDLE_CONNS", 10, 0),
|
||||
ConnMaxLifetime: time.Duration(getEnvIntMin("DATABASE_CONN_MAX_LIFETIME_MINUTES", 30, 1)) * time.Minute,
|
||||
ConnMaxIdleTime: time.Duration(getEnvIntMin("DATABASE_CONN_MAX_IDLE_TIME_MINUTES", 5, 0)) * time.Minute,
|
||||
SlowQueryThreshold: time.Duration(getEnvIntMin("DATABASE_SLOW_QUERY_THRESHOLD_MS", 500, 1)) * time.Millisecond,
|
||||
},
|
||||
BackupStatusFile: getEnv("BACKUP_STATUS_FILE", "/var/run/hfb-backup-status/status.json"),
|
||||
}
|
||||
}
|
||||
@@ -208,6 +225,14 @@ func getEnvInt(key string, fallback int) int {
|
||||
return parsed
|
||||
}
|
||||
|
||||
func getEnvIntMin(key string, fallback, minimum int) int {
|
||||
value := getEnvInt(key, fallback)
|
||||
if value < minimum {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvBool(key string, fallback bool) bool {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
|
||||
@@ -26,6 +26,41 @@ func TestIsProductionEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDatabaseConfigFromEnvironment(t *testing.T) {
|
||||
t.Setenv("DATABASE_MAX_OPEN_CONNS", "80")
|
||||
t.Setenv("DATABASE_MAX_IDLE_CONNS", "16")
|
||||
t.Setenv("DATABASE_CONN_MAX_LIFETIME_MINUTES", "45")
|
||||
t.Setenv("DATABASE_CONN_MAX_IDLE_TIME_MINUTES", "7")
|
||||
t.Setenv("DATABASE_SLOW_QUERY_THRESHOLD_MS", "750")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Database.MaxOpenConns != 80 || cfg.Database.MaxIdleConns != 16 {
|
||||
t.Fatalf("database pool = %+v, want open=80 idle=16", cfg.Database)
|
||||
}
|
||||
if cfg.Database.ConnMaxLifetime.Minutes() != 45 || cfg.Database.ConnMaxIdleTime.Minutes() != 7 {
|
||||
t.Fatalf("database lifetimes = %s/%s, want 45m/7m", cfg.Database.ConnMaxLifetime, cfg.Database.ConnMaxIdleTime)
|
||||
}
|
||||
if cfg.Database.SlowQueryThreshold.Milliseconds() != 750 {
|
||||
t.Fatalf("slow query threshold = %s, want 750ms", cfg.Database.SlowQueryThreshold)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDatabaseConfigInvalidValuesUseDefaults(t *testing.T) {
|
||||
t.Setenv("DATABASE_MAX_OPEN_CONNS", "0")
|
||||
t.Setenv("DATABASE_MAX_IDLE_CONNS", "-1")
|
||||
t.Setenv("DATABASE_CONN_MAX_LIFETIME_MINUTES", "0")
|
||||
t.Setenv("DATABASE_CONN_MAX_IDLE_TIME_MINUTES", "-1")
|
||||
t.Setenv("DATABASE_SLOW_QUERY_THRESHOLD_MS", "0")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Database.MaxOpenConns != 50 || cfg.Database.MaxIdleConns != 10 {
|
||||
t.Fatalf("invalid pool values = %+v, want defaults open=50 idle=10", cfg.Database)
|
||||
}
|
||||
if cfg.Database.ConnMaxLifetime.Minutes() != 30 || cfg.Database.ConnMaxIdleTime.Minutes() != 5 || cfg.Database.SlowQueryThreshold.Milliseconds() != 500 {
|
||||
t.Fatalf("invalid database durations = %+v, want defaults", cfg.Database)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric 验证非生产环境未设置 legacy 时
|
||||
// 回退到历史硬编码密钥(开发态零配置兼容旧密文)。
|
||||
func TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric(t *testing.T) {
|
||||
|
||||
@@ -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