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
3
3
import (
4
4
"context"
5
5
"log"
6
+ "time"
6
7
7
8
"github.com/jackc/pgx/v5/pgxpool"
8
9
)
@@ -16,6 +17,12 @@ func NewConnection(databaseURL string) (*pgxpool.Pool, error) {
16
17
return nil , err
17
18
}
18
19
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
+
19
26
// Create a new connection pool
20
27
pool , err := pgxpool .NewWithConfig (context .Background (), config )
21
28
if err != nil {
@@ -27,7 +34,7 @@ func NewConnection(databaseURL string) (*pgxpool.Pool, error) {
27
34
err = pool .Ping (context .Background ())
28
35
if err != nil {
29
36
log .Printf ("Unable to ping database: %v\n " , err )
30
- pool .Close () // Close the pool if ping fails
37
+ pool .Close ()
31
38
return nil , err
32
39
}
33
40
You can’t perform that action at this time.
0 commit comments