Skip to content

Commit b47d857

Browse files
committed
atualizar limites de conexão
1 parent 3fbd433 commit b47d857

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/database/db.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
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

0 commit comments

Comments
 (0)