blob: 2b2900f191227267812a1cbdb2efb2cedaf525a2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package database
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"time"
)
func CreateConnectionPool(parentContext context.Context, databaseURL string) (*pgxpool.Pool, error) {
poolConfiguration, parseError := pgxpool.ParseConfig(databaseURL)
if parseError != nil {
return nil, fmt.Errorf("failed to parse database URL: %w", parseError)
}
poolConfiguration.MaxConns = 25
poolConfiguration.MinConns = 5
poolConfiguration.MaxConnLifetime = 30 * time.Minute
poolConfiguration.MaxConnIdleTime = 5 * time.Minute
poolConfiguration.HealthCheckPeriod = 30 * time.Second
poolConfiguration.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
connectionPool, connectionError := pgxpool.NewWithConfig(parentContext, poolConfiguration)
if connectionError != nil {
return nil, fmt.Errorf("failed to create connection pool: %w", connectionError)
}
pingError := connectionPool.Ping(parentContext)
if pingError != nil {
connectionPool.Close()
return nil, fmt.Errorf("failed to ping database: %w", pingError)
}
return connectionPool, nil
}
|