fixed numerous linter issues

This commit is contained in:
KS Jannette
2026-02-28 20:17:55 -05:00
parent 230dc99dba
commit 90a338d46c
11 changed files with 267 additions and 73 deletions

View File

@@ -1,3 +1,4 @@
// Package database manages PostgreSQL connection pools.
package database
import (
@@ -9,20 +10,34 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)
var pool *pgxpool.Pool
const (
// maxConnIdleSeconds is the maximum idle time for a connection.
maxConnIdleSeconds = 30
// maxConnLifetimeMinutes is the maximum lifetime for a connection.
maxConnLifetimeMinutes = 5
// connectTimeoutSeconds is the timeout for initial connection.
connectTimeoutSeconds = 10
// maxConns is the maximum number of connections in the pool.
maxConns = 20
// minConns is the minimum number of connections in the pool.
minConns = 2
)
var pool *pgxpool.Pool //nolint:gochecknoglobals
// Connect establishes a PostgreSQL connection pool using the given DSN.
func Connect(dsn string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("parse database config: %w", err)
}
cfg.MaxConns = 20
cfg.MinConns = 2
cfg.MaxConnIdleTime = 30 * time.Second
cfg.MaxConnLifetime = 5 * time.Minute
cfg.MaxConns = maxConns
cfg.MinConns = minConns
cfg.MaxConnIdleTime = maxConnIdleSeconds * time.Second
cfg.MaxConnLifetime = maxConnLifetimeMinutes * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), connectTimeoutSeconds*time.Second)
defer cancel()
p, err := pgxpool.NewWithConfig(ctx, cfg)
@@ -32,18 +47,22 @@ func Connect(dsn string) (*pgxpool.Pool, error) {
if err := p.Ping(ctx); err != nil {
p.Close()
return nil, fmt.Errorf("ping database: %w", err)
}
log.Println("Connected to PostgreSQL database")
pool = p
return p, nil
}
// Pool returns the global connection pool.
func Pool() *pgxpool.Pool {
return pool
}
// Close closes the global connection pool.
func Close() {
if pool != nil {
pool.Close()