@@ -85,9 +85,19 @@ func parseDBConfig(dbConfig config.DatabaseConfiguration, src ...string) (*Postg
8585 return nil , fmt .Errorf ("failed to create %sconnection pool: %w" , src , err )
8686 }
8787
88- if dbConfig .SetMaxOpenConnections > 0 {
89- pgxCfg .MaxConns = int32 (dbConfig .SetMaxOpenConnections )
88+ // Set MaxConns - use configured value or default to 100 if not set
89+ // This prevents unlimited connections when SetMaxOpenConnections is 0
90+ maxConns := dbConfig .SetMaxOpenConnections
91+ if maxConns <= 0 {
92+ maxConns = 100
93+ log .Warnf ("[%s]: SetMaxOpenConnections not set or 0, using default: %d. Set CONVOY_DB_MAX_OPEN_CONN to override." , pkgName , maxConns )
9094 }
95+ pgxCfg .MaxConns = int32 (maxConns )
96+
97+ if dbConfig .SetMaxIdleConnections > 0 {
98+ pgxCfg .MaxConnIdleTime = time .Minute * 5
99+ }
100+
91101 pgxCfg .MaxConnLifetime = time .Second * time .Duration (dbConfig .SetConnMaxLifetime )
92102 pgxCfg .ConnConfig .Tracer = otelpgx .NewTracer (otelpgx .WithTrimSQLInSpanName ())
93103
@@ -100,6 +110,18 @@ func parseDBConfig(dbConfig config.DatabaseConfiguration, src ...string) (*Postg
100110 sqlDB := stdlib .OpenDBFromPool (pool )
101111 db := sqlx .NewDb (sqlDB , "pgx" )
102112
113+ maxOpenConns := maxConns
114+ maxIdleConns := dbConfig .SetMaxIdleConnections
115+ if maxIdleConns <= 0 {
116+ maxIdleConns = maxOpenConns / 4
117+ if maxIdleConns < 2 {
118+ maxIdleConns = 2
119+ }
120+ }
121+ db .SetMaxOpenConns (maxOpenConns )
122+ db .SetMaxIdleConns (maxIdleConns )
123+ db .SetConnMaxLifetime (time .Second * time .Duration (dbConfig .SetConnMaxLifetime ))
124+
103125 return & Postgres {dbx : db , pool : pool , conn : pool }, nil
104126}
105127
0 commit comments