perf(db): configure database connection pools
Add configurable PostgreSQL and database/sql pool limits, validate invalid environment values, clamp idle connections, and cover the parsing/application logic with unit tests.
This commit is contained in:
@@ -90,6 +90,62 @@ import (
|
||||
wgrpc "github.com/weaviate/weaviate-go-client/v5/weaviate/grpc"
|
||||
)
|
||||
|
||||
func nonNegativeEnvInt(name string, fallback int) int {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil || value < 0 {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func nonNegativeEnvDuration(name string, fallback time.Duration) time.Duration {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
if raw == "0" {
|
||||
return 0
|
||||
}
|
||||
|
||||
value, err := time.ParseDuration(raw)
|
||||
if err != nil || value < 0 {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
type databasePoolConfig struct {
|
||||
maxOpen int
|
||||
maxIdle int
|
||||
maxIdleTime time.Duration
|
||||
maxLifetime time.Duration
|
||||
}
|
||||
|
||||
func databasePoolConfigFromEnv() databasePoolConfig {
|
||||
cfg := databasePoolConfig{
|
||||
maxOpen: nonNegativeEnvInt("DB_MAX_OPEN_CONNS", 0),
|
||||
maxIdle: nonNegativeEnvInt("DB_MAX_IDLE_CONNS", 10),
|
||||
maxIdleTime: nonNegativeEnvDuration("DB_CONN_MAX_IDLE_TIME", 0),
|
||||
maxLifetime: nonNegativeEnvDuration("DB_CONN_MAX_LIFETIME", 10*time.Minute),
|
||||
}
|
||||
if cfg.maxOpen > 0 && cfg.maxIdle > cfg.maxOpen {
|
||||
cfg.maxIdle = cfg.maxOpen
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func applyDatabasePoolConfig(db *sql.DB, cfg databasePoolConfig) {
|
||||
db.SetMaxOpenConns(cfg.maxOpen)
|
||||
db.SetMaxIdleConns(cfg.maxIdle)
|
||||
db.SetConnMaxIdleTime(cfg.maxIdleTime)
|
||||
db.SetConnMaxLifetime(cfg.maxLifetime)
|
||||
}
|
||||
|
||||
// BuildContainer constructs the dependency injection container
|
||||
// Registers all components, services, repositories and handlers needed by the application
|
||||
// Creates a fully configured application container with proper dependency resolution
|
||||
@@ -608,10 +664,19 @@ func initDatabase(cfg *config.Config) (*gorm.DB, error) {
|
||||
// Limiting to a single open connection serialises all DB access and
|
||||
// prevents "database is locked" errors from concurrent goroutines.
|
||||
sqlDB.SetMaxOpenConns(1)
|
||||
sqlDB.SetConnMaxLifetime(10 * time.Minute)
|
||||
} else {
|
||||
sqlDB.SetMaxIdleConns(10)
|
||||
poolCfg := databasePoolConfigFromEnv()
|
||||
applyDatabasePoolConfig(sqlDB, poolCfg)
|
||||
logger.Infof(
|
||||
context.Background(),
|
||||
"Database pool configured: max_open=%d max_idle=%d max_idle_time=%s max_lifetime=%s",
|
||||
poolCfg.maxOpen,
|
||||
poolCfg.maxIdle,
|
||||
poolCfg.maxIdleTime,
|
||||
poolCfg.maxLifetime,
|
||||
)
|
||||
}
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(10) * time.Minute)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user