File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package database
33import (
44 "context"
55 "log"
6+ "time"
67
78 "github.com/jackc/pgx/v5/pgxpool"
89)
@@ -16,6 +17,12 @@ func NewConnection(databaseURL string) (*pgxpool.Pool, error) {
1617 return nil , err
1718 }
1819
20+ // Apply connection pool limits to reduce memory usage (important for low-RAM environments like Render Free)
21+ config .MaxConns = 4 // Hard cap on max simultaneous DB connections
22+ config .MinConns = 1 // Keep 1 connection alive to reduce cold starts
23+ config .MaxConnLifetime = 30 * time .Minute // Recycle connections after 30min
24+ config .MaxConnIdleTime = 5 * time .Minute // Close idle connections after 5min
25+
1926 // Create a new connection pool
2027 pool , err := pgxpool .NewWithConfig (context .Background (), config )
2128 if err != nil {
@@ -27,7 +34,7 @@ func NewConnection(databaseURL string) (*pgxpool.Pool, error) {
2734 err = pool .Ping (context .Background ())
2835 if err != nil {
2936 log .Printf ("Unable to ping database: %v\n " , err )
30- pool .Close () // Close the pool if ping fails
37+ pool .Close ()
3138 return nil , err
3239 }
3340
You can’t perform that action at this time.
0 commit comments