ms-auth/pkg/external/postgres/postgres.go
Vyacheslav1557 93eaf89d78 refactor:
2024-10-09 22:07:38 +05:00

31 lines
539 B
Go

package postgres
import (
"github.com/jmoiron/sqlx"
"time"
)
const (
maxOpenConns = 60
connMaxLifetime = 120
maxIdleConns = 30
connMaxIdleTime = 20
)
func NewPostgresDB(dsn string) (*sqlx.DB, error) {
db, err := sqlx.Open("pgx", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxOpenConns)
db.SetConnMaxLifetime(connMaxLifetime * time.Second)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxIdleTime(connMaxIdleTime * time.Second)
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}