完善数据库 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) {
|
||||
|
||||
Reference in New Issue
Block a user