fix(tester): improve error handling
This commit is contained in:
parent
c67405f584
commit
e6088953b9
14 changed files with 103 additions and 206 deletions
31
pkg/postgres-client.go
Normal file
31
pkg/postgres-client.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue