forked from mtusi2026/mtusi2026lab2
Lab2- оценка 5
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type PoolOptions struct {
|
||||
MaxConns int32
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, url string, opt PoolOptions) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.MaxConns = opt.MaxConns
|
||||
cfg.MinConns = 1
|
||||
cfg.MaxConnLifetime = 30 * time.Minute
|
||||
cfg.MaxConnIdleTime = 5 * time.Minute
|
||||
cfg.HealthCheckPeriod = 15 * time.Second
|
||||
cfg.ConnConfig.ConnectTimeout = 3 * time.Second
|
||||
rp := cfg.ConnConfig.RuntimeParams
|
||||
rp["application_name"] = "mtusi-messenger"
|
||||
rp["statement_timeout"] = "5000"
|
||||
rp["lock_timeout"] = "2000"
|
||||
rp["idle_in_transaction_session_timeout"] = "10000"
|
||||
return pgxpool.NewWithConfig(ctx, cfg)
|
||||
}
|
||||
Reference in New Issue
Block a user