Audit with AI

This commit is contained in:
2025-11-16 11:37:02 +05:30
parent 551e2123bc
commit 29cddb6389
24 changed files with 1286 additions and 79 deletions

27
pgm.go
View File

@@ -9,6 +9,7 @@ package pgm
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"sync/atomic"
@@ -45,6 +46,15 @@ func InitPool(conf Config) {
panic(ErrConnStringMissing)
}
// Validate configuration
if conf.MaxConns > 0 && conf.MinConns > 0 && conf.MinConns > conf.MaxConns {
panic(fmt.Errorf("MinConns (%d) cannot be greater than MaxConns (%d)", conf.MinConns, conf.MaxConns))
}
if conf.MaxConns < 0 || conf.MinConns < 0 {
panic(errors.New("connection pool configuration cannot have negative values"))
}
cfg, err := pgxpool.ParseConfig(conf.ConnString)
if err != nil {
panic(err)
@@ -55,7 +65,7 @@ func InitPool(conf Config) {
}
if conf.MinConns > 0 {
cfg.MinConns = conf.MaxConns // 5
cfg.MinConns = conf.MinConns // 5
}
if conf.MaxConnLifetime > 0 {
@@ -83,15 +93,24 @@ func GetPool() *pgxpool.Pool {
return poolPGX.Load()
}
// ClosePool closes the connection pool gracefully.
// Should be called during application shutdown.
func ClosePool() {
if p := poolPGX.Load(); p != nil {
p.Close()
poolPGX.Store(nil)
}
}
// BeginTx begins a pgx poll transaction
func BeginTx(ctx context.Context) (pgx.Tx, error) {
tx, err := poolPGX.Load().Begin(ctx)
if err != nil {
slog.Error(err.Error())
return nil, errors.New("failed to open db tx")
slog.Error("failed to begin transaction", "error", err)
return nil, fmt.Errorf("failed to open db tx: %w", err)
}
return tx, err
return tx, nil
}
// IsNotFound error check